1+ document . getElementsByClassName ( 'color1_conversion' ) [ 0 ] &&
2+ document . getElementsByClassName ( 'color1_conversion' ) [ 0 ] . addEventListener ( 'click' , ( ) => {
3+ let hexcode = '#' ;
4+ const color = document . getElementsByClassName ( 'color1_input' ) [ 0 ] . value . split ( ', ' ) ;
5+ for ( x = 0 ; x < 3 ; x ++ ) {
6+ let n = Number ( color [ x ] ) ;
7+ if ( n == '' ) n = '0' ;
8+ if ( parseInt ( n ) != n ) return alert ( '请输入正确的数字!' ) ;
9+ if ( n > 255 ) return alert ( '数字在0-255之间!' ) ;
10+ let c = '0123456789ABCDEF' , b = '' , a = n % 16 ;
11+ b = c . substr ( a , 1 ) ;
12+ a = ( n - a ) / 16 ;
13+ hexcode += c . substr ( a , 1 ) + b ;
14+ }
15+ document . getElementById ( 'color1' ) . bgColor = res1 . innerHTML = hexcode ;
16+ } )
17+
18+ document . getElementsByClassName ( 'color2_conversion' ) [ 0 ] &&
19+ document . getElementsByClassName ( 'color2_conversion' ) [ 0 ] . addEventListener ( 'click' , ( ) => {
20+ let a = document . getElementsByClassName ( 'color2_input' ) [ 0 ] . value ;
21+ if ( a . substr ( 0 , 1 ) == '#' ) a = a . substring ( 1 ) ;
22+ const len = a . length ;
23+ if ( len != 6 && len != 3 ) return alert ( '十六进制颜色码为六位或三位!' ) ;
24+ if ( / [ ^ 0 - 9 a - f ] / i. test ( a ) ) return alert ( '请输入正确的十六进制颜色码!' ) ;
25+ document . getElementById ( 'color2' ) . bgColor = '#' + a ;
26+ a = a . toLowerCase ( ) ;
27+ b = new Array ( ) ;
28+ for ( x = 0 ; x < 3 ; x ++ ) {
29+ b [ 0 ] = len == 6 ? a . substr ( x * 2 , 2 ) : a . substr ( x * 1 , 1 ) + a . substr ( x * 1 , 1 ) ;
30+ b [ 3 ] = '0123456789abcdef' ;
31+ b [ 1 ] = b [ 0 ] . substr ( 0 , 1 ) ;
32+ b [ 2 ] = b [ 0 ] . substr ( 1 , 1 ) ;
33+ b [ 20 + x ] = b [ 3 ] . indexOf ( b [ 1 ] ) * 16 + b [ 3 ] . indexOf ( b [ 2 ] ) ;
34+ }
35+ res2 . innerHTML = b [ 20 ] + ', ' + b [ 21 ] + ', ' + b [ 22 ] ;
36+ } )
0 commit comments