change hsb to doubles internally

This commit is contained in:
marauder2k7 2025-01-22 20:12:49 +00:00
parent 5ca1c37fe9
commit 029a495de1
6 changed files with 73 additions and 51 deletions

View file

@ -1095,7 +1095,7 @@ DefineEngineFunction(ColorRGBToHEX, const char*, (ColorI color), ,
return Con::getReturnBuffer(color.getHex()); return Con::getReturnBuffer(color.getHex());
} }
DefineEngineFunction(ColorRGBToHSB, Point3I, (ColorI color), , DefineEngineFunction(ColorRGBToHSB, const char*, (ColorI color), ,
"Convert from a integer RGB (red, green, blue) color to HSB (hue, saturation, brightness). HSB is also know as HSL or HSV as well, with the last letter standing for lightness or value.\n" "Convert from a integer RGB (red, green, blue) color to HSB (hue, saturation, brightness). HSB is also know as HSL or HSV as well, with the last letter standing for lightness or value.\n"
"@param color Integer color value to be converted in the form \"R G B A\", where R is red, G is green, B is blue, and A is alpha. It excepts an alpha, but keep in mind this will not be converted.\n" "@param color Integer color value to be converted in the form \"R G B A\", where R is red, G is green, B is blue, and A is alpha. It excepts an alpha, but keep in mind this will not be converted.\n"
"@return HSB color value, alpha isn't handled/converted so it is only the RGB value\n\n" "@return HSB color value, alpha isn't handled/converted so it is only the RGB value\n\n"
@ -1104,9 +1104,9 @@ DefineEngineFunction(ColorRGBToHSB, Point3I, (ColorI color), ,
"@endtsexample\n" "@endtsexample\n"
"@ingroup Strings") "@ingroup Strings")
{ {
ColorI::Hsb hsb(color.getHSB()); ColorI::Hsb hsb(color.getHSB());
Point3I hsbPoint(hsb.hue, hsb.sat, hsb.brightness); String s(String::ToString(hsb.hue) + " " + String::ToString(hsb.sat) + " " + String::ToString(hsb.brightness));
return hsbPoint; return Con::getReturnBuffer(s);
} }
DefineEngineFunction(ColorHEXToRGB, ColorI, (const char* hex), , DefineEngineFunction(ColorHEXToRGB, ColorI, (const char* hex), ,

View file

@ -409,6 +409,27 @@ ConsoleSetType( TypeS32Vector )
else else
Con::printf("Vector<S32> must be set as { a, b, c, ... } or \"a b c ...\""); Con::printf("Vector<S32> must be set as { a, b, c, ... } or \"a b c ...\"");
} }
//-----------------------------------------------------------------------------
// TypeF64
//-----------------------------------------------------------------------------
ConsoleType(double, TypeF64, F64, "")
ImplementConsoleTypeCasters(TypeF64, F64)
ConsoleGetType(TypeF64)
{
static const U32 bufSize = 256;
char* returnBuffer = Con::getReturnBuffer(bufSize);
dSprintf(returnBuffer, bufSize, "%Lg", *((F64*)dptr));
return returnBuffer;
}
ConsoleSetType(TypeF64)
{
if (argc == 1)
*((F64*)dptr) = dAtod(argv[0]);
else
Con::printf("(TypeF64) Cannot set multiple args to a single F64.");
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TypeF32 // TypeF32

View file

@ -66,6 +66,7 @@ DefineConsoleType( TypeBoolVector, Vector<bool>)
DefineConsoleType( TypeS8, S8 ) DefineConsoleType( TypeS8, S8 )
DefineConsoleType( TypeS32, S32 ) DefineConsoleType( TypeS32, S32 )
DefineConsoleType( TypeS32Vector, Vector<S32> ) DefineConsoleType( TypeS32Vector, Vector<S32> )
DefineConsoleType( TypeF64, F64 )
DefineConsoleType( TypeF32, F32 ) DefineConsoleType( TypeF32, F32 )
DefineConsoleType( TypeF32Vector, Vector<F32> ) DefineConsoleType( TypeF32Vector, Vector<F32> )
DefineUnmappedConsoleType( TypeString, const char * ) // plain UTF-8 strings are not supported in new interop DefineUnmappedConsoleType( TypeString, const char * ) // plain UTF-8 strings are not supported in new interop

View file

@ -129,11 +129,11 @@ public:
struct Hsb struct Hsb
{ {
Hsb() :hue(0), sat(0), brightness(0){}; Hsb() :hue(0), sat(0), brightness(0){};
Hsb(U32 h, U32 s, U32 b) :hue(h), sat(s), brightness(b){}; Hsb(F64 h, F64 s, F64 b) :hue(h), sat(s), brightness(b){};
U32 hue; ///Hue F64 hue; ///Hue
U32 sat; ///Saturation F64 sat; ///Saturation
U32 brightness; //Brightness/Value/Lightness F64 brightness; //Brightness/Value/Lightness
}; };
public: public:
@ -504,9 +504,9 @@ inline void ColorI::set(const Hsb& color)
} }
// Convert normalized [0.0, 1.0] RGB values to integer [0, 255] // Convert normalized [0.0, 1.0] RGB values to integer [0, 255]
red = static_cast<U32>(r * 255.0); red = static_cast<U32>(r * 255.0 + 0.5);
green = static_cast<U32>(g * 255.0); green = static_cast<U32>(g * 255.0 + 0.5);
blue = static_cast<U32>(b * 255.0); blue = static_cast<U32>(b * 255.0 + 0.5);
alpha = 255; // Set alpha to fully opaque alpha = 255; // Set alpha to fully opaque
} }
@ -755,9 +755,9 @@ inline ColorI::Hsb ColorI::getHSB() const
// Prepare the output HSB struct // Prepare the output HSB struct
ColorI::Hsb val; ColorI::Hsb val;
val.hue = static_cast<U32>(H + 0.5); // Round to nearest integer val.hue = H; // Round to nearest integer
val.sat = static_cast<U32>(S * 100.0 + 0.5); // Convert to percentage val.sat = S * 100.0; // Convert to percentage
val.brightness = static_cast<U32>(B * 100.0 + 0.5); // Convert to percentage val.brightness = B * 100.0; // Convert to percentage
return val; return val;
} }

View file

@ -234,7 +234,7 @@ void GuiColorPickerCtrl::renderAlphaGradient(RectI& bounds)
ColorI currentColor; ColorI currentColor;
currentColor.set(ColorI::Hsb(mSelectedHue, 100, 100)); currentColor.set(ColorI::Hsb(mSelectedHue, 100, 100));
ColorI alphaCol = currentColor; ColorI alphaCol = ColorI::BLACK;
alphaCol.alpha = 0; alphaCol.alpha = 0;
// Begin primitive building, 4 vertices per rectangle // Begin primitive building, 4 vertices per rectangle
@ -448,8 +448,8 @@ void GuiColorPickerCtrl::onMouseDown(const GuiEvent &event)
{ {
F32 relX = F32(mousePoint.x) / F32(ext.x); F32 relX = F32(mousePoint.x) / F32(ext.x);
F32 relY = 1.0f - F32(mousePoint.y) / F32(ext.y); F32 relY = 1.0f - F32(mousePoint.y) / F32(ext.y);
setSelectedSaturation(static_cast<U32>(relX * 100.0f)); setSelectedSaturation(relX * 100.0);
setSelectedBrightness(static_cast<U32>(relY * 100.0f)); setSelectedBrightness(relY * 100.0);
break; break;
} }
case GuiColorPickerCtrl::pHueRange: case GuiColorPickerCtrl::pHueRange:
@ -459,13 +459,13 @@ void GuiColorPickerCtrl::onMouseDown(const GuiEvent &event)
case GuiColorPickerCtrl::sHorizontal: case GuiColorPickerCtrl::sHorizontal:
{ {
F32 relX = F32(mousePoint.x) / F32(ext.x); F32 relX = F32(mousePoint.x) / F32(ext.x);
setSelectedHue(static_cast<U32>(relX * 360.0f)); setSelectedHue(relX * 360.0);
break; break;
} }
case GuiColorPickerCtrl::sVertical: case GuiColorPickerCtrl::sVertical:
{ {
F32 relY = F32(mousePoint.y) / F32(ext.y); F32 relY = F32(mousePoint.y) / F32(ext.y);
setSelectedHue(static_cast<U32>(relY * 360.0f)); setSelectedHue(relY * 360.0);
break; break;
} }
default: default:
@ -480,13 +480,13 @@ void GuiColorPickerCtrl::onMouseDown(const GuiEvent &event)
case GuiColorPickerCtrl::sHorizontal: case GuiColorPickerCtrl::sHorizontal:
{ {
F32 relX = F32(mousePoint.x) / F32(ext.x); F32 relX = F32(mousePoint.x) / F32(ext.x);
setSelectedAlpha(static_cast<U32>(relX * 255.0f)); setSelectedAlpha(relX * 255.0);
break; break;
} }
case GuiColorPickerCtrl::sVertical: case GuiColorPickerCtrl::sVertical:
{ {
F32 relY = F32(mousePoint.y) / F32(ext.y); F32 relY = F32(mousePoint.y) / F32(ext.y);
setSelectedAlpha(static_cast<U32>(relY * 255.0f)); setSelectedAlpha(relY * 255.0);
break; break;
} }
default: default:
@ -518,8 +518,8 @@ void GuiColorPickerCtrl::onMouseDragged(const GuiEvent &event)
{ {
F32 relX = F32(mousePoint.x) / F32(ext.x); F32 relX = F32(mousePoint.x) / F32(ext.x);
F32 relY = 1.0f - F32(mousePoint.y) / F32(ext.y); F32 relY = 1.0f - F32(mousePoint.y) / F32(ext.y);
setSelectedSaturation(static_cast<U32>(relX * 100.0f)); setSelectedSaturation(relX * 100.0);
setSelectedBrightness(static_cast<U32>(relY * 100.0f)); setSelectedBrightness(relY * 100.0);
break; break;
} }
case GuiColorPickerCtrl::pHueRange: case GuiColorPickerCtrl::pHueRange:
@ -529,13 +529,13 @@ void GuiColorPickerCtrl::onMouseDragged(const GuiEvent &event)
case GuiColorPickerCtrl::sHorizontal: case GuiColorPickerCtrl::sHorizontal:
{ {
F32 relX = F32(mousePoint.x) / F32(ext.x); F32 relX = F32(mousePoint.x) / F32(ext.x);
setSelectedHue(static_cast<U32>(relX * 360.0f)); setSelectedHue(relX * 360.0);
break; break;
} }
case GuiColorPickerCtrl::sVertical: case GuiColorPickerCtrl::sVertical:
{ {
F32 relY = F32(mousePoint.y) / F32(ext.y); F32 relY = F32(mousePoint.y) / F32(ext.y);
setSelectedHue(static_cast<U32>(relY * 360.0f)); setSelectedHue(relY * 360.0);
break; break;
} }
default: default:
@ -550,13 +550,13 @@ void GuiColorPickerCtrl::onMouseDragged(const GuiEvent &event)
case GuiColorPickerCtrl::sHorizontal: case GuiColorPickerCtrl::sHorizontal:
{ {
F32 relX = F32(mousePoint.x) / F32(ext.x); F32 relX = F32(mousePoint.x) / F32(ext.x);
setSelectedAlpha(static_cast<U32>(relX * 255.0f)); setSelectedAlpha(relX * 255.0);
break; break;
} }
case GuiColorPickerCtrl::sVertical: case GuiColorPickerCtrl::sVertical:
{ {
F32 relY = F32(mousePoint.y) / F32(ext.y); F32 relY = F32(mousePoint.y) / F32(ext.y);
setSelectedAlpha(static_cast<U32>(relY * 255.0f)); setSelectedAlpha(relY * 255.0);
break; break;
} }
default: default:
@ -587,7 +587,7 @@ void GuiColorPickerCtrl::onMouseLeave(const GuiEvent &)
mMouseOver = false; mMouseOver = false;
} }
void GuiColorPickerCtrl::setSelectedHue(const U32& hueValue) void GuiColorPickerCtrl::setSelectedHue(const F64& hueValue)
{ {
if (hueValue < 0) if (hueValue < 0)
{ {
@ -605,7 +605,7 @@ void GuiColorPickerCtrl::setSelectedHue(const U32& hueValue)
} }
void GuiColorPickerCtrl::setSelectedBrightness(const U32& brightValue) void GuiColorPickerCtrl::setSelectedBrightness(const F64& brightValue)
{ {
if (brightValue < 0) if (brightValue < 0)
{ {
@ -622,7 +622,7 @@ void GuiColorPickerCtrl::setSelectedBrightness(const U32& brightValue)
mSelectedBrightness = brightValue; mSelectedBrightness = brightValue;
} }
void GuiColorPickerCtrl::setSelectedSaturation(const U32& satValue) void GuiColorPickerCtrl::setSelectedSaturation(const F64& satValue)
{ {
if (satValue < 0) if (satValue < 0)
{ {
@ -639,7 +639,7 @@ void GuiColorPickerCtrl::setSelectedSaturation(const U32& satValue)
mSelectedSaturation = satValue; mSelectedSaturation = satValue;
} }
void GuiColorPickerCtrl::setSelectedAlpha(const U32& alphaValue) void GuiColorPickerCtrl::setSelectedAlpha(const F64& alphaValue)
{ {
if (alphaValue < 0) if (alphaValue < 0)
{ {
@ -673,42 +673,42 @@ DefineEngineMethod(GuiColorPickerCtrl, executeUpdate, void, (), , "Execute the o
object->onAction(); object->onAction();
} }
DefineEngineMethod(GuiColorPickerCtrl, setSelectedHue, void, (U32 hueValue), , "Sets the selected hue value should be 0-360.") DefineEngineMethod(GuiColorPickerCtrl, setSelectedHue, void, (F64 hueValue), , "Sets the selected hue value should be 0-360.")
{ {
object->setSelectedHue(hueValue); object->setSelectedHue(hueValue);
} }
DefineEngineMethod(GuiColorPickerCtrl, getSelectedHue, S32, (), , "Gets the current selected hue value.") DefineEngineMethod(GuiColorPickerCtrl, getSelectedHue, F64, (), , "Gets the current selected hue value.")
{ {
return object->getSelectedHue(); return object->getSelectedHue();
} }
DefineEngineMethod(GuiColorPickerCtrl, setSelectedBrightness, void, (U32 brightness), , "Sets the selected brightness value should be 0-100.") DefineEngineMethod(GuiColorPickerCtrl, setSelectedBrightness, void, (F64 brightness), , "Sets the selected brightness value should be 0-100.")
{ {
object->setSelectedBrightness(brightness); object->setSelectedBrightness(brightness);
} }
DefineEngineMethod(GuiColorPickerCtrl, getSelectedBrightness, S32, (), , "Gets the current selected brightness.") DefineEngineMethod(GuiColorPickerCtrl, getSelectedBrightness, F64, (), , "Gets the current selected brightness.")
{ {
return object->getSelectedBrightness(); return object->getSelectedBrightness();
} }
DefineEngineMethod(GuiColorPickerCtrl, setSelectedSaturation, void, (U32 saturation), , "Sets the selected saturation value should be 0-100.") DefineEngineMethod(GuiColorPickerCtrl, setSelectedSaturation, void, (F64 saturation), , "Sets the selected saturation value should be 0-100.")
{ {
object->setSelectedSaturation(saturation); object->setSelectedSaturation(saturation);
} }
DefineEngineMethod(GuiColorPickerCtrl, getSelectedSaturation, S32, (), , "Gets the current selected saturation value.") DefineEngineMethod(GuiColorPickerCtrl, getSelectedSaturation, F64, (), , "Gets the current selected saturation value.")
{ {
return object->getSelectedSaturation(); return object->getSelectedSaturation();
} }
DefineEngineMethod(GuiColorPickerCtrl, setSelectedAlpha, void, (U32 alpha), , "Sets the selected alpha value should be 0-255.") DefineEngineMethod(GuiColorPickerCtrl, setSelectedAlpha, void, (F64 alpha), , "Sets the selected alpha value should be 0-255.")
{ {
object->setSelectedAlpha(alpha); object->setSelectedAlpha(alpha);
} }
DefineEngineMethod(GuiColorPickerCtrl, getSelectedAlpha, S32, (), , "Gets the current selected alpha value.") DefineEngineMethod(GuiColorPickerCtrl, getSelectedAlpha, F64, (), , "Gets the current selected alpha value.")
{ {
return object->getSelectedAlpha(); return object->getSelectedAlpha();
} }

View file

@ -112,10 +112,10 @@ class GuiColorPickerCtrl : public GuiControl
/// @{ /// @{
PickMode mDisplayMode; ///< Current color display mode of the selector PickMode mDisplayMode; ///< Current color display mode of the selector
SelectorMode mSelectorMode; ///< Current color display mode of the selector SelectorMode mSelectorMode; ///< Current color display mode of the selector
U32 mSelectedHue; F64 mSelectedHue;
U32 mSelectedSaturation; F64 mSelectedSaturation;
U32 mSelectedBrightness; F64 mSelectedBrightness;
U32 mSelectedAlpha; F64 mSelectedAlpha;
bool mMouseOver; ///< Mouse is over? bool mMouseOver; ///< Mouse is over?
bool mMouseDown; ///< Mouse button down? bool mMouseDown; ///< Mouse button down?
@ -158,29 +158,29 @@ class GuiColorPickerCtrl : public GuiControl
/// Set the selected hue. /// Set the selected hue.
/// </summary> /// </summary>
/// <param name="hueValue">Hue value, 0 - 360.</param> /// <param name="hueValue">Hue value, 0 - 360.</param>
void setSelectedHue(const U32& hueValue); void setSelectedHue(const F64& hueValue);
U32 getSelectedHue() { return mSelectedHue; } F64 getSelectedHue() { return mSelectedHue; }
/// <summary> /// <summary>
/// Set the selected brightness. /// Set the selected brightness.
/// </summary> /// </summary>
/// <param name="brightValue">Brightness value, 0 - 100.</param> /// <param name="brightValue">Brightness value, 0 - 100.</param>
void setSelectedBrightness(const U32& brightValue); void setSelectedBrightness(const F64& brightValue);
U32 getSelectedBrightness() { return mSelectedBrightness; } F64 getSelectedBrightness() { return mSelectedBrightness; }
/// <summary> /// <summary>
/// Set the selected saturation. /// Set the selected saturation.
/// </summary> /// </summary>
/// <param name="satValue">Saturation value, 0 - 100.</param> /// <param name="satValue">Saturation value, 0 - 100.</param>
void setSelectedSaturation(const U32& satValue); void setSelectedSaturation(const F64& satValue);
U32 getSelectedSaturation() { return mSelectedSaturation; } F64 getSelectedSaturation() { return mSelectedSaturation; }
/// <summary> /// <summary>
/// Set the selected alpha. /// Set the selected alpha.
/// </summary> /// </summary>
/// <param name="alphaValue">Alpha value, 0 - 255.</param> /// <param name="alphaValue">Alpha value, 0 - 255.</param>
void setSelectedAlpha(const U32& alphaValue); void setSelectedAlpha(const F64& alphaValue);
U32 getSelectedAlpha() { return mSelectedAlpha; } F64 getSelectedAlpha() { return mSelectedAlpha; }
}; };
typedef GuiColorPickerCtrl::PickMode GuiColorPickMode; typedef GuiColorPickerCtrl::PickMode GuiColorPickMode;