2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0/
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.
19 * @file FUiCtrl_ColorPickerModel.cpp
20 * @brief This file contains implementation of _ColorPickerModel class
22 * This file contains implementation of _ColorPickerModel class.
26 #include <FBaseErrorDefine.h>
27 #include "FUiCtrl_ColorPickerModel.h"
28 #include "FUiCtrl_ColorPickerPresenter.h"
30 using namespace Tizen::Graphics;
32 const double MAX_COLOR_DEGREE = 255.0;
33 const double MIN_VALUE = 0.0;
34 const double MAX_VALUE = 1.0;
35 const double MAX_HUE_VALUE = 0.999999;
36 const double STANDARD_SATURATION_VALUE = 1.0;
37 const double STANDARD_LUMINANCE_VALUE = 0.5;
39 namespace Tizen { namespace Ui { namespace Controls
42 _ColorPickerModel::_ColorPickerModel(void)
46 , __color(Color::GetColor(COLOR_ID_BLACK))
50 _ColorPickerModel::~_ColorPickerModel()
55 _ColorPickerModel::CreateInstanceN(void)
57 _ColorPickerModel* pColorPickerModel = new (std::nothrow) _ColorPickerModel;
58 SysTryReturn(NID_UI_CTRL, (pColorPickerModel != null), null, E_OUT_OF_MEMORY,
59 "[E_OUT_OF_MEMORY] Memory allocation failed.");
61 if (IsFailed(GetLastResult()) == true)
63 delete pColorPickerModel;
67 return pColorPickerModel;
71 _ColorPickerModel::GetHue(void) const
77 _ColorPickerModel::GetSaturation(void) const
83 _ColorPickerModel::GetLuminance(void) const
89 _ColorPickerModel::SetHue(double hueValue)
91 if (hueValue > MAX_HUE_VALUE)
95 else if (hueValue < MIN_VALUE)
109 _ColorPickerModel::SetSaturation(double saturationValue)
111 if (saturationValue > MAX_VALUE)
115 else if (saturationValue < MIN_VALUE)
121 __sat = saturationValue;
129 _ColorPickerModel::SetLuminance(double luminanceValue)
131 if (luminanceValue > MAX_VALUE)
135 else if (luminanceValue < MIN_VALUE)
141 __lum = luminanceValue;
149 _ColorPickerModel::GetHueColor(void) const
153 ConvertHSLToRGB(__hue, STANDARD_SATURATION_VALUE, STANDARD_LUMINANCE_VALUE, color);
159 _ColorPickerModel::GetSaturationColor(void) const
163 ConvertHSLToRGB(__hue, __sat, STANDARD_LUMINANCE_VALUE, color);
169 _ColorPickerModel::GetColor(void) const
175 _ColorPickerModel::SetColor(const Color& color)
179 ConvertRGBToHSL(__color, __hue, __sat, __lum);
185 _ColorPickerModel::CalulateColor(void)
187 ConvertHSLToRGB(__hue, __sat, __lum, __color);
193 _ColorPickerModel::ConvertRGBToHSL(const Color& color, double& h, double& s, double& l)
199 double normalizeR = (double)color.GetRed() / MAX_COLOR_DEGREE;
200 double normalizeG = (double)color.GetGreen() / MAX_COLOR_DEGREE;
201 double normalizeB = (double)color.GetBlue() / MAX_COLOR_DEGREE;
202 double max = (normalizeR > normalizeG) ? ((normalizeR > normalizeB) ? normalizeR : normalizeB)
203 : ((normalizeG > normalizeB) ? normalizeG : normalizeB);
204 double min = (normalizeR > normalizeG) ? ((normalizeG > normalizeB) ? normalizeB : normalizeG)
205 : ((normalizeR > normalizeB) ? normalizeB : normalizeR);
206 double diff = max - min;
208 l = (min + max) / 2.0;
215 s = diff / ((l <= STANDARD_LUMINANCE_VALUE) ? (max + min) : (2.0 - max - min));
217 if (normalizeR >= max)
219 if (normalizeG <= min)
221 h = 5.0 + (max - normalizeB) / diff;
225 h = 1.0 - (max - normalizeG) / diff;
228 else if (normalizeG >= max)
230 if (normalizeB <= min)
232 h = 1.0 + (max - normalizeR) / diff;
236 h = 3.0 - (max - normalizeB) / diff;
241 if (normalizeR <= min)
243 h = 3.0 + (max - normalizeG) / diff;
247 h = 5.0 - (max - normalizeR) / diff;
257 _ColorPickerModel::ConvertHSLToRGB(double h, double s, double l, Color& color)
259 result r = E_SUCCESS;
261 double normalizeR = 0.0;
262 double normalizeG = 0.0;
263 double normalizeB = 0.0;
269 v = (l <= STANDARD_LUMINANCE_VALUE) ? (l + l * s) : (l + s - l * s);
271 if (h > MAX_HUE_VALUE)
278 double m = l + l - v;
282 int sextant = (int) h;
283 double fract = h - sextant;
284 double vsf = (v - m) * fract;
285 double mid1 = m + vsf;
286 double mid2 = v - vsf;
331 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[E_SYSTEM] A system error has occurred. Hue value is out of range.");
334 color.SetColorComponents((byte)(normalizeR * MAX_COLOR_DEGREE), // red
335 (byte)(normalizeG * MAX_COLOR_DEGREE), // green
336 (byte)(normalizeB * MAX_COLOR_DEGREE), // blue
337 (byte)(MAX_COLOR_DEGREE)); // alpha
342 }}} // Tizen::Ui::Controls