3 * This software is licensed under the MIT licence (as defined by the OSI at
4 * http://www.opensource.org/licenses/mit-license.php)
6 * ***************************************************************************
7 * Copyright (C) 2011 by Intel Corporation Ltd.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 * ***************************************************************************
29 (function($, undefined) {
31 $.widget("tizen.colorwidget", $.tizen.widgetex, {
37 attr: "data-" + ($.mobile.ns || "") + "color",
38 signal: "colorchanged"
41 _getElementColor: function(el, cssProp) {
42 return el.jqmData("clr");
45 _setElementColor: function(el, hsl, cssProp) {
46 var clrlib = $.tizen.colorwidget.clrlib,
47 clr = clrlib.RGBToHTML(clrlib.HSLToRGB(hsl)),
48 dclr = clrlib.RGBToHTML(clrlib.HSLToGray(hsl));
50 el.jqmData("clr", clr);
51 el.jqmData("dclr", dclr);
52 el.jqmData("cssProp", cssProp);
53 el.attr("data-" + ($.mobile.ns || "") + "has-dclr", true);
54 el.css(cssProp, this.options.disabled ? dclr : clr);
56 return { clr: clr, dclr: dclr };
59 _displayDisabledState: function(toplevel) {
61 sel = ":jqmData(has-dclr='true')",
62 dst = toplevel.is(sel) ? toplevel : $([]);
64 .add(toplevel.find(sel))
68 el.css(el.jqmData("cssProp"), el.jqmData(self.options.disabled ? "dclr" : "clr"));
72 _setColor: function(value) {
73 var currentValue = (this.options.color + "");
76 value = value.match(/#[0-9A-Fa-f]{6}/)
78 : currentValue.match(/#[0-9A-Fa-f]{6}/)
80 : $.tizen.colorwidget.prototype.options.color;
82 if (this.options.color !== value) {
83 this.options.color = value;
84 this._setValue(value);
91 $.tizen.colorwidget.clrlib = {
92 nearestInt: function(val) {
93 var theFloor = Math.floor(val);
95 return (((val - theFloor) > 0.5) ? (theFloor + 1) : theFloor);
98 // Converts html color string to rgb array.
100 // Input: string clr_str, where
101 // clr_str is of the form "#aabbcc"
103 // Returns: [ r, g, b ], where
107 HTMLToRGB: function(clr_str) {
108 clr_str = (('#' == clr_str.charAt(0)) ? clr_str.substring(1) : clr_str);
110 return [ parseInt(clr_str.substring(0, 2), 16) / 255.0,
111 parseInt(clr_str.substring(2, 4), 16) / 255.0,
112 parseInt(clr_str.substring(4, 6), 16) / 255.0 ];
115 // Converts rgb array to html color string.
117 // Input: [ r, g, b ], where
122 // Returns: string of the form "#aabbcc"
123 RGBToHTML: function(rgb) {
124 var ret = "#", val, theFloor;
125 for (var Nix in rgb) {
126 val = rgb[Nix] * 255;
127 theFloor = Math.floor(val);
128 val = ((val - theFloor > 0.5) ? (theFloor + 1) : theFloor);
129 ret = ret + (((val < 16) ? "0" : "") + (val & 0xff).toString(16));
135 // Converts hsl to rgb.
137 // From http://130.113.54.154/~monger/hsl-rgb.html
139 // Input: [ h, s, l ], where
144 // Returns: [ r, g, b ], where
148 HSLToRGB: function(hsl) {
149 var h = hsl[0] / 360.0, s = hsl[1], l = hsl[2];
154 var temp2 = ((l < 0.5)
157 temp1 = 2.0 * l - temp2,
164 temp3.r = ((temp3.r < 0) ? (temp3.r + 1.0) : ((temp3.r > 1) ? (temp3.r - 1.0) : temp3.r));
165 temp3.g = ((temp3.g < 0) ? (temp3.g + 1.0) : ((temp3.g > 1) ? (temp3.g - 1.0) : temp3.g));
166 temp3.b = ((temp3.b < 0) ? (temp3.b + 1.0) : ((temp3.b > 1) ? (temp3.b - 1.0) : temp3.b));
169 (((6.0 * temp3.r) < 1) ? (temp1 + (temp2 - temp1) * 6.0 * temp3.r) :
170 (((2.0 * temp3.r) < 1) ? temp2 :
171 (((3.0 * temp3.r) < 2) ? (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - temp3.r) * 6.0) :
173 (((6.0 * temp3.g) < 1) ? (temp1 + (temp2 - temp1) * 6.0 * temp3.g) :
174 (((2.0 * temp3.g) < 1) ? temp2 :
175 (((3.0 * temp3.g) < 2) ? (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - temp3.g) * 6.0) :
177 (((6.0 * temp3.b) < 1) ? (temp1 + (temp2 - temp1) * 6.0 * temp3.b) :
178 (((2.0 * temp3.b) < 1) ? temp2 :
179 (((3.0 * temp3.b) < 2) ? (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - temp3.b) * 6.0) :
185 // Converts hsv to rgb.
187 // Input: [ h, s, v ], where
192 // Returns: [ r, g, b ], where
196 HSVToRGB: function(hsv) {
197 return $.tizen.colorwidget.clrlib.HSLToRGB($.tizen.colorwidget.clrlib.HSVToHSL(hsv));
200 // Converts rgb to hsv.
202 // from http://coecsl.ece.illinois.edu/ge423/spring05/group8/FinalProject/HSV_writeup.pdf
204 // Input: [ r, g, b ], where
209 // Returns: [ h, s, v ], where
213 RGBToHSV: function(rgb) {
214 var min, max, delta, h, s, v, r = rgb[0], g = rgb[1], b = rgb[2];
216 min = Math.min(r, Math.min(g, b));
217 max = Math.max(r, Math.max(g, b));
224 if (delta > 0.00001) {
228 h = (g - b) / delta ;
231 h = 2 + (b - r) / delta ;
233 h = 4 + (r - g) / delta ;
244 // Converts hsv to hsl.
246 // Input: [ h, s, v ], where
251 // Returns: [ h, s, l ], where
255 HSVToHSL: function(hsv) {
257 delta = hsv[1] * max,
261 s_divisor = ((half_sum < 0.5) ? sum : (2 - max - min));
263 return [ hsv[0], ((0 == s_divisor) ? 0 : (delta / s_divisor)), half_sum ];
266 // Converts rgb to hsl
268 // Input: [ r, g, b ], where
273 // Returns: [ h, s, l ], where
277 RGBToHSL: function(rgb) {
278 return $.tizen.colorwidget.clrlib.HSVToHSL($.tizen.colorwidget.clrlib.RGBToHSV(rgb));
281 // Converts hsl to grayscale
282 // Full-saturation magic grayscale values were taken from the Gimp
284 // Input: [ h, s, l ], where
289 // Returns: [ r, g, b ], where
293 HSLToGray: function(hsl) {
294 var intrinsic_vals = [0.211764706, 0.929411765, 0.71372549, 0.788235294, 0.070588235, 0.28627451, 0.211764706],
295 idx = Math.floor(hsl[0] / 60),
299 begVal = intrinsic_vals[idx];
300 endVal = intrinsic_vals[idx + 1];
304 var lowerHalfPercent = hsl[2] * 2;
305 begVal *= lowerHalfPercent;
306 endVal *= lowerHalfPercent;
309 var upperHalfPercent = (hsl[2] - 0.5) * 2;
310 begVal += (1.0 - begVal) * upperHalfPercent;
311 endVal += (1.0 - endVal) * upperHalfPercent;
314 // This is the gray value at full sat, whereas hsl[2] is the gray value at 0 sat.
315 val = begVal + ((endVal - begVal) * (hsl[0] - (idx * 60))) / 60;
317 // Get value at hsl[1]
318 val = val + (hsl[2] - val) * (1.0 - hsl[1]);
320 return [val, val, val];