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