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 : $([]),
65 dst.add( toplevel.find( sel ) )
68 el.css( el.jqmData( "cssProp" ), el.jqmData( self.options.disabled ? "dclr" : "clr" ) );
71 _isValidColorCode: function( value ) {
72 return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test( value );
74 _setColor: function ( value ) {
75 var currentValue = ( this.options.color ),
78 if( !self._isValidColorCode( value ) )
80 console.log( " Color code is invalid " );
84 value = value.match(/#[0-9A-Fa-f]{6}/)
86 : currentValue.match(/#[0-9A-Fa-f]{6}/)
88 : $.tizen.colorwidget.prototype.options.color;
90 if ( this.options.color !== value ) {
91 this.options.color = value;
92 this._setValue( value );
99 $.tizen.colorwidget.clrlib = {
100 nearestInt: function ( val ) {
101 var theFloor = Math.floor( val );
103 return ( ( ( val - theFloor ) > 0.5 ) ? ( theFloor + 1 ) : theFloor );
106 // Converts html color string to rgb array.
108 // Input: string clr_str, where
109 // clr_str is of the form "#aabbcc"
111 // Returns: [ r, g, b ], where
115 HTMLToRGB: function ( clr_str ) {
116 clr_str = ( ( '#' == clr_str.charAt( 0 ) ) ? clr_str.substring( 1 ) : clr_str );
118 return [ parseInt(clr_str.substring(0, 2), 16) / 255.0,
119 parseInt(clr_str.substring(2, 4), 16) / 255.0,
120 parseInt(clr_str.substring(4, 6), 16) / 255.0 ];
123 // Converts rgb array to html color string.
125 // Input: [ r, g, b ], where
130 // Returns: string of the form "#aabbcc"
131 RGBToHTML: function ( rgb ) {
132 var ret = "#", val, theFloor,
135 val = rgb[Nix] * 255;
136 theFloor = Math.floor( val );
137 val = ( ( val - theFloor > 0.5 ) ? ( theFloor + 1 ) : theFloor );
138 ret = ret + ( ( ( val < 16 ) ? "0" : "" ) + ( val & 0xff ).toString( 16 ) );
144 // Converts hsl to rgb.
146 // From http://130.113.54.154/~monger/hsl-rgb.html
148 // Input: [ h, s, l ], where
153 // Returns: [ r, g, b ], where
157 HSLToRGB: function ( hsl ) {
158 var h = hsl[0] / 360.0, s = hsl[1], l = hsl[2],
168 temp2 = ( ( l < 0.5 )
172 temp1 = 2.0 * l - temp2;
179 temp3.r = ( ( temp3.r < 0 ) ? ( temp3.r + 1.0 ) : ( ( temp3.r > 1 ) ? ( temp3.r - 1.0 ) : temp3.r ) );
180 temp3.g = ( ( temp3.g < 0 ) ? ( temp3.g + 1.0 ) : ( ( temp3.g > 1 ) ? ( temp3.g - 1.0 ) : temp3.g ) );
181 temp3.b = ( ( temp3.b < 0 ) ? ( temp3.b + 1.0 ) : ( ( temp3.b > 1 ) ? ( temp3.b - 1.0 ) : temp3.b ) );
183 ret = [( ( ( 6.0 * temp3.r ) < 1 ) ? ( temp1 + ( temp2 - temp1 ) * 6.0 * temp3.r ) :
184 ( ( ( 2.0 * temp3.r ) < 1 ) ? temp2 :
185 ( ( ( 3.0 * temp3.r ) < 2 ) ? ( temp1 + ( temp2 - temp1 ) * ( ( 2.0 / 3.0 ) - temp3.r ) * 6.0 ) :
187 ( ( ( 6.0 * temp3.g ) < 1) ? ( temp1 + ( temp2 - temp1 ) * 6.0 * temp3.g ) :
188 ( ( ( 2.0 * temp3.g ) < 1 ) ? temp2 :
189 ( ( ( 3.0 * temp3.g ) < 2 ) ? ( temp1 + ( temp2 - temp1 ) * ( ( 2.0 / 3.0 ) - temp3.g ) * 6.0 ) :
191 ( ( ( 6.0 * temp3.b ) < 1 ) ? ( temp1 + ( temp2 - temp1 ) * 6.0 * temp3.b ) :
192 ( ( ( 2.0 * temp3.b ) < 1 ) ? temp2 :
193 ( ( ( 3.0 * temp3.b ) < 2 ) ? ( temp1 + ( temp2 - temp1 ) * ( ( 2.0 / 3.0 ) - temp3.b ) * 6.0 ) :
199 // Converts hsv to rgb.
201 // Input: [ h, s, v ], where
206 // Returns: [ r, g, b ], where
210 HSVToRGB: function ( hsv ) {
211 return $.tizen.colorwidget.clrlib.HSLToRGB( $.tizen.colorwidget.clrlib.HSVToHSL( hsv ) );
214 // Converts rgb to hsv.
216 // from http://coecsl.ece.illinois.edu/ge423/spring05/group8/FinalProject/HSV_writeup.pdf
218 // Input: [ r, g, b ], where
223 // Returns: [ h, s, v ], where
227 RGBToHSV: function ( rgb ) {
228 var min, max, delta, h, s, v, r = rgb[0], g = rgb[1], b = rgb[2];
230 min = Math.min( r, Math.min( g, b ) );
231 max = Math.max( r, Math.max( g, b ) );
238 if ( delta > 0.00001 ) {
242 h = ( g - b ) / delta;
245 h = 2 + ( b - r ) / delta;
247 h = 4 + ( r - g ) / delta;
260 // Converts hsv to hsl.
262 // Input: [ h, s, v ], where
267 // Returns: [ h, s, l ], where
271 HSVToHSL: function ( hsv ) {
273 delta = hsv[1] * max,
277 s_divisor = ( ( half_sum < 0.5 ) ? sum : ( 2 - max - min ) );
279 return [ hsv[0], ( ( 0 == s_divisor ) ? 0 : ( delta / s_divisor ) ), half_sum ];
282 // Converts rgb to hsl
284 // Input: [ r, g, b ], where
289 // Returns: [ h, s, l ], where
293 RGBToHSL: function ( rgb ) {
294 return $.tizen.colorwidget.clrlib.HSVToHSL( $.tizen.colorwidget.clrlib.RGBToHSV( rgb ) );
297 // Converts hsl to grayscale
298 // Full-saturation magic grayscale values were taken from the Gimp
300 // Input: [ h, s, l ], where
305 // Returns: [ r, g, b ], where
309 HSLToGray: function ( hsl ) {
310 var intrinsic_vals = [0.211764706, 0.929411765, 0.71372549, 0.788235294, 0.070588235, 0.28627451, 0.211764706],
311 idx = Math.floor(hsl[0] / 60),
319 begVal = intrinsic_vals[idx];
320 endVal = intrinsic_vals[idx + 1];
323 if ( hsl[2] < 0.5 ) {
324 lowerHalfPercent = hsl[2] * 2;
325 begVal *= lowerHalfPercent;
326 endVal *= lowerHalfPercent;
328 upperHalfPercent = ( hsl[2] - 0.5 ) * 2;
329 begVal += ( 1.0 - begVal ) * upperHalfPercent;
330 endVal += ( 1.0 - endVal ) * upperHalfPercent;
333 // This is the gray value at full sat, whereas hsl[2] is the gray value at 0 sat.
334 val = begVal + ( ( endVal - begVal ) * ( hsl[0] - ( idx * 60 ) ) ) / 60;
336 // Get value at hsl[1]
337 val = val + ( hsl[2] - val ) * ( 1.0 - hsl[1] );
339 return [val, val, val];