1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2 //>>description: Color code converter
3 //>>label: Color library
6 define( [ 'util/ensurens' ], function ( ) {
7 //>>excludeEnd("jqmBuildExclude");
9 ensureNS("jQuery.mobile.tizen.clrlib");
11 jQuery.extend( jQuery.mobile.tizen.clrlib,
13 nearestInt: function(val) {
14 var theFloor = Math.floor(val);
16 return (((val - theFloor) > 0.5) ? (theFloor + 1) : theFloor);
20 * Converts html color string to rgb array.
22 * Input: string clr_str, where
23 * clr_str is of the form "#aabbcc"
25 * Returns: [ r, g, b ], where
30 HTMLToRGB: function(clr_str) {
31 clr_str = (('#' == clr_str.charAt(0)) ? clr_str.substring(1) : clr_str);
34 clr_str.substring(0, 2),
35 clr_str.substring(2, 4),
36 clr_str.substring(4, 6)
38 return parseInt(val, 16) / 255.0;
43 * Converts rgb array to html color string.
45 * Input: [ r, g, b ], where
50 * Returns: string of the form "#aabbcc"
52 RGBToHTML: function(rgb) {
54 rgb.map(function(val) {
56 theFloor = Math.floor(ret);
58 ret = ((ret - theFloor > 0.5) ? (theFloor + 1) : theFloor);
59 ret = (((ret < 16) ? "0" : "") + (ret & 0xff).toString(16));
66 * Converts hsl to rgb.
68 * From http://130.113.54.154/~monger/hsl-rgb.html
70 * Input: [ h, s, l ], where
75 * Returns: [ r, g, b ], where
80 HSLToRGB: function(hsl) {
81 var h = hsl[0] / 360.0, s = hsl[1], l = hsl[2];
86 var temp2 = ((l < 0.5)
89 temp1 = 2.0 * l - temp2,
96 temp3.r = ((temp3.r < 0) ? (temp3.r + 1.0) : ((temp3.r > 1) ? (temp3.r - 1.0) : temp3.r));
97 temp3.g = ((temp3.g < 0) ? (temp3.g + 1.0) : ((temp3.g > 1) ? (temp3.g - 1.0) : temp3.g));
98 temp3.b = ((temp3.b < 0) ? (temp3.b + 1.0) : ((temp3.b > 1) ? (temp3.b - 1.0) : temp3.b));
101 (((6.0 * temp3.r) < 1) ? (temp1 + (temp2 - temp1) * 6.0 * temp3.r) :
102 (((2.0 * temp3.r) < 1) ? temp2 :
103 (((3.0 * temp3.r) < 2) ? (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - temp3.r) * 6.0) :
105 (((6.0 * temp3.g) < 1) ? (temp1 + (temp2 - temp1) * 6.0 * temp3.g) :
106 (((2.0 * temp3.g) < 1) ? temp2 :
107 (((3.0 * temp3.g) < 2) ? (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - temp3.g) * 6.0) :
109 (((6.0 * temp3.b) < 1) ? (temp1 + (temp2 - temp1) * 6.0 * temp3.b) :
110 (((2.0 * temp3.b) < 1) ? temp2 :
111 (((3.0 * temp3.b) < 2) ? (temp1 + (temp2 - temp1) * ((2.0 / 3.0) - temp3.b) * 6.0) :
118 * Converts hsv to rgb.
120 * Input: [ h, s, v ], where
125 * Returns: [ r, g, b ], where
130 HSVToRGB: function(hsv) {
131 return $.mobile.tizen.clrlib.HSLToRGB($.mobile.tizen.clrlib.HSVToHSL(hsv));
135 * Converts rgb to hsv.
137 * from http://coecsl.ece.illinois.edu/ge423/spring05/group8/FinalProject/HSV_writeup.pdf
139 * Input: [ r, g, b ], where
144 * Returns: [ h, s, v ], where
149 RGBToHSV: function(rgb) {
150 var min, max, delta, h, s, v, r = rgb[0], g = rgb[1], b = rgb[2];
152 min = Math.min(r, Math.min(g, b));
153 max = Math.max(r, Math.max(g, b));
160 if (delta > 0.00001) {
164 h = (g - b) / delta ;
167 h = 2 + (b - r) / delta ;
169 h = 4 + (r - g) / delta ;
181 * Converts hsv to hsl.
183 * Input: [ h, s, v ], where
188 * Returns: [ h, s, l ], where
193 HSVToHSL: function(hsv) {
195 delta = hsv[1] * max,
199 s_divisor = ((half_sum < 0.5) ? sum : (2 - max - min));
201 return [ hsv[0], ((0 == s_divisor) ? 0 : (delta / s_divisor)), half_sum ];
205 * Converts rgb to hsl
207 * Input: [ r, g, b ], where
212 * Returns: [ h, s, l ], where
217 RGBToHSL: function(rgb) {
218 return $.mobile.tizen.clrlib.HSVToHSL($.mobile.tizen.clrlib.RGBToHSV(rgb));
222 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
224 //>>excludeEnd("jqmBuildExclude");