Back to index

nux  3.0.0
RGBValuator.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "Nux.h"
00024 #include "NuxGraphics/GLTextureResourceManager.h"
00025 #include "HLayout.h"
00026 #include "VLayout.h"
00027 #include "EditTextBox.h"
00028 #include "ToggleButton.h"
00029 #include "HexRegExpValidator.h"
00030 #include "IntegerValidator.h"
00031 #include "DoubleValidator.h"
00032 #include "RGBValuator.h"
00033 
00034 namespace nux
00035 {
00036   NUX_IMPLEMENT_OBJECT_TYPE(RGBValuator);
00037 
00038   RGBValuator::RGBValuator(NUX_FILE_LINE_DECL)
00039     : View(NUX_FILE_LINE_PARAM)
00040     , rgb_(1, 1, 1)
00041     , hsv_(rgb_)
00042     , hls_(rgb_)
00043     , m_color_model(color::RGB)
00044     , m_color_format(color::FLOAT)
00045   {
00046     InitializeLayout();
00047     SetColorModel(m_color_model);
00048     SetColorFormat(m_color_format);
00049     SetRGBA(1.0f, 1.0f, 1.0f, 1.0f);
00050     InitializeWidgets();
00051   }
00052 
00053   RGBValuator::RGBValuator(Color const& color, NUX_FILE_LINE_DECL)
00054     : View(NUX_FILE_LINE_PARAM)
00055     , rgb_(1, 1, 1)
00056     , hsv_(rgb_)
00057     , hls_(rgb_)
00058     , m_color_model(color::RGB)
00059     , m_color_format(color::FLOAT)
00060   {
00061     InitializeLayout();
00062     SetColorModel(m_color_model);
00063     SetColorFormat(m_color_format);
00064     SetRGBA(color);
00065     InitializeWidgets();
00066   }
00067 
00068   RGBValuator::RGBValuator(color::Model colorModel, float x, float y, float z, float alpha,    NUX_FILE_LINE_DECL)
00069     : View(NUX_FILE_LINE_PARAM)
00070     , rgb_(1, 1, 1)
00071     , hsv_(rgb_)
00072     , hls_(rgb_)
00073     , m_color_model(colorModel)
00074     , m_color_format(color::FLOAT)
00075   {
00076     InitializeLayout();
00077 
00078     switch(m_color_model)
00079     {
00080       case color::HSV:
00081       {
00082         SetColorModel(color::HSV);
00083         SetHSV(x, y, z);
00084         SetAlpha(alpha);
00085       }
00086       case color::HLS:
00087       {
00088         SetColorModel(color::HLS);
00089         SetHLS(x, y, z);
00090         SetAlpha(alpha);
00091       }
00092       default:
00093       case color::RGB:
00094       {
00095         SetColorModel(color::RGB);
00096         SetRGBA(x, y, z, alpha);
00097       }
00098     }
00099 
00100     SetColorFormat(m_color_format);
00101 
00102     InitializeWidgets();
00103 
00104   }
00105   void RGBValuator::InitializeLayout()
00106   {
00107     hlayout             = new HLayout("RGBValuatorLayout", NUX_TRACKER_LOCATION);
00108     redlayout           = new HLayout("RedLayout", NUX_TRACKER_LOCATION);
00109     greenlayout         = new HLayout("GreenLayout", NUX_TRACKER_LOCATION);
00110     bluelayout          = new HLayout("BlueLayout", NUX_TRACKER_LOCATION);
00111     alphalayout         = new HLayout("AlphaLayout", NUX_TRACKER_LOCATION);
00112     vlayout             = new VLayout("RGBVLayout", NUX_TRACKER_LOCATION);
00113     colormodel_layout   = new VLayout("ColorModel", NUX_TRACKER_LOCATION);
00114 
00115     m_ColorModel        = new ToggleButton();
00116     m_ColorFormat       = new ToggleButton();
00117 
00118     red_caption_        = new EditTextBox("", NUX_TRACKER_LOCATION);
00119     green_caption_      = new EditTextBox("", NUX_TRACKER_LOCATION);
00120     blue_caption_       = new EditTextBox("", NUX_TRACKER_LOCATION);
00121     alpha_caption_      = new EditTextBox("", NUX_TRACKER_LOCATION);
00122 
00123     red_valuator_       = new InputArea(NUX_TRACKER_LOCATION);
00124     green_valuator_     = new InputArea(NUX_TRACKER_LOCATION);
00125     blue_valuator_      = new InputArea(NUX_TRACKER_LOCATION);
00126     alpha_valuator_     = new InputArea(NUX_TRACKER_LOCATION);
00127     color_square_       = new InputArea(NUX_TRACKER_LOCATION);
00128 
00129     m_ComponentLabel0   = new InputArea(NUX_TRACKER_LOCATION);
00130     m_ComponentLabel1   = new InputArea(NUX_TRACKER_LOCATION);
00131     m_ComponentLabel2   = new InputArea(NUX_TRACKER_LOCATION);
00132     m_ComponentAlpha    = new InputArea(NUX_TRACKER_LOCATION);
00133   }
00134 
00135   void RGBValuator::InitializeWidgets()
00136   {
00137     m_HexRegExp.SetMaximum(255);
00138     m_HexRegExp.SetMinimum(0);
00139     m_IntRegExp.SetMaximum(255);
00140     m_IntRegExp.SetMinimum(0);
00141     m_DoubleRegExp.SetMaximum(1.0);
00142     m_DoubleRegExp.SetMinimum(0);
00143 
00144     // Set Signals
00145     red_valuator_->mouse_down.connect(sigc::mem_fun(this, &RGBValuator::OnReceiveMouseDown_Red));
00146     green_valuator_->mouse_down.connect(sigc::mem_fun(this, &RGBValuator::OnReceiveMouseDown_Green));
00147     blue_valuator_->mouse_down.connect(sigc::mem_fun(this, &RGBValuator::OnReceiveMouseDown_Blue));
00148     alpha_valuator_->mouse_down.connect(sigc::mem_fun(this, &RGBValuator::OnReceiveMouseDown_Alpha));
00149     red_valuator_->mouse_drag.connect(sigc::mem_fun(this, &RGBValuator::OnReceiveMouseDrag_Red));
00150     green_valuator_->mouse_drag.connect(sigc::mem_fun(this, &RGBValuator::OnReceiveMouseDrag_Green));
00151     blue_valuator_->mouse_drag.connect(sigc::mem_fun(this, &RGBValuator::OnReceiveMouseDrag_Blue));
00152     alpha_valuator_->mouse_drag.connect(sigc::mem_fun(this, &RGBValuator::OnReceiveMouseDrag_Alpha));
00153     m_ColorModel->click.connect(sigc::mem_fun(this, &RGBValuator::OnChangeColorModel));
00154     m_ColorFormat->click.connect(sigc::mem_fun(this, &RGBValuator::OnChangeColorFormat));
00155 //    m_ColorModel->mouse_down.connect(sigc::mem_fun(this, &RGBValuator::RecvColorModelEvent));
00156 //    m_ColorModel->mouse_up.connect(sigc::mem_fun(this, &RGBValuator::RecvColorModelEvent));
00157 //    m_ColorModel->mouse_enter.connect(sigc::mem_fun(this, &RGBValuator::RecvColorModelEvent));
00158 
00159 
00160     m_ColorModel->SetFont(GetSysBoldFont());
00161     m_ColorFormat->SetFont(GetSysBoldFont());
00162 
00163     red_valuator_->mouse_up.connect(sigc::mem_fun(this, &RGBValuator::OnReceiveMouseUp_Red));
00164     green_valuator_->mouse_up.connect(sigc::mem_fun(this, &RGBValuator::OnReceiveMouseUp_Green));
00165     blue_valuator_->mouse_up.connect(sigc::mem_fun(this, &RGBValuator::OnReceiveMouseUp_Blue));
00166 
00167     red_caption_->sigValidateKeyboardEntry.connect(sigc::bind(sigc::mem_fun(this, &RGBValuator::OnComponentInput), 0));
00168     green_caption_->sigValidateKeyboardEntry.connect(sigc::bind(sigc::mem_fun(this, &RGBValuator::OnComponentInput), 1));
00169     blue_caption_->sigValidateKeyboardEntry.connect(sigc::bind(sigc::mem_fun(this, &RGBValuator::OnComponentInput), 2));
00170     alpha_caption_->sigValidateKeyboardEntry.connect(sigc::bind(sigc::mem_fun(this, &RGBValuator::OnComponentInput), 3));
00171 
00172     // Set Geometry
00173 
00174     red_caption_->SetGeometry(Geometry(0, 0, 42, DEFAULT_WIDGET_HEIGHT));
00175     red_caption_->SetMaximumHeight(15);
00176 
00177     green_caption_->SetGeometry(Geometry(0, 0, 42, DEFAULT_WIDGET_HEIGHT));
00178     green_caption_->SetMaximumHeight(15);
00179 
00180     blue_caption_->SetGeometry(Geometry(0, 0, 42, DEFAULT_WIDGET_HEIGHT));
00181     blue_caption_->SetMaximumHeight(15);
00182 
00183     alpha_caption_->SetGeometry(Geometry(0, 0, 42, DEFAULT_WIDGET_HEIGHT));
00184     alpha_caption_->SetMaximumHeight(15);
00185 
00186     m_ComponentLabel0->SetMinimumSize(10, DEFAULT_WIDGET_HEIGHT);
00187     m_ComponentLabel0->SetMaximumHeight(15);
00188     //m_ComponentLabel0->SetGeometry(Geometry(0, 0, 15, DEFAULT_WIDGET_HEIGHT));
00189 
00190     m_ComponentLabel1->SetMinimumSize(10, DEFAULT_WIDGET_HEIGHT);
00191     m_ComponentLabel1->SetMaximumHeight(15);
00192     //m_ComponentLabel1->SetGeometry(Geometry(0, 0, 15, DEFAULT_WIDGET_HEIGHT));
00193 
00194     m_ComponentLabel2->SetMinimumSize(10, DEFAULT_WIDGET_HEIGHT);
00195     m_ComponentLabel2->SetMaximumHeight(15);
00196     //m_ComponentLabel2->SetGeometry(Geometry(0, 0, 15, DEFAULT_WIDGET_HEIGHT));
00197 
00198     m_ComponentAlpha->SetMinimumSize(10, DEFAULT_WIDGET_HEIGHT);
00199     m_ComponentAlpha->SetMaximumHeight(15);
00200     //m_ComponentAlpha->SetGeometry(Geometry(0, 0, 15, DEFAULT_WIDGET_HEIGHT));
00201 
00202     red_valuator_->SetMinimumSize(3 * DEFAULT_WIDGET_WIDTH, 15);
00203     red_valuator_->SetMaximumHeight(15);
00204     red_valuator_->SetGeometry(Geometry(0, 0, DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT));
00205 
00206     green_valuator_->SetMinimumSize(3 * DEFAULT_WIDGET_WIDTH, 15);
00207     green_valuator_->SetMaximumHeight(15);
00208     green_valuator_->SetGeometry(Geometry(0, 0, DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT));
00209 
00210     blue_valuator_->SetMinimumSize(3 * DEFAULT_WIDGET_WIDTH, 15);
00211     blue_valuator_->SetMaximumHeight(15);
00212     blue_valuator_->SetGeometry(Geometry(0, 0, DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT));
00213 
00214     alpha_valuator_->SetMinimumSize(3 * DEFAULT_WIDGET_WIDTH, 15);
00215     alpha_valuator_->SetMaximumHeight(15);
00216     alpha_valuator_->SetGeometry(Geometry(0, 0, DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT));
00217 
00218     color_square_->SetMinimumSize(32, 32);
00219     color_square_->SetGeometry(Geometry(0, 0, 32, 32));
00220     m_ColorModel->SetMinimumSize(20, 16);
00221     m_ColorModel->SetLayoutPadding(0, 0, 0, 0);
00222     m_ColorFormat->SetMinimumSize(20, 16);
00223     m_ColorFormat->SetLayoutPadding(0, 0, 0, 0);
00224 
00225     redlayout->AddView(m_ComponentLabel0, 0, MINOR_POSITION_CENTER, MINOR_SIZE_MATCHCONTENT);
00226     redlayout->AddView(red_caption_, 0);
00227     redlayout->AddView(red_valuator_, 1);
00228     redlayout->SetSpaceBetweenChildren(4);
00229 
00230     greenlayout->AddView(m_ComponentLabel1, 0, MINOR_POSITION_CENTER, MINOR_SIZE_MATCHCONTENT);
00231     greenlayout->AddView(green_caption_, 0);
00232     greenlayout->AddView(green_valuator_, 1);
00233     greenlayout->SetSpaceBetweenChildren(4);
00234 
00235     bluelayout->AddView(m_ComponentLabel2, 0, MINOR_POSITION_CENTER, MINOR_SIZE_MATCHCONTENT);
00236     bluelayout->AddView(blue_caption_, 0);
00237     bluelayout->AddView(blue_valuator_, 1);
00238     bluelayout->SetSpaceBetweenChildren(4);
00239 
00240     alphalayout->AddView(m_ComponentAlpha, 0, MINOR_POSITION_CENTER, MINOR_SIZE_MATCHCONTENT);
00241     alphalayout->AddView(alpha_caption_, 0);
00242     alphalayout->AddView(alpha_valuator_, 1);
00243     alphalayout->SetSpaceBetweenChildren(4);
00244 
00245     // Set layout
00246     colormodel_layout->AddView(color_square_, 0, eCenter, eFix);
00247     colormodel_layout->AddView(m_ColorModel, 0, eCenter, eFull);
00248     colormodel_layout->AddView(m_ColorFormat, 0, eCenter, eFull);
00249     colormodel_layout->SetSpaceBetweenChildren(2);
00250 
00251     hlayout->AddView(colormodel_layout, 0, MINOR_POSITION_START, MINOR_SIZE_MATCHCONTENT);
00252 
00253     vlayout->AddLayout(redlayout, 0);
00254     vlayout->AddLayout(greenlayout, 0);
00255     vlayout->AddLayout(bluelayout, 0);
00256     vlayout->AddLayout(alphalayout, 0);
00257     vlayout->SetSpaceBetweenChildren(2);
00258     vlayout->SetTopAndBottomPadding(4);
00259     vlayout->SetLeftAndRightPadding(4);
00260     vlayout->SetContentDistribution(MAJOR_POSITION_CENTER);
00261 
00262 
00263     hlayout->AddLayout(vlayout, 1, MINOR_POSITION_CENTER, MINOR_SIZE_FULL);
00264     hlayout->SetSpaceBetweenChildren(2);
00265     hlayout->SetLeftAndRightPadding(6, 6);
00266     hlayout->SetTopAndBottomPadding(0);
00267 
00268     SetCompositionLayout(hlayout);
00269 
00270     NTextureData image;
00271     MakeCheckBoardImage(image.GetSurface(0), 64, 64, Color(0xff000000), Color(0xff323232), 4, 4);
00272     BaseTexture* CheckboardPattern = GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableTexture();
00273     CheckboardPattern->Update(&image);
00274 
00275     TexCoordXForm texxform;
00276     texxform.SetTexCoordType(TexCoordXForm::OFFSET_COORD);
00277     texxform.SetWrap(TEXWRAP_REPEAT, TEXWRAP_REPEAT);
00278     m_CheckboardLayer = new TextureLayer(CheckboardPattern->GetDeviceTexture(), texxform, color::White);
00279 
00280     CheckboardPattern->UnReference();
00281   }
00282 
00283   RGBValuator::~RGBValuator()
00284   {
00285     NUX_SAFE_DELETE(m_CheckboardLayer);
00286   }
00287 
00288   void RGBValuator::DrawRedMarker(GraphicsEngine &graphics_engine)
00289   {
00290     int marker_position_x;
00291     int marker_position_y;
00292 
00293     float percent = 0.0f;
00294 
00295     if (m_color_model == color::RGB)
00296       percent = rgb_.red;
00297 
00298     if (m_color_model == color::HSV)
00299       percent = hsv_.hue;
00300 
00301     if (m_color_model == color::HLS)
00302       percent = hls_.hue;
00303 
00304     graphics_engine.PushClippingRectangle(red_valuator_->GetGeometry());
00305 
00306     marker_position_x = red_valuator_->GetBaseX() + percent * red_valuator_->GetBaseWidth();
00307     marker_position_y = red_valuator_->GetBaseY() + red_valuator_->GetBaseHeight();
00308     GetPainter().Draw2DTriangleColor(graphics_engine, marker_position_x - 5, marker_position_y,
00309                                   marker_position_x, marker_position_y - 5,
00310                                   marker_position_x + 5, marker_position_y, Color(0xFF000000));
00311 
00312     GetPainter().Draw2DTriangleColor(graphics_engine, marker_position_x - 4, marker_position_y,
00313                                   marker_position_x, marker_position_y - 4,
00314                                   marker_position_x + 4, marker_position_y, Color(0.7f, 0.7f, 0.7f, 1.0f));
00315 
00316     graphics_engine.PopClippingRectangle();
00317   }
00318 
00319   void RGBValuator::DrawGreenMarker(GraphicsEngine &graphics_engine)
00320   {
00321     int marker_position_x;
00322     int marker_position_y;
00323 
00324     float percent = 0.0f;
00325 
00326     if (m_color_model == color::RGB)
00327       percent = rgb_.green;
00328 
00329     if (m_color_model == color::HSV)
00330       percent = hsv_.saturation;
00331 
00332     if (m_color_model == color::HLS)
00333       percent = hls_.lightness;
00334 
00335     graphics_engine.PushClippingRectangle(green_valuator_->GetGeometry());
00336 
00337     marker_position_x = green_valuator_->GetBaseX() + percent * green_valuator_->GetBaseWidth();
00338     marker_position_y = green_valuator_->GetBaseY() + green_valuator_->GetBaseHeight();
00339     GetPainter().Draw2DTriangleColor(graphics_engine, marker_position_x - 5, marker_position_y,
00340                                   marker_position_x, marker_position_y - 5,
00341                                   marker_position_x + 5, marker_position_y, Color(0.0f, 0.0f, 0.0f, 1.0f));
00342 
00343     GetPainter().Draw2DTriangleColor(graphics_engine, marker_position_x - 4, marker_position_y,
00344                                   marker_position_x, marker_position_y - 4,
00345                                   marker_position_x + 4, marker_position_y, Color(0.7f, 0.7f, 0.7f, 1.0f));
00346 
00347     graphics_engine.PopClippingRectangle();
00348   }
00349 
00350   void RGBValuator::DrawBlueMarker(GraphicsEngine &graphics_engine)
00351   {
00352     int marker_position_x;
00353     int marker_position_y;
00354 
00355     float percent = 0.0f;
00356 
00357     if (m_color_model == color::RGB)
00358       percent = rgb_.blue;
00359 
00360     if (m_color_model == color::HSV)
00361       percent = hsv_.value;
00362 
00363     if (m_color_model == color::HLS)
00364       percent = hls_.saturation;
00365 
00366     graphics_engine.PushClippingRectangle(blue_valuator_->GetGeometry());
00367 
00368     marker_position_x = blue_valuator_->GetBaseX() + percent * blue_valuator_->GetBaseWidth();
00369     marker_position_y = blue_valuator_->GetBaseY() + blue_valuator_->GetBaseHeight();
00370     GetPainter().Draw2DTriangleColor(graphics_engine, marker_position_x - 5, marker_position_y,
00371                                   marker_position_x, marker_position_y - 5,
00372                                   marker_position_x + 5, marker_position_y, Color(0.0f, 0.0f, 0.0f, 1.0f));
00373 
00374     GetPainter().Draw2DTriangleColor(graphics_engine, marker_position_x - 4, marker_position_y,
00375                                   marker_position_x, marker_position_y - 4,
00376                                   marker_position_x + 4, marker_position_y, Color(0.7f, 0.7f, 0.7f, 1.0f));
00377 
00378     graphics_engine.PopClippingRectangle();
00379   }
00380 
00381   void RGBValuator::DrawAlphaMarker(GraphicsEngine &graphics_engine)
00382   {
00383     int marker_position_x;
00384     int marker_position_y;
00385 
00386     graphics_engine.PushClippingRectangle(alpha_valuator_->GetGeometry());
00387 
00388     marker_position_x = alpha_valuator_->GetBaseX() + alpha_ * alpha_valuator_->GetBaseWidth();
00389     marker_position_y = alpha_valuator_->GetBaseY() + alpha_valuator_->GetBaseHeight();
00390     GetPainter().Draw2DTriangleColor(graphics_engine, marker_position_x - 5, marker_position_y,
00391                                   marker_position_x, marker_position_y - 5,
00392                                   marker_position_x + 5, marker_position_y, Color(0.0f, 0.0f, 0.0f, 1.0f));
00393 
00394     GetPainter().Draw2DTriangleColor(graphics_engine, marker_position_x - 4, marker_position_y,
00395                                   marker_position_x, marker_position_y - 4,
00396                                   marker_position_x + 4, marker_position_y, Color(0.7f, 0.7f, 0.7f, 1.0f));
00397 
00398     graphics_engine.PopClippingRectangle();
00399   }
00400 
00401   void RGBValuator::Draw(GraphicsEngine &graphics_engine, bool force_draw)
00402   {
00403     Geometry base = GetGeometry();
00404 
00405     graphics_engine.PushClippingRectangle(base);
00406     GetPainter().PushDrawShapeLayer(graphics_engine, vlayout->GetGeometry(), eSHAPE_CORNER_ROUND4, Color(0xFF000000), eAllCorners);
00407 
00408     if (m_color_model == color::RGB)
00409     {
00410       DrawRGB(graphics_engine);
00411     }
00412     else if (m_color_model == color::HSV)
00413     {
00414       DrawHSV(graphics_engine);
00415     }
00416     else if (m_color_model == color::HLS)
00417     {
00418       DrawHLS(graphics_engine);
00419     }
00420 
00421     GetPainter().PaintTextLineStatic(graphics_engine, GetSysBoldFont(), m_ComponentLabel0->GetGeometry(), m_ComponentLabel0->GetBaseString().GetTCharPtr(), Color(0xFFFFFFFF));
00422     GetPainter().PaintTextLineStatic(graphics_engine, GetSysBoldFont(), m_ComponentLabel1->GetGeometry(), m_ComponentLabel1->GetBaseString().GetTCharPtr(), Color(0xFFFFFFFF));
00423     GetPainter().PaintTextLineStatic(graphics_engine, GetSysBoldFont(), m_ComponentLabel2->GetGeometry(), m_ComponentLabel2->GetBaseString().GetTCharPtr(), Color(0xFFFFFFFF));
00424     GetPainter().PaintTextLineStatic(graphics_engine, GetSysBoldFont(), m_ComponentAlpha->GetGeometry(), m_ComponentAlpha->GetBaseString().GetTCharPtr(), Color(0xFFFFFFFF));
00425 
00426     DrawRedMarker(graphics_engine);
00427     DrawGreenMarker(graphics_engine);
00428     DrawBlueMarker(graphics_engine);
00429     DrawAlphaMarker(graphics_engine);
00430 
00431     red_caption_->QueueDraw();
00432     green_caption_->QueueDraw();
00433     blue_caption_->QueueDraw();
00434     alpha_caption_->QueueDraw();
00435     m_ColorModel->QueueDraw();
00436     m_ColorFormat->QueueDraw();
00437 
00438     GetPainter().PopBackground();
00439     graphics_engine.PopClippingRectangle();
00440   }
00441 
00442   void RGBValuator::DrawContent(GraphicsEngine &graphics_engine, bool force_draw)
00443   {
00444     Geometry base = GetGeometry();
00445     graphics_engine.PushClippingRectangle(base);
00446 
00447     red_caption_->ProcessDraw(graphics_engine, force_draw);
00448     green_caption_->ProcessDraw(graphics_engine, force_draw);
00449     blue_caption_->ProcessDraw(graphics_engine, force_draw);
00450     alpha_caption_->ProcessDraw(graphics_engine, force_draw);
00451 
00452     m_ColorModel->ProcessDraw(graphics_engine, force_draw); // the button has round corner. That is why we need to push the background.
00453     m_ColorFormat->ProcessDraw(graphics_engine, force_draw); // the button has round corner. That is why we need to push the background.
00454     graphics_engine.PopClippingRectangle();
00455   }
00456 
00457   void RGBValuator::DrawRGB(GraphicsEngine &graphics_engine)
00458   {
00459     // Red
00460     Geometry P = red_valuator_->GetGeometry();
00461     GetPainter().Paint2DQuadColor(graphics_engine, P, Color(0.0f, rgb_.green, rgb_.blue), Color(0.0f, rgb_.green, rgb_.blue),
00462                                Color(1.0f, rgb_.green, rgb_.blue), Color(1.0f, rgb_.green, rgb_.blue));
00463 
00464     // Green
00465     P = green_valuator_->GetGeometry();
00466     GetPainter().Paint2DQuadColor(graphics_engine, P, Color(rgb_.red, 0.0f, rgb_.blue), Color(rgb_.red, 0.0f, rgb_.blue),
00467                                Color(rgb_.red, 1.0f, rgb_.blue), Color(rgb_.red, 1.0f, rgb_.blue));
00468 
00469     // Blue
00470     P = blue_valuator_->GetGeometry();
00471     GetPainter().Paint2DQuadColor(graphics_engine, P, Color(rgb_.red, rgb_.green, 0.0f), Color(rgb_.red, rgb_.green, 0.0f),
00472                                Color(rgb_.red, rgb_.green, 1.0f), Color(rgb_.red, rgb_.green, 1.0f));
00473 
00474     // Alpha
00475     P = alpha_valuator_->GetGeometry();
00476     m_CheckboardLayer->SetGeometry(P);
00477     m_CheckboardLayer->Renderlayer(graphics_engine);
00478 
00479     graphics_engine.GetRenderStates().SetBlend(true, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00480     GetPainter().Paint2DQuadColor(graphics_engine, P, Color(0.0f, 0.0f, 0.0f, 0.0f), Color(0.0f, 0.0f, 0.0f, 0.0f),
00481                                Color(rgb_.red, rgb_.green, rgb_.blue, 1.0f), Color(rgb_.red, rgb_.green, rgb_.blue, 1.0f));
00482     graphics_engine.GetRenderStates().SetBlend(false);
00483 
00484     P = color_square_->GetGeometry();
00485     GetPainter().Paint2DQuadColor(graphics_engine, P, Color(rgb_.red, rgb_.green, rgb_.blue), Color(rgb_.red, rgb_.green, rgb_.blue),
00486                                Color(rgb_.red, rgb_.green, rgb_.blue), Color(rgb_.red, rgb_.green, rgb_.blue));
00487 
00488     m_ColorModel->ProcessDraw(graphics_engine, true);
00489     m_ColorFormat->ProcessDraw(graphics_engine, true);
00490   }
00491 
00492   void RGBValuator::DrawHSV(GraphicsEngine &graphics_engine)
00493   {
00494     // Red
00495     Geometry P = red_valuator_->GetGeometry();
00496     float s = 0; //XSI: 1.0f - hsv_.saturation;
00497     float v = 1; //XSI: hsv_.value;
00498     float fw = P.GetWidth() / 6;
00499 
00500     Geometry p = Geometry(P.x, P.y, fw, P.GetHeight());
00501     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(1.0f * v, s * v, s * v), Color(1.0f * v, s * v, s * v), Color(1.0f * v, 1.0f * v, s * v), Color(1.0f * v, 1.0f * v, s * v));
00502     p.SetX(P.x + fw);
00503     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(1.0f * v, 1.0f * v, s * v), Color(1.0f * v, 1.0f * v, s * v), Color(s * v, 1.0f * v, s * v), Color(s * v, 1.0f * v, s * v));
00504 
00505     p.SetX(P.x + 2 * fw);
00506     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(s * v, 1.0f * v, s * v), Color(s * v, 1.0f * v, s * v), Color(s * v, 1.0f * v, 1.0f * v), Color(s * v, 1.0f * v, 1.0f * v));
00507     p.SetX(P.x + 3 * fw);
00508     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(s * v, 1.0f * v, 1.0f * v), Color(s * v, 1.0f * v, 1.0f * v), Color(s * v, s * v, 1.0f * v), Color(s * v, s * v, 1.0f * v));
00509 
00510     p.SetX(P.x + 4 * fw);
00511     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(s * v, s * v, 1.0f * v), Color(s * v, s * v, 1.0f * v), Color(1.0f * v, s * v, 1.0f * v), Color(1.0f * v, s * v, 1.0f * v));
00512     p.SetX(P.x + 5 * fw);
00513     p.SetWidth(P.GetWidth() - 5 * fw); // correct rounding errors
00514     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(1.0f * v, s * v, 1.0f * v), Color(1.0f * v, s * v, 1.0f * v), Color(1.0f * v, s * v, s * v), Color(1.0f * v, s * v, s * v));
00515 
00516     s = 1.0f - hsv_.saturation;
00517     v = hsv_.value;
00518 
00519     float hue = hsv_.hue;
00520     if (hue == 1.0f)
00521       hue = 0.0f;
00522 
00523     color::RedGreenBlue rgb(color::HueSaturationValue(hue, 1, 1));
00524     Color value_gray(v, v, v);
00525     Color value_color(Color(rgb) * v);
00526     // Green
00527     P = green_valuator_->GetGeometry();
00528     GetPainter().Paint2DQuadColor(graphics_engine, P, value_gray, value_gray, value_color, value_color);
00529 
00530     rgb = color::RedGreenBlue(color::HueSaturationValue(hue, hsv_.saturation, 1));
00531     // Blue
00532     P = blue_valuator_->GetGeometry();
00533     GetPainter().Paint2DQuadColor(graphics_engine, P, color::Black, color::Black, Color(rgb), Color(rgb));
00534 
00535     rgb = color::RedGreenBlue(color::HueSaturationValue(hue, hsv_.saturation, hsv_.value));
00536 
00537     // Alpha
00538     P = alpha_valuator_->GetGeometry();
00539     m_CheckboardLayer->SetGeometry(P);
00540     m_CheckboardLayer->Renderlayer(graphics_engine);
00541 
00542     graphics_engine.GetRenderStates().SetBlend(true, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00543     GetPainter().Paint2DQuadColor(graphics_engine, P, Color(0.0f, 0.0f, 0.0f, 0.0f), Color(0.0f, 0.0f, 0.0f, 0.0f), Color(rgb), Color(rgb));
00544     graphics_engine.GetRenderStates().SetBlend(false);
00545 
00546     P = color_square_->GetGeometry();
00547     GetPainter().Paint2DQuadColor(graphics_engine, P, Color(rgb));
00548 
00549     m_ColorModel->ProcessDraw(graphics_engine, true);
00550     m_ColorFormat->ProcessDraw(graphics_engine, true);
00551   }
00552 
00553   void RGBValuator::DrawHLS(GraphicsEngine &graphics_engine)
00554   {
00555     // Red
00556     Geometry P = red_valuator_->GetGeometry();
00557     float s = 0; //XSI: 1.0f - hls_.saturation;
00558     float l = 1; //XSI: hls_.lightness;
00559     float fw = P.GetWidth() / 6;
00560 
00561     Geometry p = Geometry(P.x, P.y, fw, P.GetHeight());
00562     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(1.0f * l, s * l, s * l), Color(1.0f * l, s * l, s * l), Color(1.0f * l, 1.0f * l, s * l), Color(1.0f * l, 1.0f * l, s * l));
00563     p.SetX(P.x + fw);
00564     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(1.0f * l, 1.0f * l, s * l), Color(1.0f * l, 1.0f * l, s * l), Color(s * l, 1.0f * l, s * l), Color(s * l, 1.0f * l, s * l));
00565 
00566     p.SetX(P.x + 2 * fw);
00567     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(s * l, 1.0f * l, s * l), Color(s * l, 1.0f * l, s * l), Color(s * l, 1.0f * l, 1.0f * l), Color(s * l, 1.0f * l, 1.0f * l));
00568     p.SetX(P.x + 3 * fw);
00569     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(s * l, 1.0f * l, 1.0f * l), Color(s * l, 1.0f * l, 1.0f * l), Color(s * l, s * l, 1.0f * l), Color(s * l, s * l, 1.0f * l));
00570 
00571     p.SetX(P.x + 4 * fw);
00572     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(s * l, s * l, 1.0f * l), Color(s * l, s * l, 1.0f * l), Color(1.0f * l, s * l, 1.0f * l), Color(1.0f * l, s * l, 1.0f * l));
00573     p.SetX(P.x + 5 * fw);
00574     p.SetWidth(P.GetWidth() - 5 * fw); // correct rounding errors
00575     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(1.0f * l, s * l, 1.0f * l), Color(1.0f * l, s * l, 1.0f * l), Color(1.0f * l, s * l, s * l), Color(1.0f * l, s * l, s * l));
00576 
00577     s = 1.0f - hls_.saturation;
00578     l = hls_.lightness;
00579 
00580     float Hue;
00581 
00582     if (hls_.hue == 1.0f)
00583       Hue = 0.0f;
00584     else
00585       Hue = hls_.hue;
00586 
00587     // TODO: Tim Penhey 2011-05-13
00588     // refactor this code to use the same methods as the RGB(A)Property classes.
00589     color::RedGreenBlue rgb(color::HueSaturationValue(Hue, 1, 1));
00590     // Need to use HSVtoRGB to compute the color
00591     float r = rgb.red;
00592     float g = rgb.green;
00593     float b = rgb.blue;
00594 
00595     // Green
00596     P = green_valuator_->GetGeometry();
00597     fw = P.GetWidth() / 2;
00598     p = Geometry(P.x, P.y, fw, P.GetHeight());
00599     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(0.0f, 0.0f, 0.0f), Color(0.0f, 0.0f, 0.0f), Color(r* (1 - s) + 0.5f * s, g* (1 - s) + 0.5f * s, b* (1 - s) + 0.5f * s), Color(r* (1 - s) + 0.5f * s, g* (1 - s) + 0.5f * s, b* (1 - s) + 0.5f * s));
00600     p.SetX(P.x + fw);
00601     GetPainter().Paint2DQuadColor(graphics_engine, p, Color(r* (1 - s) + 0.5f * s, g* (1 - s) + 0.5f * s, b* (1 - s) + 0.5f * s), Color(r* (1 - s) + 0.5f * s, g* (1 - s) + 0.5f * s, b* (1 - s) + 0.5f * s), Color(1.0f, 1.0f, 1.0f), Color(1.0f, 1.0f, 1.0f));
00602 
00603 
00604     //HLStoRGB(r, g, b, Hue, hsv_.saturation, 1.0f);
00605     // Blue
00606     float cr, cg, cb;
00607 
00608     if (l > 0.5)
00609     {
00610       float factor = (l - 0.5f) / 0.5f;
00611       cr = (1 - factor) * r * (1 - s) + 0.5 * s + factor * 1.0f;
00612       cg = (1 - factor) * g * (1 - s) + 0.5 * s + factor * 1.0f;
00613       cb = (1 - factor) * b * (1 - s) + 0.5 * s + factor * 1.0f;
00614     }
00615     else
00616     {
00617       float factor = l / 0.5f;
00618       cr = (factor) * r * (1 - s) + 0.5 * s;
00619       cg = (factor) * g * (1 - s) + 0.5 * s;
00620       cb = (factor) * b * (1 - s) + 0.5 * s;
00621     }
00622 
00623     P = blue_valuator_->GetGeometry();
00624     GetPainter().Paint2DQuadColor(graphics_engine, P, Color(l, l, l), Color(l, l, l), Color(cr, cg, cb), Color(cr, cg, cb));
00625 
00626     // TODO: Tim Penhey 2011-05-13
00627     // Can we just use the rgb_ member variable?  Check later.
00628     rgb = color::RedGreenBlue(hls_);
00629 
00630     // Alpha
00631     P = alpha_valuator_->GetGeometry();
00632     m_CheckboardLayer->SetGeometry(P);
00633     m_CheckboardLayer->Renderlayer(graphics_engine);
00634 
00635     graphics_engine.GetRenderStates().SetBlend(true, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00636     GetPainter().Paint2DQuadColor(graphics_engine, P, Color(0.0f, 0.0f, 0.0f, 0.0f), Color(0.0f, 0.0f, 0.0f, 0.0f), Color(rgb), Color(rgb));
00637     graphics_engine.GetRenderStates().SetBlend(false);
00638 
00639     P = color_square_->GetGeometry();
00640     GetPainter().Paint2DQuadColor(graphics_engine, P, Color(rgb));
00641 
00642     m_ColorModel->ProcessDraw(graphics_engine, true);
00643     m_ColorFormat->ProcessDraw(graphics_engine, true);
00644   }
00645 
00646   void RGBValuator::SetRGBA(Color const& color)
00647   {
00648     SetRGBA(color.red, color.green, color.blue, color.alpha);
00649   }
00650 
00651   void RGBValuator::SetRGBA(float r, float g, float b, float a)
00652   {
00653     SetRGB(r, g, b);
00654     SetAlpha(a);
00655   }
00656 
00657   void RGBValuator::SetRGB(Color const& color)
00658   {
00659     SetRGB(color.red, color.green, color.blue);
00660   }
00661 
00662   void RGBValuator::SetRGB(float r, float g, float b)
00663   {
00664     rgb_.red   = Clamp(r, 0.0f, 1.0f);
00665     rgb_.green = Clamp(g, 0.0f, 1.0f);
00666     rgb_.blue  = Clamp(b, 0.0f, 1.0f);
00667 
00668     bool RedEditSelected = red_caption_->IsTextSelected();
00669     bool GreenEditSelected = green_caption_->IsTextSelected();
00670     bool BlueEditSelected = blue_caption_->IsTextSelected();
00671 
00672     if (m_color_format == color::HEX)
00673     {
00674       red_caption_->SetText(NString::Printf("%x", (int) (rgb_.red * 255)));
00675       green_caption_->SetText(NString::Printf("%x", (int) (rgb_.green * 255)));
00676       blue_caption_->SetText(NString::Printf("%x", (int) (rgb_.blue * 255)));
00677     }
00678     else if (m_color_format == color::INT)
00679     {
00680       red_caption_->SetText(NString::Printf("%d", (int) (rgb_.red * 255)));
00681       green_caption_->SetText(NString::Printf("%d", (int) (rgb_.green * 255)));
00682       blue_caption_->SetText(NString::Printf("%d", (int) (rgb_.blue * 255)));
00683     }
00684     else
00685     {
00686       red_caption_->SetText(NString::Printf("%.3f", rgb_.red));
00687       green_caption_->SetText(NString::Printf("%.3f", rgb_.green));
00688       blue_caption_->SetText(NString::Printf("%.3f", rgb_.blue));
00689     }
00690 
00691     // Restore text selection if necessary.
00692     // This solves a problem when an EditTextBox of the widget gets the focus and has its text selected
00693     // but another who is losing the focus will cause it to lose the text selection by calling SetRGB.
00694     if (RedEditSelected) red_caption_->m_KeyboardHandler.SelectAllText();
00695 
00696     if (GreenEditSelected) green_caption_->m_KeyboardHandler.SelectAllText();
00697 
00698     if (BlueEditSelected) blue_caption_->m_KeyboardHandler.SelectAllText();
00699 
00700     sigColorChanged.emit(rgb_.red, rgb_.green, rgb_.blue, alpha_);
00701   }
00702 
00703   void RGBValuator::SetHSV(float h, float s, float v)
00704   {
00705     hsv_.hue        = Clamp(h, 0.0f, 1.0f);
00706     hsv_.saturation = Clamp(s, 0.0f, 1.0f);
00707     hsv_.value      = Clamp(v, 0.0f, 1.0f);
00708 
00709     bool RedEditSelected = red_caption_->IsTextSelected();
00710     bool GreenEditSelected = green_caption_->IsTextSelected();
00711     bool BlueEditSelected = blue_caption_->IsTextSelected();
00712 
00713     if (m_color_format == color::HEX)
00714     {
00715       red_caption_->SetText(NString::Printf("%x", (int) (hsv_.hue * 255)));
00716       green_caption_->SetText(NString::Printf("%x", (int) (hsv_.saturation * 255)));
00717       blue_caption_->SetText(NString::Printf("%x", (int) (hsv_.value * 255)));
00718     }
00719     else if (m_color_format == color::INT)
00720     {
00721       red_caption_->SetText(NString::Printf("%d", (int) (hsv_.hue * 255)));
00722       green_caption_->SetText(NString::Printf("%d", (int) (hsv_.saturation * 255)));
00723       blue_caption_->SetText(NString::Printf("%d", (int) (hsv_.value * 255)));
00724     }
00725     else
00726     {
00727       red_caption_->SetText(NString::Printf("%.3f", hsv_.hue));
00728       green_caption_->SetText(NString::Printf("%.3f", hsv_.saturation));
00729       blue_caption_->SetText(NString::Printf("%.3f", hsv_.value));
00730     }
00731 
00732     if (hsv_.hue >= 1.0f)
00733     {
00734       hsv_.hue = 1.0f;
00735       // XSI: hsv_.hue = 0.0f;
00736     }
00737 
00738     if (RedEditSelected) red_caption_->m_KeyboardHandler.SelectAllText();
00739 
00740     if (GreenEditSelected) green_caption_->m_KeyboardHandler.SelectAllText();
00741 
00742     if (BlueEditSelected) blue_caption_->m_KeyboardHandler.SelectAllText();
00743 
00744     rgb_ = color::RedGreenBlue(hsv_);
00745     sigColorChanged.emit(rgb_.red, rgb_.green, rgb_.blue, alpha_);
00746   }
00747 
00748   void RGBValuator::SetHLS(float h, float l, float s)
00749   {
00750     hls_.hue        = Clamp(h, 0.0f, 1.0f);
00751     hls_.lightness      = Clamp(l, 0.0f, 1.0f);
00752     hls_.saturation = Clamp(s, 0.0f, 1.0f);
00753 
00754     bool RedEditSelected = red_caption_->IsTextSelected();
00755     bool GreenEditSelected = green_caption_->IsTextSelected();
00756     bool BlueEditSelected = blue_caption_->IsTextSelected();
00757 
00758     if (m_color_format == color::HEX)
00759     {
00760       red_caption_->SetText(NString::Printf("%x", (int) (hls_.hue * 255)));
00761       green_caption_->SetText(NString::Printf("%x", (int) (hls_.lightness * 255)));
00762       blue_caption_->SetText(NString::Printf("%x", (int) (hls_.saturation * 255)));
00763     }
00764     else if (m_color_format == color::INT)
00765     {
00766       red_caption_->SetText(NString::Printf("%d", (int) (hls_.hue * 255)));
00767       green_caption_->SetText(NString::Printf("%d", (int) (hls_.lightness * 255)));
00768       blue_caption_->SetText(NString::Printf("%d", (int) (hls_.saturation * 255)));
00769     }
00770     else
00771     {
00772       red_caption_->SetText(NString::Printf("%.3f", hls_.hue));
00773       green_caption_->SetText(NString::Printf("%.3f", hls_.lightness));
00774       blue_caption_->SetText(NString::Printf("%.3f", hls_.saturation));
00775     }
00776 
00777     if (hls_.hue >= 1.0f)
00778     {
00779       hls_.hue = 1.0f;
00780       // XSI: hls_.hue = 0.0f;
00781     }
00782 
00783     if (RedEditSelected) red_caption_->m_KeyboardHandler.SelectAllText();
00784 
00785     if (GreenEditSelected) green_caption_->m_KeyboardHandler.SelectAllText();
00786 
00787     if (BlueEditSelected) blue_caption_->m_KeyboardHandler.SelectAllText();
00788 
00789     rgb_ = color::RedGreenBlue(hls_);
00790     sigColorChanged.emit(rgb_.red, rgb_.green, rgb_.blue, alpha_);
00791   }
00792 
00793   void RGBValuator::SetAlpha(float alpha)
00794   {
00795     alpha_ = Clamp(alpha, 0.0f, 1.0f);
00796 
00797     if (m_color_format == color::HEX)
00798     {
00799       alpha_caption_->SetText(NString::Printf("%x", (int) (alpha_ * 255)));
00800     }
00801     else if (m_color_format == color::INT)
00802     {
00803       alpha_caption_->SetText(NString::Printf("%d", (int) (alpha_ * 255)));
00804     }
00805     else
00806     {
00807       alpha_caption_->SetText(NString::Printf("%.3f", alpha_));
00808     }
00809 
00810     sigColorChanged.emit(rgb_.red, rgb_.green, rgb_.blue, alpha_);
00811   }
00812 
00813   void RGBValuator::OnReceiveMouseDown_Red(int x, int y, unsigned long button_flags, unsigned long key_flags)
00814   {
00815     if (m_color_model == color::RGB)
00816     {
00817       if (x < 0)
00818         rgb_.red = 0.0f;
00819       else if (x > red_valuator_->GetBaseWidth())
00820         rgb_.red = 1.0f;
00821       else
00822         rgb_.red = (float) x / (float) red_valuator_->GetBaseWidth();
00823 
00824       if (key_flags & NUX_STATE_CTRL)
00825       {
00826         SetRGB(rgb_.red, rgb_.red, rgb_.red);
00827       }
00828       else
00829       {
00830         SetRGB(rgb_.red, rgb_.green, rgb_.blue);
00831       }
00832     }
00833     else if (m_color_model == color::HSV)
00834     {
00835       if (x < 0)
00836         hsv_.hue = 0.0f;
00837       else if (x > red_valuator_->GetBaseWidth())
00838         hsv_.hue = 1.0f;
00839       else
00840         hsv_.hue = (float) x / (float) red_valuator_->GetBaseWidth();
00841 
00842       if (key_flags & NUX_STATE_CTRL)
00843       {
00844         SetHSV(hsv_.hue, hsv_.hue, hsv_.hue);
00845       }
00846       else
00847       {
00848         SetHSV(hsv_.hue, hsv_.saturation, hsv_.value);
00849       }
00850     }
00851     else if (m_color_model == color::HLS)
00852     {
00853       if (x < 0)
00854         hls_.hue = 0.0f;
00855       else if (x > red_valuator_->GetBaseWidth())
00856         hls_.hue = 1.0f;
00857       else
00858         hls_.hue = (float) x / (float) red_valuator_->GetBaseWidth();
00859 
00860       if (key_flags & NUX_STATE_CTRL)
00861       {
00862         SetHLS(hls_.hue, hls_.hue, hls_.hue);
00863       }
00864       else
00865       {
00866         SetHLS(hls_.hue, hls_.lightness, hls_.saturation);
00867       }
00868     }
00869 
00870     QueueDraw();
00871   }
00872 
00873   void RGBValuator::OnReceiveMouseDown_Green(int x, int y, unsigned long button_flags, unsigned long key_flags)
00874   {
00875     if (m_color_model == color::RGB)
00876     {
00877       if (x < 0)
00878         rgb_.green = 0.0f;
00879       else if (x > green_valuator_->GetBaseWidth())
00880         rgb_.green = 1.0f;
00881       else
00882         rgb_.green = (float) x / (float) green_valuator_->GetBaseWidth();
00883 
00884       if (key_flags & NUX_STATE_CTRL)
00885       {
00886         SetRGB(rgb_.green, rgb_.green, rgb_.green);
00887       }
00888       else
00889       {
00890         SetRGB(rgb_.red, rgb_.green, rgb_.blue);
00891       }
00892     }
00893     else if (m_color_model == color::HSV)
00894     {
00895       if (x < 0)
00896         hsv_.saturation = 0.0f;
00897       else if (x > green_valuator_->GetBaseWidth())
00898         hsv_.saturation = 1.0f;
00899       else
00900         hsv_.saturation = (float) x / (float) green_valuator_->GetBaseWidth();
00901 
00902       if (key_flags & NUX_STATE_CTRL)
00903       {
00904         SetHSV(hsv_.saturation, hsv_.saturation, hsv_.saturation);
00905       }
00906       else
00907       {
00908         SetHSV(hsv_.hue, hsv_.saturation, hsv_.value);
00909       }
00910     }
00911     else if (m_color_model == color::HLS)
00912     {
00913       if (x < 0)
00914         hls_.lightness = 0.0f;
00915       else if (x > green_valuator_->GetBaseWidth())
00916         hls_.lightness = 1.0f;
00917       else
00918         hls_.lightness = (float) x / (float) green_valuator_->GetBaseWidth();
00919 
00920       if (key_flags & NUX_STATE_CTRL)
00921       {
00922         SetHLS(hls_.lightness, hls_.lightness, hls_.lightness);
00923       }
00924       else
00925       {
00926         SetHLS(hls_.hue, hls_.lightness, hls_.saturation);
00927       }
00928     }
00929 
00930     QueueDraw();
00931   }
00932 
00933   void RGBValuator::OnReceiveMouseDown_Blue(int x, int y, unsigned long button_flags, unsigned long key_flags)
00934   {
00935     if (m_color_model == color::RGB)
00936     {
00937       if (x < 0)
00938         rgb_.blue = 0.0f;
00939       else if (x > blue_valuator_->GetBaseWidth())
00940         rgb_.blue = 1.0f;
00941       else
00942         rgb_.blue = (float) x / (float) blue_valuator_->GetBaseWidth();
00943 
00944       if (key_flags & NUX_STATE_CTRL)
00945       {
00946         SetRGB(rgb_.blue, rgb_.blue, rgb_.blue);
00947       }
00948       else
00949       {
00950         SetRGB(rgb_.red, rgb_.green, rgb_.blue);
00951       }
00952     }
00953     else if (m_color_model == color::HSV)
00954     {
00955       if (x < 0)
00956         hsv_.value = 0.0f;
00957       else if (x > blue_valuator_->GetBaseWidth())
00958         hsv_.value = 1.0f;
00959       else
00960         hsv_.value = (float) x / (float) blue_valuator_->GetBaseWidth();
00961 
00962       if (key_flags & NUX_STATE_CTRL)
00963       {
00964         SetHSV(hsv_.value, hsv_.value, hsv_.value);
00965       }
00966       else
00967       {
00968         SetHSV(hsv_.hue, hsv_.saturation, hsv_.value);
00969       }
00970     }
00971     else if (m_color_model == color::HLS)
00972     {
00973       if (x < 0)
00974         hls_.saturation = 0.0f;
00975       else if (x > blue_valuator_->GetBaseWidth())
00976         hls_.saturation = 1.0f;
00977       else
00978         hls_.saturation = (float) x / (float) blue_valuator_->GetBaseWidth();
00979 
00980       if (key_flags & NUX_STATE_CTRL)
00981       {
00982         SetHLS(hls_.saturation, hls_.saturation, hls_.saturation);
00983       }
00984       else
00985       {
00986         SetHLS(hls_.hue, hls_.lightness, hls_.saturation);
00987       }
00988     }
00989 
00990     QueueDraw();
00991   }
00992 
00993   void RGBValuator::OnReceiveMouseDown_Alpha(int x, int y, unsigned long button_flags, unsigned long key_flags)
00994   {
00995     if (x < 0)
00996       alpha_ = 0.0f;
00997     else if (x > alpha_valuator_->GetBaseWidth())
00998       alpha_ = 1.0f;
00999     else
01000       alpha_ = (float) x / (float) alpha_valuator_->GetBaseWidth();
01001 
01002     SetAlpha(alpha_);
01003     QueueDraw();
01004   }
01005 
01006   void RGBValuator::OnReceiveMouseDrag_Red(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
01007   {
01008     OnReceiveMouseDown_Red(x, y, button_flags, key_flags);
01009   }
01010 
01011   void RGBValuator::OnReceiveMouseDrag_Green(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
01012   {
01013     OnReceiveMouseDown_Green(x, y, button_flags, key_flags);
01014   }
01015 
01016   void RGBValuator::OnReceiveMouseDrag_Blue(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
01017   {
01018     OnReceiveMouseDown_Blue(x, y, button_flags, key_flags);
01019   }
01020 
01021   void RGBValuator::OnReceiveMouseDrag_Alpha(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags)
01022   {
01023     OnReceiveMouseDown_Alpha(x, y, button_flags, key_flags);
01024   }
01025 
01026   void RGBValuator::OnReceiveMouseUp_Red       (int x, int y, unsigned long button_flags, unsigned long key_flags)
01027   {
01028     if (m_color_model == color::HSV)
01029     {
01030       if (hsv_.hue >= 1.0f)
01031       {
01032         hsv_.hue = 1.0f;
01033         // XSI: hsv_.hue = 0.0f;
01034       }
01035 
01036       if (hsv_.saturation <= 0.0f)
01037       {
01038         // XSI: hsv_.hue = 0.0f;
01039       }
01040 
01041       if (hsv_.value <= 0.0f)
01042       {
01043         // XSI: hsv_.hue = 0.0f;
01044         // XSI: hsv_.saturation = 0.0f;
01045       }
01046 
01047       SetHSV(hsv_.hue, hsv_.saturation, hsv_.value);
01048     }
01049 
01050     if (m_color_model == color::HLS)
01051     {
01052       if (hls_.hue >= 1.0f)
01053       {
01054         hls_.hue = 1.0f;
01055         // XSI: hls_.hue = 0.0f;
01056       }
01057 
01058       if (hls_.saturation <= 0.0f)
01059       {
01060         // XSI: hls_.hue = 0.0f;
01061       }
01062 
01063       if (hls_.lightness <= 0.0f || hls_.lightness >= 1.0f)
01064       {
01065         // XSI: hls_.hue = 0.0f;
01066         // XSI: hls_.saturation = 0.0f;
01067       }
01068 
01069       SetHLS(hls_.hue, hls_.lightness, hls_.saturation);
01070     }
01071 
01072     QueueDraw();
01073   }
01074 
01075   void RGBValuator::OnReceiveMouseUp_Green     (int x, int y, unsigned long button_flags, unsigned long key_flags)
01076   {
01077     if (m_color_model == color::HSV)
01078     {
01079       if (hsv_.hue >= 1.0f)
01080       {
01081         hsv_.hue = 1.0f;
01082         // XSI: hsv_.hue = 0.0f;
01083       }
01084 
01085       if (hsv_.saturation <= 0.0f)
01086       {
01087         // XSI: hsv_.hue = 0.0f;
01088       }
01089 
01090       if (hsv_.value <= 0.0f)
01091       {
01092         // XSI: hsv_.hue = 0.0f;
01093         // XSI: hsv_.saturation = 0.0f;
01094       }
01095 
01096       SetHSV(hsv_.hue, hsv_.saturation, hsv_.value);
01097     }
01098 
01099     if (m_color_model == color::HLS)
01100     {
01101       if (hls_.hue >= 1.0f)
01102       {
01103         hls_.hue = 1.0f;
01104         // XSI: hls_.hue = 0.0f;
01105       }
01106 
01107       if (hls_.saturation <= 0.0f)
01108       {
01109         // XSI: hls_.hue = 0.0f;
01110       }
01111 
01112       if (hls_.lightness <= 0.0f || hls_.lightness >= 1.0f)
01113       {
01114         // XSI: hls_.hue = 0.0f;
01115         // XSI: hls_.saturation = 0.0f;
01116       }
01117 
01118       SetHLS(hls_.hue, hls_.lightness, hls_.saturation);
01119     }
01120 
01121     QueueDraw();
01122   }
01123 
01124   void RGBValuator::OnReceiveMouseUp_Blue(int x, int y, unsigned long button_flags, unsigned long key_flags)
01125   {
01126     if (m_color_model == color::HSV)
01127     {
01128       if (hsv_.hue >= 1.0f)
01129       {
01130         hsv_.hue = 1.0f;
01131         // XSI: hsv_.hue = 0.0f;
01132       }
01133 
01134       if (hsv_.saturation <= 0.0f)
01135       {
01136         // XSI: hsv_.hue = 0.0f;
01137       }
01138 
01139       if (hsv_.value <= 0.0f)
01140       {
01141         // XSI: hsv_.hue = 0.0f;
01142         // XSI: hsv_.saturation = 0.0f;
01143       }
01144 
01145       SetHSV(hsv_.hue, hsv_.saturation, hsv_.value);
01146     }
01147 
01148     if (m_color_model == color::HLS)
01149     {
01150       if (hls_.hue >= 1.0f)
01151       {
01152         hls_.hue = 1.0f;
01153         // XSI: hls_.hue = 0.0f;
01154       }
01155 
01156       if (hls_.saturation <= 0.0f)
01157       {
01158         // XSI: hls_.hue = 0.0f;
01159       }
01160 
01161       if (hls_.lightness <= 0.0f || hls_.lightness >= 1.0f)
01162       {
01163         // XSI: hls_.hue = 0.0f;
01164         // XSI: hls_.saturation = 0.0f;
01165       }
01166 
01167       SetHLS(hls_.hue, hls_.lightness, hls_.saturation);
01168     }
01169 
01170     QueueDraw();
01171   }
01172 
01173   void RGBValuator::RecvMouseDownColorModel(int x, int y, unsigned long button_flags, unsigned long key_flags)
01174   {
01175     QueueDraw();
01176   }
01177 
01178   void RGBValuator::OnChangeColorModel(AbstractButton* button)
01179   {
01180     if (m_color_model == color::RGB)
01181     {
01182       SetColorModel(color::HLS);
01183       hls_ = color::HueLightnessSaturation(rgb_);
01184       SetHLS(hls_.hue, hls_.lightness, hls_.saturation);
01185     }
01186     else if (m_color_model == color::HLS)
01187     {
01188       SetColorModel(color::HSV);
01189       rgb_ = color::RedGreenBlue(hls_);
01190       hsv_ = color::HueSaturationValue(rgb_);
01191       SetHSV(hsv_.hue, hsv_.saturation, hsv_.value);
01192 
01193       if (hsv_.hue == -1.0f)
01194       {
01195         hsv_.hue = 0;
01196       }
01197     }
01198     else if (m_color_model == color::HSV)
01199     {
01200       SetColorModel(color::RGB);
01201       rgb_ = color::RedGreenBlue(hsv_);
01202       SetRGB(rgb_.red, rgb_.green, rgb_.blue);
01203     }
01204 
01205     QueueDraw();
01206   }
01207 
01208   void RGBValuator::OnChangeColorFormat(AbstractButton* button)
01209   {
01210     if (m_color_format == color::FLOAT)
01211     {
01212       SetColorFormat(color::INT);
01213     }
01214     else if (m_color_format == color::INT)
01215     {
01216       SetColorFormat(color::HEX);
01217     }
01218     else if (m_color_format == color::HEX)
01219     {
01220       SetColorFormat(color::FLOAT);
01221     }
01222 
01223   }
01224 
01225   void RGBValuator::OnComponentInput(EditTextBox *textbox, const NString &s, int componentIndex)
01226   {
01227     float f = 0;
01228 
01229     if ((m_color_format == color::HEX) && (m_HexRegExp.Validate(s.GetTCharPtr()) == Validator::Acceptable))
01230     {
01231       f = (float) m_HexRegExp.ToInteger(s.GetTCharPtr()) / 255.0f;
01232     }
01233     else if ((m_color_format == color::INT) && (m_IntRegExp.Validate(s.GetTCharPtr()) == Validator::Acceptable))
01234     {
01235       f = (float) m_IntRegExp.ToInteger(s.GetTCharPtr()) / 255.0f;
01236     }
01237     else
01238     {
01239       f = (float) m_DoubleRegExp.ToDouble(s.GetTCharPtr());
01240     }
01241 
01242     f = Clamp(f, 0.0f, 1.0f);
01243 
01244     if (componentIndex == 0)
01245     {
01246       f = Clamp(f, 0.0f, 1.0f);
01247 
01248       if (m_color_model == color::RGB)
01249       {
01250         rgb_.red = f;
01251         SetRGB(rgb_.red, rgb_.green, rgb_.blue);
01252       }
01253       else if (m_color_model == color::HSV)
01254       {
01255         hsv_.hue = f;
01256         OnReceiveMouseUp_Red(0, 0, 0, 0);
01257         SetHSV(hsv_.hue, hsv_.saturation, hsv_.value);
01258       }
01259       else if (m_color_model == color::HLS)
01260       {
01261         hls_.hue = f;
01262         OnReceiveMouseUp_Red(0, 0, 0, 0);
01263         SetHLS(hls_.hue, hls_.lightness, hls_.saturation);
01264       }
01265     }
01266 
01267     if (componentIndex == 1)
01268     {
01269       f = Clamp(f, 0.0f, 1.0f);
01270 
01271       if (m_color_model == color::RGB)
01272       {
01273         rgb_.green = f;
01274         SetRGB(rgb_.red, rgb_.green, rgb_.blue);
01275       }
01276       else if (m_color_model == color::HSV)
01277       {
01278         hsv_.saturation = f;
01279         OnReceiveMouseUp_Green(0, 0, 0, 0);
01280         SetHSV(hsv_.hue, hsv_.saturation, hsv_.value);
01281       }
01282       else if (m_color_model == color::HLS)
01283       {
01284         hls_.lightness = f;
01285         OnReceiveMouseUp_Green(0, 0, 0, 0);
01286         SetHLS(hls_.hue, hls_.lightness, hls_.saturation);
01287       }
01288     }
01289 
01290     if (componentIndex == 2)
01291     {
01292       f = Clamp(f, 0.0f, 1.0f);
01293 
01294       if (m_color_model == color::RGB)
01295       {
01296         rgb_.blue = f;
01297         SetRGB(rgb_.red, rgb_.green, rgb_.blue);
01298       }
01299       else if (m_color_model == color::HSV)
01300       {
01301         hsv_.value = f;
01302         OnReceiveMouseUp_Blue(0, 0, 0, 0);
01303         SetHSV(hsv_.hue, hsv_.saturation, hsv_.value);
01304       }
01305       else if (m_color_model == color::HLS)
01306       {
01307         hls_.saturation = f;
01308         OnReceiveMouseUp_Blue(0, 0, 0, 0);
01309         SetHLS(hls_.hue, hls_.lightness, hls_.saturation);
01310       }
01311     }
01312 
01313     if (componentIndex == 3)
01314     {
01315       float f = 0;
01316       f = CharToDouble(s.GetTCharPtr());
01317       f = Clamp(f, 0.0f, 1.0f);
01318       //if(m_color_model == color::RGB)
01319       {
01320         alpha_ = f;
01321         SetAlpha(alpha_);
01322       }
01323     }
01324 
01325     QueueDraw();
01326   }
01327 
01328 
01329   void RGBValuator::SetColorModel(color::Model cm)
01330   {
01331     if (cm == color::RGB)
01332     {
01333       m_color_model = color::RGB;
01334       m_ColorModel->SetLabel("RGB");
01335 
01336       m_ComponentLabel0->SetBaseString("R");
01337       m_ComponentLabel1->SetBaseString("G");
01338       m_ComponentLabel2->SetBaseString("B");
01339       m_ComponentAlpha->SetBaseString("A");
01340 
01341     }
01342 
01343     if (cm == color::HSV)
01344     {
01345       m_color_model = color::HSV;
01346       m_ColorModel->SetLabel("HSV");
01347 
01348       m_ComponentLabel0->SetBaseString("H");
01349       m_ComponentLabel1->SetBaseString("S");
01350       m_ComponentLabel2->SetBaseString("V");
01351       m_ComponentAlpha->SetBaseString("A");
01352     }
01353 
01354     if (cm == color::HLS)
01355     {
01356       m_color_model = color::HLS;
01357       m_ColorModel->SetLabel("HLS");
01358 
01359       m_ComponentLabel0->SetBaseString("H");
01360       m_ComponentLabel1->SetBaseString("L");
01361       m_ComponentLabel2->SetBaseString("S");
01362       m_ComponentAlpha->SetBaseString("A");
01363     }
01364 
01365     if (cm == color::YUV)
01366     {
01367       m_color_model = color::YUV;
01368       m_ColorModel->SetLabel("YUV");
01369 
01370       m_ComponentLabel0->SetBaseString("Y");
01371       m_ComponentLabel1->SetBaseString("U");
01372       m_ComponentLabel2->SetBaseString("V");
01373       m_ComponentAlpha->SetBaseString("A");
01374     }
01375   }
01376 
01377   void RGBValuator::SetColorFormat(color::Format cf)
01378   {
01379     if (cf == color::FLOAT)
01380     {
01381       m_color_format = color::FLOAT;
01382       m_ColorFormat->SetLabel("float");
01383       red_caption_->SetKeyEntryType(BaseKeyboardHandler::eAlphaNumeric);
01384       green_caption_->SetKeyEntryType(BaseKeyboardHandler::eAlphaNumeric);
01385       blue_caption_->SetKeyEntryType(BaseKeyboardHandler::eAlphaNumeric);
01386 
01387       red_caption_->SetPrefix("");
01388       green_caption_->SetPrefix("");
01389       blue_caption_->SetPrefix("");
01390       alpha_caption_->SetPrefix("");
01391     }
01392 
01393     if (cf == color::INT)
01394     {
01395       m_color_format = color::INT;
01396       m_ColorFormat->SetLabel("int");
01397       red_caption_->SetKeyEntryType(BaseKeyboardHandler::eIntegerNumber);
01398       green_caption_->SetKeyEntryType(BaseKeyboardHandler::eIntegerNumber);
01399       blue_caption_->SetKeyEntryType(BaseKeyboardHandler::eIntegerNumber);
01400 
01401       red_caption_->SetPrefix("");
01402       green_caption_->SetPrefix("");
01403       blue_caption_->SetPrefix("");
01404       alpha_caption_->SetPrefix("");
01405     }
01406 
01407     if (cf == color::HEX)
01408     {
01409       m_color_format = color::HEX;
01410       m_ColorFormat->SetLabel("hex");
01411       red_caption_->SetKeyEntryType(BaseKeyboardHandler::eHexadecimalNumber);
01412       green_caption_->SetKeyEntryType(BaseKeyboardHandler::eHexadecimalNumber);
01413       blue_caption_->SetKeyEntryType(BaseKeyboardHandler::eHexadecimalNumber);
01414 
01415       red_caption_->SetPrefix("0x");
01416       green_caption_->SetPrefix("0x");
01417       blue_caption_->SetPrefix("0x");
01418       alpha_caption_->SetPrefix("0x");
01419     }
01420 
01421     if (m_color_model == color::RGB)
01422     {
01423       SetRGBA(rgb_.red, rgb_.green, rgb_.blue, alpha_);
01424     }
01425     else if (m_color_model == color::HLS)
01426     {
01427       SetHLS(hls_.hue, hls_.lightness, hls_.saturation);
01428       SetAlpha(alpha_);
01429     }
01430     else if (m_color_model == color::HSV)
01431     {
01432       SetHSV(hsv_.hue, hsv_.saturation, hsv_.value);
01433       SetAlpha(alpha_);
01434     }
01435   }
01436 
01437   void RGBValuator::PreLayoutManagement()
01438   {
01439     View::PreLayoutManagement();
01440     if (view_layout_)
01441     {
01442       // Constrain the vertical expansion of the color selector.
01443       view_layout_->SetBaseHeight(1);
01444     }
01445   }
01446 
01447   Color RGBValuator::GetColor() const
01448   {
01449     return Color(rgb_, alpha_);
01450   }
01451 
01452   void RGBValuator::EmitColorChangedSignal()
01453   {
01454     sigColorChanged.emit(rgb_.red, rgb_.green, rgb_.blue, alpha_);
01455   }
01456 
01457   bool RGBValuator::AcceptKeyNavFocus()
01458   {
01459     return false;
01460   }
01461 }