Commit c84fd53f authored by hybrid's avatar hybrid

Fix HSL color class and conversions from/to RGB classes.

Fix Color selection dialog. It's now fully usable and gives correct results. The color display ring could be reworked.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@3616 dfc29bdd-3216-0410-991c-e03cc46cb475
parent c9d021cc
......@@ -583,9 +583,10 @@ namespace video
};
//! Class representing a color in HSV format
/** The color values for hue, saturation, value
are stored in a 32 bit floating point variable.
//! Class representing a color in HSL format
/** The color values for hue, saturation, luminance
are stored in 32bit floating point variables. Hue is in range [0,360],
Luminance and Saturation are in percent [0,100]
*/
class SColorHSL
{
......@@ -593,24 +594,23 @@ namespace video
SColorHSL ( f32 h = 0.f, f32 s = 0.f, f32 l = 0.f )
: Hue ( h ), Saturation ( s ), Luminance ( l ) {}
void fromRGB(const SColor &color);
void toRGB(SColor &color) const;
void fromRGB(const SColorf &color);
void toRGB(SColorf &color) const;
f32 Hue;
f32 Saturation;
f32 Luminance;
private:
inline u32 toRGB1(f32 rm1, f32 rm2, f32 rh) const;
inline f32 toRGB1(f32 rm1, f32 rm2, f32 rh) const;
};
inline void SColorHSL::fromRGB(const SColor &color)
inline void SColorHSL::fromRGB(const SColorf &color)
{
const u32 maxValInt = core::max_(color.getRed(), color.getGreen(), color.getBlue());
const f32 maxVal = (f32)maxValInt;
const f32 maxVal = core::max_(color.getRed(), color.getGreen(), color.getBlue());
const f32 minVal = (f32)core::min_(color.getRed(), color.getGreen(), color.getBlue());
Luminance = (maxVal/minVal)*0.5f;
Luminance = (maxVal+minVal)*50;
if (core::equals(maxVal, minVal))
{
Hue=0.f;
......@@ -619,7 +619,7 @@ namespace video
}
const f32 delta = maxVal-minVal;
if ( Luminance <= 0.5f )
if ( Luminance <= 50 )
{
Saturation = (delta)/(maxVal+minVal);
}
......@@ -627,71 +627,70 @@ namespace video
{
Saturation = (delta)/(2-maxVal-minVal);
}
Saturation *= 100;
if (maxValInt == color.getRed())
if (core::equals(maxVal, color.getRed()))
Hue = (color.getGreen()-color.getBlue())/delta;
else if (maxValInt == color.getGreen())
Hue = 2+(color.getBlue()-color.getRed())/delta;
else if (core::equals(maxVal, color.getGreen()))
Hue = 2+((color.getBlue()-color.getRed())/delta);
else // blue is max
Hue = 4+(color.getRed()-color.getGreen())/delta;
Hue = 4+((color.getRed()-color.getGreen())/delta);
Hue *= (60.0f * core::DEGTORAD);
Hue *= 60.0f;
while ( Hue < 0.f )
Hue += 2.f * core::PI;
Hue += 360;
}
inline void SColorHSL::toRGB(SColor &color) const
inline void SColorHSL::toRGB(SColorf &color) const
{
const f32 l = Luminance/100;
if (core::iszero(Saturation)) // grey
{
u8 c = (u8) ( Luminance * 255.0 );
color.setRed(c);
color.setGreen(c);
color.setBlue(c);
color.set(l, l, l);
return;
}
f32 rm2;
if ( Luminance <= 0.5f )
if ( Luminance <= 50 )
{
rm2 = Luminance + Luminance * Saturation;
rm2 = l + l * (Saturation/100);
}
else
{
rm2 = Luminance + Saturation - Luminance * Saturation;
rm2 = l + (1 - l) * (Saturation/100);
}
const f32 rm1 = 2.0f * Luminance - rm2;
const f32 rm1 = 2.0f * l - rm2;
color.setRed ( toRGB1(rm1, rm2, Hue + (120.0f * core::DEGTORAD )) );
color.setGreen ( toRGB1(rm1, rm2, Hue) );
color.setBlue ( toRGB1(rm1, rm2, Hue - (120.0f * core::DEGTORAD) ) );
const f32 h = Hue / 360.0f;
color.set( toRGB1(rm1, rm2, h + 1.f/3.f),
toRGB1(rm1, rm2, h),
toRGB1(rm1, rm2, h - 1.f/3.f)
);
}
inline u32 SColorHSL::toRGB1(f32 rm1, f32 rm2, f32 rh) const
// algorithm from Foley/Van-Dam
inline f32 SColorHSL::toRGB1(f32 rm1, f32 rm2, f32 rh) const
{
while ( rh > 2.f * core::PI )
rh -= 2.f * core::PI;
while ( rh < 0.f )
rh += 2.f * core::PI;
if (rh < 60.0f * core::DEGTORAD )
rm1 = rm1 + (rm2 - rm1) * rh / (60.0f * core::DEGTORAD);
else if (rh < 180.0f * core::DEGTORAD )
if (rh<0)
rh += 1;
if (rh>1)
rh -= 1;
if (rh < 1.f/6.f)
rm1 = rm1 + (rm2 - rm1) * rh*6.f;
else if (rh < 0.5f)
rm1 = rm2;
else if (rh < 240.0f * core::DEGTORAD )
rm1 = rm1 + (rm2 - rm1) * ( ( 240.0f * core::DEGTORAD ) - rh) /
(60.0f * core::DEGTORAD);
else if (rh < 2.f/3.f)
rm1 = rm1 + (rm2 - rm1) * ((2.f/3.f)-rh)*6.f;
return (u32) core::round32(rm1 * 255.f);
return rm1;
}
} // end namespace video
} // end namespace irr
#endif
This diff is collapsed.
......@@ -10,8 +10,7 @@
#include "IGUIColorSelectDialog.h"
#include "IGUIButton.h"
#include "IGUIEditBox.h"
#include "IGUIScrollBar.h"
#include "IGUISpinBox.h"
#include "IGUIImage.h"
#include "irrArray.h"
......@@ -37,6 +36,9 @@ namespace gui
//! draws the element and its children
virtual void draw();
virtual video::SColor getColor();
virtual video::SColorHSL getColorHSL();
private:
//! sends the event that the file has been selected.
......@@ -51,14 +53,7 @@ namespace gui
IGUIButton* OKButton;
IGUIButton* CancelButton;
struct SBatteryItem
{
f32 Incoming;
f32 Outgoing;
IGUIEditBox * Edit;
IGUIScrollBar *Scrollbar;
};
core::array< SBatteryItem > Battery;
core::array<IGUISpinBox*> Battery;
struct SColorCircle
{
......@@ -77,4 +72,3 @@ namespace gui
#endif // _IRR_COMPILE_WITH_GUI_
#endif // __C_GUI_COLOR_SELECT_DIALOG_H_INCLUDED__
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment