1+ ( function ( ) {
2+ var $ = function ( id ) { return document . getElementById ( id ) } ;
3+ var ColorPicker = new ( function ( ) {
4+ var analyzeColor = function ( e ) {
5+ var color = e . target . value ;
6+ if ( e . target . id == "colorpicker-rgb" ) {
7+ if ( color . indexOf ( "rgb" ) < 0 ) {
8+ color = "rgb(" + color + ")" ;
9+ }
10+ }
11+ if ( e . target . id == "colorpicker-hsl" ) {
12+ if ( color . indexOf ( "hsl" ) < 0 ) {
13+ color = "hsl(" + color + ")" ;
14+ }
15+ }
16+ ColorPicker . color ( color ) ;
17+
18+ } ,
19+ shadeColor = function ( color , percent ) {
20+ var f = parseInt ( color . slice ( 1 ) , 16 ) , t = percent < 0 ?0 :255 , p = percent < 0 ?percent * - 1 :percent , R = f >> 16 , G = f >> 8 & 0x00FF , B = f & 0x0000FF ;
21+ return "#" + Math . round ( 0x1000000 + ( Math . round ( ( t - R ) * p ) + R ) * 0x10000 + ( Math . round ( ( t - G ) * p ) + G ) * 0x100 + ( Math . round ( ( t - B ) * p ) + B ) ) . toString ( 16 ) . slice ( 1 ) ;
22+ } ,
23+ toHex = function ( color ) {
24+ return "#" + Math . round ( ( 1 << 24 ) + ( color [ 0 ] << 16 ) + ( color [ 1 ] << 8 ) + color [ 2 ] ) . toString ( 16 ) . slice ( 1 ) ;
25+ } ,
26+ rangeColor = function ( ) {
27+ var color = "rgb(" ;
28+ color += $ ( "colorpicker-r" ) . value + "," ;
29+ color += $ ( "colorpicker-g" ) . value + "," ;
30+ color += $ ( "colorpicker-b" ) . value + ")" ;
31+ ColorPicker . color ( color ) ;
32+ } ,
33+ rgbToHsl = function ( r , g , b ) {
34+ r /= 255 , g /= 255 , b /= 255 ;
35+ var max = Math . max ( r , g , b ) , min = Math . min ( r , g , b ) ;
36+ var h , s , l = ( max + min ) / 2 ;
37+
38+ if ( max == min ) {
39+ h = s = 0 ; // achromatic
40+ } else {
41+ var d = ( max - min ) ;
42+ s = l >= 0.5 ? d / ( 2 - ( max + min ) ) : d / ( max + min ) ;
43+ switch ( max ) {
44+ case r : h = ( ( g - b ) / d + 0 ) * 60 ; break ;
45+ case g : h = ( ( b - r ) / d + 2 ) * 60 ; break ;
46+ case b : h = ( ( r - g ) / d + 4 ) * 60 ; break ;
47+ }
48+ }
49+ console . log ( h ) ;
50+ return [ h , s , l ] ;
51+ }
52+
53+ this . actualSettings = null ;
54+ this . color = function ( color ) {
55+ console . log ( color ) ;
56+ color = toRGBA ( color ) || [ 0 , 0 , 0 , 1 ] ;
57+ console . log ( color ) ;
58+ color = color . map ( function ( a ) { return a || 0 } )
59+ color . pop ( ) ;
60+ var rgb = "rgb(" + color . map ( Math . round ) . join ( ) + ")" ,
61+ hex = toHex ( color ) ,
62+ hsl = rgbToHsl ( color [ 0 ] , color [ 1 ] , color [ 2 ] ) ;
63+ hsl = "hsl(" + Math . round ( ( hsl [ 0 ] + 720 ) % 360 ) + "," + Math . round ( hsl [ 1 ] * 100 ) + "%," + Math . round ( hsl [ 2 ] * 100 ) + "%)" ;
64+ var name = ntc . name ( hex ) [ 1 ] ;
65+ $ ( "colorpicker-r" ) . value = color [ 0 ] ;
66+ $ ( "colorpicker-g" ) . value = color [ 1 ] ;
67+ $ ( "colorpicker-b" ) . value = color [ 2 ] ;
68+ $ ( "colorpicker-rgb" ) . value = rgb ;
69+ $ ( "colorpicker-hex" ) . value = hex ;
70+ $ ( "colorpicker-hsl" ) . value = hsl ;
71+ $ ( "colorpicker-container" ) . style . color = hex ;
72+ $ ( "colorpicker-name" ) . innerHTML = name ;
73+ $ ( "colorpicker-name1" ) . innerHTML = name ;
74+ $ ( "colorpicker-name2" ) . innerHTML = name ;
75+ }
76+ this . choose = function ( ) {
77+ if ( this . actualSettings ) {
78+ var rgba = this . actualRGBA ( ) ,
79+ hex = toHex ( rgba ) ;
80+ if ( this . actualSettings [ 0 ] . type ) {
81+ this . actualSettings [ 0 ] . value = hex ;
82+ }
83+ if ( this . actualSettings [ 1 ] ) {
84+ this . actualSettings [ 1 ] ( hex ) ;
85+ }
86+ }
87+ this . actualSettings = null ;
88+ jQuery ( "#colorpicker" ) . modal ( 'hide' ) ;
89+ }
90+ this . cancel = function ( ) {
91+ if ( this . actualSettings && this . actualSettings [ 1 ] ) {
92+ this . actualSettings [ 1 ] ( ) ;
93+ }
94+ this . actualSettings = null ;
95+ jQuery ( "#colorpicker" ) . modal ( 'hide' ) ;
96+ }
97+ this . get = function ( color , callback ) {
98+ color = color || "#000000" ;
99+ this . actualSettings = [ color , callback ] ;
100+ if ( color . nodeType ) {
101+ color = color . value ;
102+ }
103+ this . color ( color ) ;
104+ var element = $ ( "colorpicker-table-scheme" ) ;
105+ while ( element . firstChild ) {
106+ element . removeChild ( element . firstChild ) ;
107+ }
108+ var schemes = this . getTableColorScheme ( ) ,
109+ frag = document . createDocumentFragment ( ) ;
110+ for ( var i = 0 ; i < schemes . length ; i ++ ) {
111+ var div = document . createElement ( "div" ) ,
112+ scheme = "rgb(" + schemes [ i ] . join ( "," ) + ")" ;
113+ div . setAttribute ( "data-color" , scheme ) ;
114+ div . title = scheme ;
115+ div . style . backgroundColor = scheme ;
116+ div . addEventListener ( "click" , function ( ) {
117+ ColorPicker . color ( this . getAttribute ( "data-color" ) ) ;
118+ } , false ) ;
119+ frag . appendChild ( div ) ;
120+ }
121+ element . appendChild ( frag ) ;
122+ element = $ ( "colorpicker-default-scheme" ) ,
123+ frag = document . createDocumentFragment ( ) ;
124+ if ( ! element . firstElementChild ) {
125+ schemes = [ "white" , "silver" , "gray" , "black" ,
126+ "orange" , "red" , "maroon" , "yellow" , "olive" , "lime" ,
127+ "green" , "aqua" , "teal" , "blue" ,
128+ "navy" , "fuchsia" , "purple" ] ;
129+ for ( var i = 0 ; i < schemes . length ; i ++ ) {
130+ var div = document . createElement ( "div" ) ,
131+ scheme = schemes [ i ] ;
132+ div . setAttribute ( "data-color" , scheme ) ;
133+ div . title = scheme ;
134+ div . style . backgroundColor = scheme ;
135+ div . addEventListener ( "click" , function ( ) {
136+ ColorPicker . color ( this . getAttribute ( "data-color" ) ) ;
137+ } , false ) ;
138+ frag . appendChild ( div ) ;
139+ }
140+ }
141+ element . appendChild ( frag ) ;
142+ jQuery ( "#colorpicker" ) . modal ( 'show' ) ;
143+ }
144+ this . actualRGBA = function ( ) {
145+ return toRGBA ( $ ( "colorpicker-container" ) . style . color )
146+ }
147+ this . getTableColorScheme = function ( ) {
148+ var table = $ ( "table" ) ,
149+ colors = [ "#000000" , "#ffffff" ] ,
150+ alternate = window . table . oddEvenColors ( ) ;
151+ if ( alternate ) {
152+ colors . push ( alternate [ 1 ] , alternate [ 2 ] )
153+ }
154+ for ( var i = 0 ; i < table . rows . length ; i ++ ) {
155+ var cells = table . rows [ i ] . cells ;
156+ for ( var j = 0 ; j < cells . length ; j ++ ) {
157+ var cell = cells [ j ] , style = cell . style ;
158+ if ( style . backgroundColor ) {
159+ colors . push ( style . backgroundColor ) ;
160+ }
161+ if ( style . borderLeftColor ) {
162+ colors . push ( style . borderLeftColor ) ;
163+ }
164+ if ( style . borderTopColor ) {
165+ colors . push ( style . borderTopColor ) ;
166+ }
167+ if ( style . borderRightColor ) {
168+ colors . push ( style . borderRightColor ) ;
169+ }
170+ if ( style . borderBottomColor ) {
171+ colors . push ( style . borderBottomColor ) ;
172+ }
173+ }
174+ }
175+ var elements = table . querySelectorAll ( 'font[color], tr > * [style*="color"]' ) ;
176+ for ( var i = 0 ; i < elements . length ; i ++ ) {
177+ var element = elements [ i ] ;
178+ if ( element . hasAttribute ( "color" ) ) {
179+ colors . push ( element . getAttribute ( "color" ) ) ;
180+ }
181+ if ( element . style . color ) {
182+ colors . push ( element . style . color ) ;
183+ }
184+ }
185+ var col = { } ;
186+ for ( var i = 0 ; i < colors . length ; i ++ ) {
187+ var rgba = toRGBA ( colors [ i ] ) || [ 0 , 0 , 0 , 1 ] ;
188+ if ( rgba [ 3 ] === 0 ) { continue ; }
189+ rgba . pop ( ) ;
190+ col [ rgba . join ( "," ) ] = true ;
191+ }
192+ colors = [ ] ;
193+ for ( var i in col ) {
194+ if ( col . hasOwnProperty ( i ) ) {
195+ colors . push ( i . split ( "," ) ) ;
196+ }
197+ }
198+ return colors ;
199+ }
200+ this . shade = function ( p ) {
201+ var hex = toHex ( this . actualRGBA ( ) ) ;
202+ this . color ( shadeColor ( hex , p ) ) ;
203+ }
204+ this . init = function ( ) {
205+ $ ( "colorpicker-rgb" ) . addEventListener ( "blur" , analyzeColor , false ) ;
206+ $ ( "colorpicker-hex" ) . addEventListener ( "blur" , analyzeColor , false ) ;
207+ $ ( "colorpicker-hsl" ) . addEventListener ( "blur" , analyzeColor , false ) ;
208+ $ ( "colorpicker-r" ) . addEventListener ( "input" , rangeColor , false ) ;
209+ $ ( "colorpicker-g" ) . addEventListener ( "input" , rangeColor , false ) ;
210+ $ ( "colorpicker-b" ) . addEventListener ( "input" , rangeColor , false ) ;
211+ }
212+ } ) ( ) ;
213+ ColorPicker . init ( ) ;
214+ window . ColorPicker = ColorPicker ;
215+ } ) ( ) ;
0 commit comments