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
master
hybrid 2011-03-20 21:44:28 +00:00
parent c9d021cc36
commit c84fd53f13
3 changed files with 198 additions and 181 deletions

View File

@ -583,9 +583,10 @@ namespace video
}; };
//! Class representing a color in HSV format //! Class representing a color in HSL format
/** The color values for hue, saturation, value /** The color values for hue, saturation, luminance
are stored in a 32 bit floating point variable. are stored in 32bit floating point variables. Hue is in range [0,360],
Luminance and Saturation are in percent [0,100]
*/ */
class SColorHSL class SColorHSL
{ {
@ -593,24 +594,23 @@ namespace video
SColorHSL ( f32 h = 0.f, f32 s = 0.f, f32 l = 0.f ) SColorHSL ( f32 h = 0.f, f32 s = 0.f, f32 l = 0.f )
: Hue ( h ), Saturation ( s ), Luminance ( l ) {} : Hue ( h ), Saturation ( s ), Luminance ( l ) {}
void fromRGB(const SColor &color); void fromRGB(const SColorf &color);
void toRGB(SColor &color) const; void toRGB(SColorf &color) const;
f32 Hue; f32 Hue;
f32 Saturation; f32 Saturation;
f32 Luminance; f32 Luminance;
private: 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 = core::max_(color.getRed(), color.getGreen(), color.getBlue());
const f32 maxVal = (f32)maxValInt;
const f32 minVal = (f32)core::min_(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)) if (core::equals(maxVal, minVal))
{ {
Hue=0.f; Hue=0.f;
@ -619,7 +619,7 @@ namespace video
} }
const f32 delta = maxVal-minVal; const f32 delta = maxVal-minVal;
if ( Luminance <= 0.5f ) if ( Luminance <= 50 )
{ {
Saturation = (delta)/(maxVal+minVal); Saturation = (delta)/(maxVal+minVal);
} }
@ -627,71 +627,70 @@ namespace video
{ {
Saturation = (delta)/(2-maxVal-minVal); Saturation = (delta)/(2-maxVal-minVal);
} }
Saturation *= 100;
if (maxValInt == color.getRed()) if (core::equals(maxVal, color.getRed()))
Hue = (color.getGreen()-color.getBlue())/delta; Hue = (color.getGreen()-color.getBlue())/delta;
else if (maxValInt == color.getGreen()) else if (core::equals(maxVal, color.getGreen()))
Hue = 2+(color.getBlue()-color.getRed())/delta; Hue = 2+((color.getBlue()-color.getRed())/delta);
else // blue is max 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 ) 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 if (core::iszero(Saturation)) // grey
{ {
u8 c = (u8) ( Luminance * 255.0 ); color.set(l, l, l);
color.setRed(c);
color.setGreen(c);
color.setBlue(c);
return; return;
} }
f32 rm2; f32 rm2;
if ( Luminance <= 0.5f ) if ( Luminance <= 50 )
{ {
rm2 = Luminance + Luminance * Saturation; rm2 = l + l * (Saturation/100);
} }
else 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 )) ); const f32 h = Hue / 360.0f;
color.setGreen ( toRGB1(rm1, rm2, Hue) ); color.set( toRGB1(rm1, rm2, h + 1.f/3.f),
color.setBlue ( toRGB1(rm1, rm2, Hue - (120.0f * core::DEGTORAD) ) ); 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 ) if (rh<0)
rh -= 2.f * core::PI; rh += 1;
if (rh>1)
rh -= 1;
while ( rh < 0.f ) if (rh < 1.f/6.f)
rh += 2.f * core::PI; rm1 = rm1 + (rm2 - rm1) * rh*6.f;
else if (rh < 0.5f)
if (rh < 60.0f * core::DEGTORAD )
rm1 = rm1 + (rm2 - rm1) * rh / (60.0f * core::DEGTORAD);
else if (rh < 180.0f * core::DEGTORAD )
rm1 = rm2; rm1 = rm2;
else if (rh < 240.0f * core::DEGTORAD ) else if (rh < 2.f/3.f)
rm1 = rm1 + (rm2 - rm1) * ( ( 240.0f * core::DEGTORAD ) - rh) / rm1 = rm1 + (rm2 - rm1) * ((2.f/3.f)-rh)*6.f;
(60.0f * core::DEGTORAD);
return (u32) core::round32(rm1 * 255.f); return rm1;
} }
} // end namespace video } // end namespace video
} // end namespace irr } // end namespace irr
#endif #endif

View File

@ -16,6 +16,7 @@
#include "IFileList.h" #include "IFileList.h"
#include "os.h" #include "os.h"
#include "CImage.h" #include "CImage.h"
#include "fast_atof.h"
namespace irr namespace irr
{ {
@ -25,26 +26,30 @@ namespace gui
const s32 CSD_WIDTH = 350; const s32 CSD_WIDTH = 350;
const s32 CSD_HEIGHT = 300; const s32 CSD_HEIGHT = 300;
struct sTemplate namespace
{
struct subElementPredefines
{ {
const wchar_t *pre; const wchar_t *pre;
const wchar_t *init; const wchar_t *init;
const wchar_t *pos; const wchar_t *post;
int x, y; int x, y;
int range_down ,range_up; int range_down ,range_up;
}; };
static const sTemplate Template [] = static const subElementPredefines Template [] =
{ {
{ L"A:", L"0", 0,20,165, 0, 255 }, { L"A:", L"0", 0,50,165, 0, 255 },
{ L"R:", L"0", 0,20,205, 0, 255 }, { L"R:", L"0", 0,20,205, 0, 255 },
{ L"G:", L"0", 0,20,230, 0, 255 }, { L"G:", L"0", 0,20,230, 0, 255 },
{ L"B:", L"0", 0,20,255, 0, 255 }, { L"B:", L"0", 0,20,255, 0, 255 },
{ L"H:", L"0", L"°",180,205, 0, 360 }, { L"H:", L"0", L"°",80,205, 0, 360 },
{ L"S:", L"0", L"%",180,230, 0, 100 }, { L"S:", L"0", L"%",80,230, 0, 100 },
{ L"L:", L"0", L"%",180,255, 0, 100 }, { L"L:", L"0", L"%",80,255, 0, 100 },
}; };
}
//! constructor //! constructor
CGUIColorSelectDialog::CGUIColorSelectDialog(const wchar_t* title, IGUIEnvironment* environment, IGUIElement* parent, s32 id) CGUIColorSelectDialog::CGUIColorSelectDialog(const wchar_t* title, IGUIEnvironment* environment, IGUIElement* parent, s32 id)
@ -107,7 +112,7 @@ CGUIColorSelectDialog::CGUIColorSelectDialog(const wchar_t* title, IGUIEnvironme
ColorRing.Control->setSubElement(true); ColorRing.Control->setSubElement(true);
ColorRing.Control->grab(); ColorRing.Control->grab();
for ( u32 i = 0; i != sizeof (Template) / sizeof ( sTemplate ); ++i ) for ( u32 i = 0; i != sizeof (Template) / sizeof ( subElementPredefines ); ++i )
{ {
if ( Template[i].pre ) if ( Template[i].pre )
{ {
@ -119,41 +124,28 @@ CGUIColorSelectDialog::CGUIColorSelectDialog(const wchar_t* title, IGUIEnvironme
t->setSubElement(true); t->setSubElement(true);
} }
if ( Template[i].pos ) if ( Template[i].post )
{ {
r.UpperLeftCorner.X = Template[i].x + 52; r.UpperLeftCorner.X = Template[i].x + 56;
r.UpperLeftCorner.Y = Template[i].y; r.UpperLeftCorner.Y = Template[i].y;
r.LowerRightCorner.X = r.UpperLeftCorner.X + 15; r.LowerRightCorner.X = r.UpperLeftCorner.X + 15;
r.LowerRightCorner.Y = r.UpperLeftCorner.Y + 20; r.LowerRightCorner.Y = r.UpperLeftCorner.Y + 20;
IGUIElement *t = Environment->addStaticText( Template[i].pos, r, false, false, this); IGUIElement *t = Environment->addStaticText( Template[i].post, r, false, false, this);
t->setSubElement(true); t->setSubElement(true);
} }
SBatteryItem item;
item.Incoming=0.f;
item.Outgoing=0.f;
r.UpperLeftCorner.X = Template[i].x + 15; r.UpperLeftCorner.X = Template[i].x + 15;
r.UpperLeftCorner.Y = Template[i].y; r.UpperLeftCorner.Y = Template[i].y-2;
r.LowerRightCorner.X = r.UpperLeftCorner.X + 35; r.LowerRightCorner.X = r.UpperLeftCorner.X + 40;
r.LowerRightCorner.Y = r.UpperLeftCorner.Y + 20; r.LowerRightCorner.Y = r.UpperLeftCorner.Y + 20;
item.Edit = Environment->addEditBox( Template[i].init, r, true, this); gui::IGUISpinBox* spin = Environment->addSpinBox( Template[i].init, r, true, this);
item.Edit->setSubElement(true); spin->setSubElement(true);
item.Edit->grab(); spin->setDecimalPlaces(0);
spin->setRange((f32)Template[i].range_down, (f32)Template[i].range_up);
spin->grab();
r.UpperLeftCorner.X = Template[i].x + 70; Battery.push_back(spin);
r.UpperLeftCorner.Y = Template[i].y + 4;
r.LowerRightCorner.X = r.UpperLeftCorner.X + 60;
r.LowerRightCorner.Y = r.UpperLeftCorner.Y + 12;
item.Scrollbar = Environment->addScrollBar(true, r, this);
item.Scrollbar->grab ();
item.Scrollbar->setSubElement(true);
item.Scrollbar->setMax ( Template[i].range_up - Template[i].range_down );
item.Scrollbar->setSmallStep ( 1 );
Battery.push_back ( item );
} }
bringToFront(CancelButton); bringToFront(CancelButton);
@ -173,11 +165,8 @@ CGUIColorSelectDialog::~CGUIColorSelectDialog()
if (CancelButton) if (CancelButton)
CancelButton->drop(); CancelButton->drop();
for ( u32 i = 0; i != Battery.size ();++i ) for (u32 i = 0; i != Battery.size(); ++i)
{ Battery[i]->drop();
Battery[i].Edit->drop();
Battery[i].Scrollbar->drop();
}
if (ColorRing.Control) if (ColorRing.Control)
ColorRing.Control->drop(); ColorRing.Control->drop();
@ -195,10 +184,10 @@ void CGUIColorSelectDialog::buildColorRing( const core::dimension2d<u32> & dim,
const s32 radiusOut = ( d.Width / 2 ) - 4; const s32 radiusOut = ( d.Width / 2 ) - 4;
const s32 fullR2 = radiusOut * radiusOut; const s32 fullR2 = radiusOut * radiusOut;
video::SColor rgb(0xFF000000); video::SColorf rgb(0,0,0);
video::SColorHSL hsl; video::SColorHSL hsl;
hsl.Luminance = 0.5f; hsl.Luminance = 50;
hsl.Saturation = 1.f; hsl.Saturation = 100;
core::position2d<s32> p; core::position2d<s32> p;
for ( p.Y = -radiusOut; p.Y <= radiusOut; p.Y += 1 ) for ( p.Y = -radiusOut; p.Y <= radiusOut; p.Y += 1 )
@ -221,68 +210,58 @@ void CGUIColorSelectDialog::buildColorRing( const core::dimension2d<u32> & dim,
// normalize, dotproduct = xnorm // normalize, dotproduct = xnorm
const f32 xn = r == 0.f ? 0.f : -p.X * core::reciprocal(r); const f32 xn = r == 0.f ? 0.f : -p.X * core::reciprocal(r);
hsl.Hue = acosf(xn); hsl.Hue = acosf(xn)*core::RADTODEG;
if ( p.Y > 0 ) if ( p.Y > 0 )
hsl.Hue = (2.f * core::PI ) - hsl.Hue; hsl.Hue = 360 - hsl.Hue;
hsl.Hue -= 90;
hsl.Hue -= core::PI / 2.f;
const f32 rTest = r / radiusOut; const f32 rTest = r / radiusOut;
/* #if 0
if ( rTest < 0.25f ) if (rTest < 0.33f)
{ {
hsl.Luminance = rTest / 0.25f; // luminance from 0 to 50
hsl.Luminance = 50*(rTest/0.33);
hsl.Saturation = 0.f; hsl.Saturation = 0.f;
hsl.toRGB ( rgb );
*dst = rgb.color;
}
else
if ( rTest < 0.4f )
{
hsl.Saturation = ( rTest - 0.25f ) / 0.15f;
hsl.Luminance = 1.f - ( hsl.Saturation / 2.4f );
hsl.Luminance = 0.5f;
hsl.toRGB ( rgb );
// *dst = rgb.color;
}
else
if ( rTest < 0.75f )
{
hsl.Luminance = 0.5f;
hsl.Saturation = 1.f;
hsl.toRGB ( rgb );
*dst = rgb.color;
}
else
if ( rTest < 0.98f )
{
hsl.Luminance = 0.5f - ( ( rTest - 0.75f ) / 0.75f );
hsl.Saturation = 1.f;
hsl.toRGB ( rgb );
*dst = rgb.color;
}
*/
if ( rTest >= 0.5f )
{
hsl.Luminance = 0.5f;
hsl.Saturation = 1.f;
hsl.toRGB(rgb); hsl.toRGB(rgb);
if ( rTest <= 0.55f )
{
const u32 alpha = (u32) ( (rTest - 0.5f ) * ( 255.f / 0.05f ) );
rgb.setAlpha(alpha);
}
else if ( rTest >= 0.95f )
{
const u32 alpha = (u32) ( (rTest - 0.95f ) * ( 255.f / 0.05f ) );
rgb.setAlpha(255-alpha);
}
else
rgb.setAlpha(255);
RawTexture->setPixel(4+p.X+radiusOut, 4+p.Y+radiusOut, rgb);
} }
else
if ( rTest < 0.66f )
{
// saturation from 0 to 100
hsl.Saturation = 100*(( rTest - 0.33f ) / 0.33f);
hsl.Luminance = 50;
hsl.toRGB(rgb);
}
else
{
// luminance from 50 to 100
hsl.Luminance = 100*(0.5f + ( ( rTest - 0.66f ) / .66f ));
hsl.Saturation = 100;
hsl.toRGB(rgb);
}
// borders should be slightly transparent
if ( rTest >= 0.95f )
rgb.a = (1.f-rTest)*20;
else
rgb.a=1.f;
#else
if ( rTest > 0.5f )
{
hsl.Saturation = 100;
hsl.Luminance = 50;
hsl.toRGB(rgb);
}
// borders should be slightly transparent
if ( rTest < 0.5f )
rgb.a = 0;
else if ( rTest >= 0.95f )
rgb.a = (1.f-rTest)*20;
else if ( rTest <= 0.55f )
rgb.a = (rTest-0.5f)*20;
else
rgb.a=1.f;
#endif
RawTexture->setPixel(4+p.X+radiusOut, 4+p.Y+radiusOut, rgb.toSColor());
} }
} }
} }
@ -319,43 +298,63 @@ bool CGUIColorSelectDialog::OnEvent(const SEvent& event)
case EET_GUI_EVENT: case EET_GUI_EVENT:
switch(event.GUIEvent.EventType) switch(event.GUIEvent.EventType)
{ {
case EGET_SCROLL_BAR_CHANGED: case EGET_SPINBOX_CHANGED:
{ {
for ( u32 i = 0; i!= Battery.size (); ++i ) for ( u32 i = 0; i!= Battery.size (); ++i )
{ {
if ( event.GUIEvent.Caller == Battery[i].Scrollbar ) if ( event.GUIEvent.Caller == Battery[i] )
{ {
const s32 value = Template[i].range_down + Battery[i].Scrollbar->getPos(); if (i<4)
Battery[i].Edit->setText(core::stringw(value).c_str()); {
video::SColor rgb((u32)Battery[0]->getValue(), (u32)Battery[1]->getValue(),
(u32)Battery[2]->getValue(), (u32)Battery[3]->getValue());
video::SColorHSL hsl;
video::SColorf rgb2(rgb);
hsl.fromRGB(rgb2);
Battery[4]->setValue(hsl.Hue);
Battery[5]->setValue(hsl.Saturation);
Battery[6]->setValue(hsl.Luminance);
}
else
{
video::SColorHSL hsl(Battery[4]->getValue(), Battery[5]->getValue(),
Battery[6]->getValue());
video::SColorf rgb2;
hsl.toRGB(rgb2);
video::SColor rgb = rgb2.toSColor();
Battery[1]->setValue((f32)rgb.getRed());
Battery[2]->setValue((f32)rgb.getGreen());
Battery[3]->setValue((f32)rgb.getBlue());
}
} }
} }
return true; return true;
} }
case EGET_ELEMENT_FOCUS_LOST: case EGET_ELEMENT_FOCUS_LOST:
Dragging = false; Dragging = false;
break; break;
case EGET_BUTTON_CLICKED: case EGET_BUTTON_CLICKED:
if (event.GUIEvent.Caller == CloseButton || if (event.GUIEvent.Caller == CloseButton ||
event.GUIEvent.Caller == CancelButton) event.GUIEvent.Caller == CancelButton)
{ {
sendCancelEvent(); sendCancelEvent();
remove(); remove();
return true; return true;
} }
else else
if (event.GUIEvent.Caller == OKButton) if (event.GUIEvent.Caller == OKButton)
{ {
sendSelectedEvent(); sendSelectedEvent();
remove(); remove();
return true; return true;
} }
break; break;
case EGET_LISTBOX_CHANGED: case EGET_LISTBOX_CHANGED:
case EGET_LISTBOX_SELECTED_AGAIN: case EGET_LISTBOX_SELECTED_AGAIN:
default: default:
break; break;
} }
break; break;
case EET_MOUSE_INPUT_EVENT: case EET_MOUSE_INPUT_EVENT:
@ -420,15 +419,41 @@ void CGUIColorSelectDialog::draw()
font->draw(Text.c_str(), rect, skin->getColor(EGDC_ACTIVE_CAPTION), false, true, font->draw(Text.c_str(), rect, skin->getColor(EGDC_ACTIVE_CAPTION), false, true,
&AbsoluteClippingRect); &AbsoluteClippingRect);
} }
IGUIFont* font = Environment->getBuiltInFont();
if (font)
font->draw(L"+", core::rect<s32>(20,20,50,50), video::SColor(), false, false,
&AbsoluteClippingRect);
IGUIElement::draw(); IGUIElement::draw();
// draw color selector after the window elements
core::vector2di pos(ColorRing.Control->getAbsolutePosition().UpperLeftCorner);
pos.X += ColorRing.Texture->getOriginalSize().Width/2;
pos.Y += ColorRing.Texture->getOriginalSize().Height/2;
#if 0
const f32 h = Battery[4]->getValue();
const f32 s = Battery[5]->getValue();
const f32 l = Battery[6]->getValue();
const f32 factor = 58.f*(((s==0)&&(l<50))?(l*0.33f/50):(
(s<100)?((.33f+(s*0.33f/100))):((0.66f+(l-50)*0.33f/50))));
#else
const f32 factor = 44;
#endif
pos.X += core::round32(sinf(Battery[4]->getValue()*core::DEGTORAD)*factor);
pos.Y -= core::round32(cosf(Battery[4]->getValue()*core::DEGTORAD)*factor);
Environment->getVideoDriver()->draw2DPolygon(pos, 4, 0xffffffff, 4);
} }
video::SColor CGUIColorSelectDialog::getColor()
{
return video::SColor((u32)Battery[0]->getValue(), (u32)Battery[1]->getValue(),
(u32)Battery[2]->getValue(), (u32)Battery[3]->getValue());
}
video::SColorHSL CGUIColorSelectDialog::getColorHSL()
{
return video::SColorHSL(Battery[4]->getValue(), Battery[5]->getValue(),
Battery[6]->getValue());
}
//! sends the event that the file has been selected. //! sends the event that the file has been selected.
void CGUIColorSelectDialog::sendSelectedEvent() void CGUIColorSelectDialog::sendSelectedEvent()
{ {
@ -457,4 +482,3 @@ void CGUIColorSelectDialog::sendCancelEvent()
} // end namespace irr } // end namespace irr
#endif // _IRR_COMPILE_WITH_GUI_ #endif // _IRR_COMPILE_WITH_GUI_

View File

@ -10,8 +10,7 @@
#include "IGUIColorSelectDialog.h" #include "IGUIColorSelectDialog.h"
#include "IGUIButton.h" #include "IGUIButton.h"
#include "IGUIEditBox.h" #include "IGUISpinBox.h"
#include "IGUIScrollBar.h"
#include "IGUIImage.h" #include "IGUIImage.h"
#include "irrArray.h" #include "irrArray.h"
@ -37,6 +36,9 @@ namespace gui
//! draws the element and its children //! draws the element and its children
virtual void draw(); virtual void draw();
virtual video::SColor getColor();
virtual video::SColorHSL getColorHSL();
private: private:
//! sends the event that the file has been selected. //! sends the event that the file has been selected.
@ -51,14 +53,7 @@ namespace gui
IGUIButton* OKButton; IGUIButton* OKButton;
IGUIButton* CancelButton; IGUIButton* CancelButton;
struct SBatteryItem core::array<IGUISpinBox*> Battery;
{
f32 Incoming;
f32 Outgoing;
IGUIEditBox * Edit;
IGUIScrollBar *Scrollbar;
};
core::array< SBatteryItem > Battery;
struct SColorCircle struct SColorCircle
{ {
@ -77,4 +72,3 @@ namespace gui
#endif // _IRR_COMPILE_WITH_GUI_ #endif // _IRR_COMPILE_WITH_GUI_
#endif // __C_GUI_COLOR_SELECT_DIALOG_H_INCLUDED__ #endif // __C_GUI_COLOR_SELECT_DIALOG_H_INCLUDED__