2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FUiCtrl_ColorPickerModel.cpp
19 * @brief This file contains implementation of _ColorPickerModel class
21 * This file contains implementation of _ColorPickerModel class.
25 #include <FBaseErrorDefine.h>
26 #include "FUiCtrl_ColorPickerModel.h"
27 #include "FUiCtrl_ColorPickerPresenter.h"
29 using namespace Tizen::Graphics;
31 const double MAX_COLOR_DEGREE = 255.0;
32 const double MIN_VALUE = 0.0;
33 const double MAX_VALUE = 1.0;
34 const double MAX_HUE_VALUE = 0.999999;
35 const double STANDARD_SATURATION_VALUE = 1.0;
36 const double STANDARD_LUMINANCE_VALUE = 0.5;
38 namespace Tizen { namespace Ui { namespace Controls
41 _ColorPickerModel::_ColorPickerModel(void)
45 , __color(Color::GetColor(COLOR_ID_BLACK))
49 _ColorPickerModel::~_ColorPickerModel()
54 _ColorPickerModel::CreateInstanceN(void)
56 _ColorPickerModel* pColorPickerModel = new (std::nothrow) _ColorPickerModel;
57 SysTryReturn(NID_UI_CTRL, (pColorPickerModel != null), null, E_OUT_OF_MEMORY,
58 "[E_OUT_OF_MEMORY] Memory allocation failed.");
60 if (IsFailed(GetLastResult()) == true)
62 delete pColorPickerModel;
66 return pColorPickerModel;
70 _ColorPickerModel::GetHue(void) const
76 _ColorPickerModel::GetSaturation(void) const
82 _ColorPickerModel::GetLuminance(void) const
88 _ColorPickerModel::SetHue(double hueValue)
90 if (hueValue > MAX_HUE_VALUE)
92 __hue = MAX_HUE_VALUE;
94 else if (hueValue < MIN_VALUE)
106 _ColorPickerModel::SetSaturation(double saturationValue)
108 if (saturationValue > MAX_VALUE)
112 else if (saturationValue < MIN_VALUE)
118 __sat = saturationValue;
124 _ColorPickerModel::SetLuminance(double luminanceValue)
126 if (luminanceValue > MAX_VALUE)
130 else if (luminanceValue < MIN_VALUE)
136 __lum = luminanceValue;
142 _ColorPickerModel::GetHueColor(void) const
146 ConvertHSLToRGB(__hue, STANDARD_SATURATION_VALUE, STANDARD_LUMINANCE_VALUE, color);
152 _ColorPickerModel::GetSaturationColor(void) const
156 ConvertHSLToRGB(__hue, __sat, STANDARD_LUMINANCE_VALUE, color);
162 _ColorPickerModel::GetColor(void) const
168 _ColorPickerModel::SetColor(const Color& color)
172 ConvertRGBToHSL(__color, __hue, __sat, __lum);
176 _ColorPickerModel::CalulateColor(void)
178 ConvertHSLToRGB(__hue, __sat, __lum, __color);
182 _ColorPickerModel::ConvertRGBToHSL(const Color& color, double& h, double& s, double& l)
184 double normalizeR = (double)color.GetRed() / MAX_COLOR_DEGREE;
185 double normalizeG = (double)color.GetGreen() / MAX_COLOR_DEGREE;
186 double normalizeB = (double)color.GetBlue() / MAX_COLOR_DEGREE;
187 double max = (normalizeR > normalizeG) ? ((normalizeR > normalizeB) ? normalizeR : normalizeB)
188 : ((normalizeG > normalizeB) ? normalizeG : normalizeB);
189 double min = (normalizeR > normalizeG) ? ((normalizeG > normalizeB) ? normalizeB : normalizeG)
190 : ((normalizeR > normalizeB) ? normalizeB : normalizeR);
191 double diff = max - min;
197 l = (min + max) / 2.0;
198 s = diff / ((l <= STANDARD_LUMINANCE_VALUE) ? (max + min) : (2.0 - max - min));
200 if (normalizeR >= max)
202 if (normalizeG <= min)
204 h = 5.0 + (max - normalizeB) / diff;
208 h = 1.0 - (max - normalizeG) / diff;
211 else if (normalizeG >= max)
213 if (normalizeB <= min)
215 h = 1.0 + (max - normalizeR) / diff;
219 h = 3.0 - (max - normalizeB) / diff;
224 if (normalizeR <= min)
226 h = 3.0 + (max - normalizeG) / diff;
230 h = 5.0 - (max - normalizeR) / diff;
238 _ColorPickerModel::ConvertHSLToRGB(double h, double s, double l, Color& color)
240 result r = E_SUCCESS;
242 double normalizeR = 0.0;
243 double normalizeG = 0.0;
244 double normalizeB = 0.0;
250 v = (l <= STANDARD_LUMINANCE_VALUE) ? (l + l * s) : (l + s - l * s);
254 double m = l + l - v;
258 int sextant = (int) h;
259 double fract = h - sextant;
260 double vsf = (v - m) * fract;
261 double mid1 = m + vsf;
262 double mid2 = v - vsf;
307 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[E_SYSTEM] A system error has occurred. Hue value is out of range.");
310 color.SetColorComponents((byte)(normalizeR * MAX_COLOR_DEGREE), // red
311 (byte)(normalizeG * MAX_COLOR_DEGREE), // green
312 (byte)(normalizeB * MAX_COLOR_DEGREE), // blue
313 (byte)(MAX_COLOR_DEGREE)); // alpha
316 }}} // Tizen::Ui::Controls