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