Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_ColorPickerModel.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://floralicense.org/license/
10 //
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.
16 //
17 /**
18 * @file                 FUiCtrl_ColorPickerModel.cpp
19 * @brief                This file contains implementation of _ColorPickerModel class
20 *
21 * This file contains implementation of _ColorPickerModel class.
22 */
23
24 #include <new>
25 #include <FBaseErrorDefine.h>
26 #include "FUiCtrl_ColorPickerModel.h"
27 #include "FUiCtrl_ColorPickerPresenter.h"
28
29 using namespace Tizen::Graphics;
30
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;
37
38 namespace Tizen { namespace Ui { namespace Controls
39 {
40
41 _ColorPickerModel::_ColorPickerModel(void)
42         : __hue(MIN_VALUE)
43         , __sat(MIN_VALUE)
44         , __lum(MIN_VALUE)
45         , __color(Color::GetColor(COLOR_ID_BLACK))
46 {
47 }
48
49 _ColorPickerModel::~_ColorPickerModel()
50 {
51 }
52
53 _ColorPickerModel*
54 _ColorPickerModel::CreateInstanceN(void)
55 {
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.");
59
60         if (IsFailed(GetLastResult()) == true)
61         {
62                 delete pColorPickerModel;
63                 return null;
64         }
65
66         return pColorPickerModel;
67 }
68
69 double
70 _ColorPickerModel::GetHue(void) const
71 {
72         return __hue;
73 }
74
75 double
76 _ColorPickerModel::GetSaturation(void) const
77 {
78         return __sat;
79 }
80
81 double
82 _ColorPickerModel::GetLuminance(void) const
83 {
84         return __lum;
85 }
86
87 void
88 _ColorPickerModel::SetHue(double hueValue)
89 {
90         if (hueValue > MAX_HUE_VALUE)
91         {
92                 __hue = MAX_HUE_VALUE;
93         }
94         else if (hueValue < MIN_VALUE)
95         {
96                 __hue = MIN_VALUE;
97         }
98         else
99         {
100                 __hue = hueValue;
101         }
102         CalulateColor();
103 }
104
105 void
106 _ColorPickerModel::SetSaturation(double saturationValue)
107 {
108         if (saturationValue > MAX_VALUE)
109         {
110                 __sat = MAX_VALUE;
111         }
112         else if (saturationValue < MIN_VALUE)
113         {
114                 __sat = MIN_VALUE;
115         }
116         else
117         {
118                 __sat = saturationValue;
119         }
120         CalulateColor();
121 }
122
123 void
124 _ColorPickerModel::SetLuminance(double luminanceValue)
125 {
126         if (luminanceValue > MAX_VALUE)
127         {
128                 __lum = MAX_VALUE;
129         }
130         else if (luminanceValue < MIN_VALUE)
131         {
132                 __lum = MIN_VALUE;
133         }
134         else
135         {
136                 __lum = luminanceValue;
137         }
138         CalulateColor();
139 }
140
141 Color
142 _ColorPickerModel::GetHueColor(void) const
143 {
144         Color color;
145
146         ConvertHSLToRGB(__hue, STANDARD_SATURATION_VALUE, STANDARD_LUMINANCE_VALUE, color);
147
148         return color;
149 }
150
151 Color
152 _ColorPickerModel::GetSaturationColor(void) const
153 {
154         Color color;
155
156         ConvertHSLToRGB(__hue, __sat, STANDARD_LUMINANCE_VALUE, color);
157
158         return color;
159 }
160
161 Color
162 _ColorPickerModel::GetColor(void) const
163 {
164         return __color;
165 }
166
167 void
168 _ColorPickerModel::SetColor(const Color& color)
169 {
170         __color = color;
171
172         ConvertRGBToHSL(__color, __hue, __sat, __lum);
173 }
174
175 void
176 _ColorPickerModel::CalulateColor(void)
177 {
178         ConvertHSLToRGB(__hue, __sat, __lum, __color);
179 }
180
181 void
182 _ColorPickerModel::ConvertRGBToHSL(const Color& color, double& h, double& s, double& l)
183 {
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;
192         if (diff <= 0.0)
193         {
194                 return;
195         }
196
197         l = (min + max) / 2.0;
198         s = diff / ((l <= STANDARD_LUMINANCE_VALUE) ? (max + min) : (2.0 - max - min));
199
200         if (normalizeR >= max)
201         {
202                 if (normalizeG <= min)
203                 {
204                         h = 5.0 + (max - normalizeB) / diff;
205                 }
206                 else
207                 {
208                         h = 1.0 - (max - normalizeG) / diff;
209                 }
210         }
211         else if (normalizeG >= max)
212         {
213                 if (normalizeB <= min)
214                 {
215                         h = 1.0 + (max - normalizeR) / diff;
216                 }
217                 else
218                 {
219                         h = 3.0 - (max - normalizeB) / diff;
220                 }
221         }
222         else
223         {
224                 if (normalizeR <= min)
225                 {
226                         h = 3.0 + (max - normalizeG) / diff;
227                 }
228                 else
229                 {
230                         h = 5.0 - (max - normalizeR) / diff;
231                 }
232         }
233
234         h /= 6.0;
235 }
236
237 void
238 _ColorPickerModel::ConvertHSLToRGB(double h, double s, double l, Color& color)
239 {
240         result r = E_SUCCESS;
241         double v = 0.0;
242         double normalizeR = 0.0;
243         double normalizeG = 0.0;
244         double normalizeB = 0.0;
245
246         normalizeR = l;
247         normalizeG = l;
248         normalizeB = l;
249
250         v = (l <= STANDARD_LUMINANCE_VALUE) ? (l + l * s) : (l + s - l * s);
251
252         if (v > 0)
253         {
254                 double m = l + l - v;
255
256                 h *= 6.0;
257
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;
263
264                 switch (sextant)
265                 {
266                 case 0:
267                         normalizeR = v;
268                         normalizeG = mid1;
269                         normalizeB = m;
270                         break;
271
272                 case 1:
273                         normalizeR = mid2;
274                         normalizeG = v;
275                         normalizeB = m;
276                         break;
277
278                 case 2:
279                         normalizeR = m;
280                         normalizeG = v;
281                         normalizeB = mid1;
282                         break;
283
284                 case 3:
285                         normalizeR = m;
286                         normalizeG = mid2;
287                         normalizeB = v;
288                         break;
289
290                 case 4:
291                         normalizeR = mid1;
292                         normalizeG = m;
293                         normalizeB = v;
294                         break;
295
296                 case 5:
297                         normalizeR = v;
298                         normalizeG = m;
299                         normalizeB = mid2;
300                         break;
301
302                 default:
303                         r = E_SYSTEM;
304                         break;
305                 }
306
307                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[E_SYSTEM] A system error has occurred. Hue value is out of range.");
308         }
309
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
314 }
315
316 }}} // Tizen::Ui::Controls