mirror of
https://github.com/ddnet/ddnet.git
synced 2024-09-20 09:34:19 +00:00
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:
parent
0aa55e224c
commit
f30682be04
|
@ -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);
|
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 WithAlpha(float alpha) const
|
||||||
{
|
{
|
||||||
DerivedT col(static_cast<const DerivedT &>(*this));
|
DerivedT col(static_cast<const DerivedT &>(*this));
|
||||||
col.a = alpha;
|
col.a = alpha;
|
||||||
return col;
|
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>
|
class ColorHSLA : public color4_base<ColorHSLA>
|
||||||
|
|
|
@ -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);
|
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;
|
SValueSelectorProperties Props;
|
||||||
Props.m_UseScroll = false;
|
Props.m_UseScroll = false;
|
||||||
Props.m_IsHex = true;
|
Props.m_IsHex = true;
|
||||||
Props.m_HexPrefix = pColorPicker->m_Alpha ? 8 : 6;
|
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);
|
const unsigned NewHex = pUI->DoValueSelector(&pColorPicker->m_aValueSelectorIds[4], &HexRect, "Hex:", Hex, 0, pColorPicker->m_Alpha ? 0xFFFFFFFFll : 0xFFFFFFll, Props);
|
||||||
if(Hex != NewHex)
|
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)
|
if(!pColorPicker->m_Alpha)
|
||||||
PickerColorHSV.a = A / 255.0f;
|
PickerColorHSV.a = A / 255.0f;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue