UnitTC: Additional unit testcases have been added
[platform/framework/web/web-ui-fw.git] / src / js / jquery.mobile.tizen.clrlib.js
1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2 //>>description: Color code converter
3 //>>label: Color library
4 //>>group: Tizen:Core
5
6 define( [ ], function ( ) {
7 //>>excludeEnd("jqmBuildExclude");
8
9 ensureNS("jQuery.mobile.tizen.clrlib");
10
11 jQuery.extend( jQuery.mobile.tizen.clrlib, 
12 {
13     nearestInt: function(val) { 
14         var theFloor = Math.floor(val);
15
16         return (((val - theFloor) > 0.5) ? (theFloor + 1) : theFloor);
17     },
18
19     /*
20      * Converts html color string to rgb array.
21      *
22      * Input: string clr_str, where
23      * clr_str is of the form "#aabbcc"
24      *
25      * Returns: [ r, g, b ], where
26      * r is in [0, 1]
27      * g is in [0, 1]
28      * b is in [0, 1]
29      */
30     HTMLToRGB: function(clr_str) {
31         clr_str = (('#' == clr_str.charAt(0)) ? clr_str.substring(1) : clr_str);
32
33         return ([
34             clr_str.substring(0, 2),
35             clr_str.substring(2, 4),
36             clr_str.substring(4, 6)
37             ].map(function(val) {
38                 return parseInt(val, 16) / 255.0;
39             }));
40     },
41
42     /*
43      * Converts rgb array to html color string.
44      *
45      * Input: [ r, g, b ], where
46      * r is in [0, 1]
47      * g is in [0, 1]
48      * b is in [0, 1]
49      *
50      * Returns: string of the form "#aabbcc"
51      */
52     RGBToHTML: function(rgb) {
53         return ("#" + 
54             rgb.map(function(val) {
55                       var ret = val * 255,
56                           theFloor = Math.floor(ret);
57
58                       ret = ((ret - theFloor > 0.5) ? (theFloor + 1) : theFloor);
59                       ret = (((ret < 16) ? "0" : "") + (ret & 0xff).toString(16));
60                       return ret;
61                   })
62                .join(""));
63     },
64
65     /*
66      * Converts hsl to rgb.
67      *
68      * From http://130.113.54.154/~monger/hsl-rgb.html
69      *
70      * Input: [ h, s, l ], where
71      * h is in [0, 360]
72      * s is in [0,   1]
73      * l is in [0,   1]
74      *
75      * Returns: [ r, g, b ], where
76      * r is in [0, 1]
77      * g is in [0, 1]
78      * b is in [0, 1]
79      */
80     HSLToRGB: function(hsl) {
81         var h = hsl[0] / 360.0, s = hsl[1], l = hsl[2];
82
83         if (0 === s)
84             return [ l, l, l ];
85
86         var temp2 = ((l < 0.5)
87                 ? l * (1.0 + s)
88                 : l + s - l * s),
89             temp1 = 2.0 * l - temp2,
90             temp3 = {
91                 r: h + 1.0 / 3.0,
92                 g: h,
93                 b: h - 1.0 / 3.0
94             };
95
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));
99
100         ret = [
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) :
104              temp1))),
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) :
108              temp1))),
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) :
112              temp1)))]; 
113
114         return ret;
115     },
116
117     /*
118      * Converts hsv to rgb.
119      *
120      * Input: [ h, s, v ], where
121      * h is in [0, 360]
122      * s is in [0,   1]
123      * v is in [0,   1]
124      *
125      * Returns: [ r, g, b ], where
126      * r is in [0, 1]
127      * g is in [0, 1]
128      * b is in [0, 1]
129      */
130     HSVToRGB: function(hsv) {
131         return $.mobile.tizen.clrlib.HSLToRGB($.mobile.tizen.clrlib.HSVToHSL(hsv));
132     },
133
134     /*
135      * Converts rgb to hsv.
136      *
137      * from http://coecsl.ece.illinois.edu/ge423/spring05/group8/FinalProject/HSV_writeup.pdf
138      *
139      * Input: [ r, g, b ], where
140      * r is in [0,   1]
141      * g is in [0,   1]
142      * b is in [0,   1]
143      *
144      * Returns: [ h, s, v ], where
145      * h is in [0, 360]
146      * s is in [0,   1]
147      * v is in [0,   1]
148      */
149     RGBToHSV: function(rgb) {
150         var min, max, delta, h, s, v, r = rgb[0], g = rgb[1], b = rgb[2];
151
152         min = Math.min(r, Math.min(g, b));
153         max = Math.max(r, Math.max(g, b));
154         delta = max - min;
155
156         h = 0;
157         s = 0;
158         v = max;
159
160         if (delta > 0.00001) {
161             s = delta / max;
162
163             if (r === max)
164                 h = (g - b) / delta ;
165             else
166             if (g === max)
167                 h = 2 + (b - r) / delta ;
168             else
169                 h = 4 + (r - g) / delta ;
170
171             h *= 60 ;
172
173             if (h < 0)
174                 h += 360 ;
175         }
176
177         return [h, s, v];
178     },
179
180     /*
181      * Converts hsv to hsl.
182      *
183      * Input: [ h, s, v ], where
184      * h is in [0, 360]
185      * s is in [0,   1]
186      * v is in [0,   1]
187      *
188      * Returns: [ h, s, l ], where
189      * h is in [0, 360]
190      * s is in [0,   1]
191      * l is in [0,   1]
192      */
193     HSVToHSL: function(hsv) {
194         var max = hsv[2],
195             delta = hsv[1] * max,
196             min = max - delta,
197             sum = max + min,
198             half_sum = sum / 2,
199             s_divisor = ((half_sum < 0.5) ? sum : (2 - max - min));
200
201         return [ hsv[0], ((0 == s_divisor) ? 0 : (delta / s_divisor)), half_sum ];
202     },
203
204     /*
205      * Converts rgb to hsl
206      *
207      * Input: [ r, g, b ], where
208      * r is in [0,   1]
209      * g is in [0,   1]
210      * b is in [0,   1]
211      *
212      * Returns: [ h, s, l ], where
213      * h is in [0, 360]
214      * s is in [0,   1]
215      * l is in [0,   1]
216      */
217     RGBToHSL: function(rgb) {
218         return $.mobile.tizen.clrlib.HSVToHSL($.mobile.tizen.clrlib.RGBToHSV(rgb));
219     }
220 });
221
222 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
223 } );
224 //>>excludeEnd("jqmBuildExclude");