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