Add color4_base::PackAlphaLast and UnpackAlphaLast

So pack and unpack colors as 4 byte unsigned values with the alpha component last. Existing functions assume that alpha is the first component.
This commit is contained in:
Robert Müller 2023-06-20 23:04:52 +02:00
parent 0aa55e224c
commit f30682be04
2 changed files with 30 additions and 19 deletions

View file

@ -114,12 +114,40 @@ public:
return (Alpha ? ((unsigned)round_to_int(a * 255.0f) << 24) : 0) + ((unsigned)round_to_int(x * 255.0f) << 16) + ((unsigned)round_to_int(y * 255.0f) << 8) + (unsigned)round_to_int(z * 255.0f);
}
unsigned PackAlphaLast(bool Alpha = true) const
{
if(Alpha)
return ((unsigned)round_to_int(x * 255.0f) << 24) + ((unsigned)round_to_int(y * 255.0f) << 16) + ((unsigned)round_to_int(z * 255.0f) << 8) + (unsigned)round_to_int(a * 255.0f);
return ((unsigned)round_to_int(x * 255.0f) << 16) + ((unsigned)round_to_int(y * 255.0f) << 8) + (unsigned)round_to_int(z * 255.0f);
}
DerivedT WithAlpha(float alpha) const
{
DerivedT col(static_cast<const DerivedT &>(*this));
col.a = alpha;
return col;
}
template<typename UnpackT>
static UnpackT UnpackAlphaLast(unsigned Color, bool Alpha = true)
{
UnpackT Result;
if(Alpha)
{
Result.x = ((Color >> 24) & 0xFF) / 255.0f;
Result.y = ((Color >> 16) & 0xFF) / 255.0f;
Result.z = ((Color >> 8) & 0xFF) / 255.0f;
Result.a = ((Color >> 0) & 0xFF) / 255.0f;
}
else
{
Result.x = ((Color >> 16) & 0xFF) / 255.0f;
Result.y = ((Color >> 8) & 0xFF) / 255.0f;
Result.z = ((Color >> 0) & 0xFF) / 255.0f;
Result.a = 1.0f;
}
return Result;
}
};
class ColorHSLA : public color4_base<ColorHSLA>

View file

@ -1711,32 +1711,15 @@ CUI::EPopupMenuFunctionResult CUI::PopupColorPicker(void *pContext, CUIRect View
PickerColorHSV = ColorHSVA(H / 255.0f, S / 255.0f, V / 255.0f, A / 255.0f);
const auto RotateByteLeft = [pColorPicker](unsigned Num) {
if(pColorPicker->m_Alpha)
{
// ARGB -> RGBA (internal -> displayed)
return ((Num & 0xFF000000u) >> 24) | (Num << 8);
}
return Num;
};
const auto RotateByteRight = [pColorPicker](unsigned Num) {
if(pColorPicker->m_Alpha)
{
// RGBA -> ARGB (displayed -> internal)
return ((Num & 0xFFu) << 24) | (Num >> 8);
}
return Num;
};
SValueSelectorProperties Props;
Props.m_UseScroll = false;
Props.m_IsHex = true;
Props.m_HexPrefix = pColorPicker->m_Alpha ? 8 : 6;
const unsigned Hex = RotateByteLeft(color_cast<ColorRGBA>(PickerColorHSV).Pack(pColorPicker->m_Alpha));
const unsigned Hex = color_cast<ColorRGBA>(PickerColorHSV).PackAlphaLast(pColorPicker->m_Alpha);
const unsigned NewHex = pUI->DoValueSelector(&pColorPicker->m_aValueSelectorIds[4], &HexRect, "Hex:", Hex, 0, pColorPicker->m_Alpha ? 0xFFFFFFFFll : 0xFFFFFFll, Props);
if(Hex != NewHex)
{
PickerColorHSV = color_cast<ColorHSVA>(ColorRGBA(RotateByteRight(NewHex), pColorPicker->m_Alpha));
PickerColorHSV = color_cast<ColorHSVA>(ColorRGBA::UnpackAlphaLast<ColorRGBA>(NewHex, pColorPicker->m_Alpha));
if(!pColorPicker->m_Alpha)
PickerColorHSV.a = A / 255.0f;
}