22
33const isNumeric = require ( 'fast-isnumeric' ) ;
44const isTypedArray = require ( '../../lib/array' ) . isTypedArray ;
5- const color = require ( 'color' ) . default
5+ const color = require ( 'color' ) . default ;
66
77const { background, defaultLine, defaults, lightLine } = require ( './attributes' ) ;
88
9- const rgb = cstr => {
9+ const rgb = ( cstr ) => {
1010 const { r, g, b } = color ( cstr ) . rgb ( ) . object ( ) ;
1111 return `rgb(${ Math . round ( r ) } , ${ Math . round ( g ) } , ${ Math . round ( b ) } )` ;
12- }
12+ } ;
1313
14- const opacity = cstr => cstr ? color ( cstr ) . alpha ( ) : 0 ;
14+ const opacity = ( cstr ) => ( cstr ? color ( cstr ) . alpha ( ) : 0 ) ;
1515
1616const addOpacity = ( cstr , op ) => {
1717 const c = color ( cstr ) . rgb ( ) . object ( ) ;
@@ -24,18 +24,19 @@ const addOpacity = (cstr, op) => {
2424const combine = ( front , back = background ) => {
2525 const fc = color ( front ) . rgb ( ) . object ( ) ;
2626 fc . alpha ||= 1 ;
27- if ( fc . alpha === 1 ) return color ( front ) . rgb ( ) . string ( ) ;
27+ if ( fc . alpha === 1 ) return color ( front ) . rgb ( ) . string ( ) ;
2828
2929 const bc = color ( back ) . rgb ( ) . object ( ) ;
3030 bc . alpha ||= 1 ;
31- const bcflat = bc . alpha === 1
32- ? bc
33- : {
34- r : 255 * ( 1 - bc . alpha ) + bc . r * bc . alpha ,
35- g : 255 * ( 1 - bc . alpha ) + bc . g * bc . alpha ,
36- b : 255 * ( 1 - bc . alpha ) + bc . b * bc . alpha
37- } ;
38-
31+ const bcflat =
32+ bc . alpha === 1
33+ ? bc
34+ : {
35+ r : 255 * ( 1 - bc . alpha ) + bc . r * bc . alpha ,
36+ g : 255 * ( 1 - bc . alpha ) + bc . g * bc . alpha ,
37+ b : 255 * ( 1 - bc . alpha ) + bc . b * bc . alpha
38+ } ;
39+
3940 const fcflat = {
4041 r : bcflat . r * ( 1 - fc . alpha ) + fc . r * fc . alpha ,
4142 g : bcflat . g * ( 1 - fc . alpha ) + fc . g * fc . alpha ,
@@ -58,7 +59,7 @@ const interpolate = (first, second, factor) => {
5859 const ic = {
5960 r : factor * fc . r + ( 1 - factor ) * sc . r ,
6061 g : factor * fc . g + ( 1 - factor ) * sc . g ,
61- b : factor * fc . b + ( 1 - factor ) * sc . b ,
62+ b : factor * fc . b + ( 1 - factor ) * sc . b
6263 } ;
6364
6465 return color ( ic ) . rgb ( ) . string ( ) ;
@@ -73,15 +74,19 @@ const interpolate = (first, second, factor) => {
7374 * otherwise we go all the way to white or black.
7475 */
7576const contrast = ( cstr , lightAmount , darkAmount ) => {
76- let c = color ( cstr )
77+ let c = color ( cstr ) ;
7778
78- if ( c . alpha ( ) !== 1 ) c = color ( combine ( cstr , background ) ) ;
79+ if ( c . alpha ( ) !== 1 ) c = color ( combine ( cstr , background ) ) ;
7980
8081 // TODO: Should the API change such that lightAmount/darkAmount are passed in as decimal instead of percent number?
8182 const newColor = color (
8283 c . isDark ( )
83- ? ( lightAmount ? c . lighten ( lightAmount / 100 ) : background )
84- : ( darkAmount ? c . darken ( darkAmount / 100 ) : defaultLine )
84+ ? lightAmount
85+ ? c . lighten ( lightAmount / 100 )
86+ : background
87+ : darkAmount
88+ ? c . darken ( darkAmount / 100 )
89+ : defaultLine
8590 ) ;
8691
8792 return newColor . rgb ( ) . string ( ) ;
@@ -93,89 +98,94 @@ const fill = (s, cstr) => s.style({ fill: rgb(cstr), 'fill-opacity': opacity(cst
9398
9499// search container for colors with the deprecated rgb(fractions) format
95100// and convert them to rgb(0-255 values)
96- const clean = container => {
97- if ( ! container || typeof container !== 'object' ) return ;
101+ const clean = ( container ) => {
102+ if ( ! container || typeof container !== 'object' ) return ;
98103
99104 var keys = Object . keys ( container ) ;
100105 var i , j , key , val ;
101106
102- for ( i = 0 ; i < keys . length ; i ++ ) {
107+ for ( i = 0 ; i < keys . length ; i ++ ) {
103108 key = keys [ i ] ;
104109 val = container [ key ] ;
105110
106- if ( key . substr ( key . length - 5 ) === 'color' ) {
111+ if ( key . substr ( key . length - 5 ) === 'color' ) {
107112 // only sanitize keys that end in "color" or "colorscale"
108113
109- if ( Array . isArray ( val ) ) {
110- for ( j = 0 ; j < val . length ; j ++ ) val [ j ] = cleanOne ( val [ j ] ) ;
114+ if ( Array . isArray ( val ) ) {
115+ for ( j = 0 ; j < val . length ; j ++ ) val [ j ] = cleanOne ( val [ j ] ) ;
111116 } else container [ key ] = cleanOne ( val ) ;
112- } else if ( key . substr ( key . length - 10 ) === 'colorscale' && Array . isArray ( val ) ) {
117+ } else if ( key . substr ( key . length - 10 ) === 'colorscale' && Array . isArray ( val ) ) {
113118 // colorscales have the format [[0, color1], [frac, color2], ... [1, colorN]]
114119
115- for ( j = 0 ; j < val . length ; j ++ ) {
116- if ( Array . isArray ( val [ j ] ) ) val [ j ] [ 1 ] = cleanOne ( val [ j ] [ 1 ] ) ;
120+ for ( j = 0 ; j < val . length ; j ++ ) {
121+ if ( Array . isArray ( val [ j ] ) ) val [ j ] [ 1 ] = cleanOne ( val [ j ] [ 1 ] ) ;
117122 }
118- } else if ( Array . isArray ( val ) ) {
123+ } else if ( Array . isArray ( val ) ) {
119124 // recurse into arrays of objects, and plain objects
120125
121126 var el0 = val [ 0 ] ;
122- if ( ! Array . isArray ( el0 ) && el0 && typeof el0 === 'object' ) {
123- for ( j = 0 ; j < val . length ; j ++ ) clean ( val [ j ] ) ;
127+ if ( ! Array . isArray ( el0 ) && el0 && typeof el0 === 'object' ) {
128+ for ( j = 0 ; j < val . length ; j ++ ) clean ( val [ j ] ) ;
124129 }
125- } else if ( val && typeof val === 'object' && ! isTypedArray ( val ) ) clean ( val ) ;
130+ } else if ( val && typeof val === 'object' && ! isTypedArray ( val ) ) clean ( val ) ;
126131 }
127132} ;
128133
129- const cleanOne = val => {
130- if ( isNumeric ( val ) || typeof val !== 'string' ) return val ;
134+ const cleanOne = ( val ) => {
135+ if ( isNumeric ( val ) || typeof val !== 'string' ) return val ;
131136
132137 var valTrim = val . trim ( ) ;
133- if ( valTrim . substr ( 0 , 3 ) !== 'rgb' ) return val ;
138+ if ( valTrim . substr ( 0 , 3 ) !== 'rgb' ) return val ;
134139
135140 var match = valTrim . match ( / ^ r g b a ? \s * \( ( [ ^ ( ) ] * ) \) $ / ) ;
136- if ( ! match ) return val ;
141+ if ( ! match ) return val ;
137142
138143 var parts = match [ 1 ] . trim ( ) . split ( / \s * [ \s , ] \s * / ) ;
139144 var rgba = valTrim . charAt ( 3 ) === 'a' && parts . length === 4 ;
140- if ( ! rgba && parts . length !== 3 ) return val ;
145+ if ( ! rgba && parts . length !== 3 ) return val ;
141146
142- for ( var i = 0 ; i < parts . length ; i ++ ) {
143- if ( ! parts [ i ] . length ) return val ;
147+ for ( var i = 0 ; i < parts . length ; i ++ ) {
148+ if ( ! parts [ i ] . length ) return val ;
144149 parts [ i ] = Number ( parts [ i ] ) ;
145150
146- if ( ! ( parts [ i ] >= 0 ) ) {
151+ if ( ! ( parts [ i ] >= 0 ) ) {
147152 // all parts must be non-negative numbers
148153
149154 return val ;
150155 }
151156
152- if ( i === 3 ) {
157+ if ( i === 3 ) {
153158 // alpha>1 gets clipped to 1
154159
155- if ( parts [ i ] > 1 ) parts [ i ] = 1 ;
156- } else if ( parts [ i ] >= 1 ) {
160+ if ( parts [ i ] > 1 ) parts [ i ] = 1 ;
161+ } else if ( parts [ i ] >= 1 ) {
157162 // r, g, b must be < 1 (ie 1 itself is not allowed)
158163
159164 return val ;
160165 }
161166 }
162167
163- var rgbStr = Math . round ( parts [ 0 ] * 255 ) + ', ' +
164- Math . round ( parts [ 1 ] * 255 ) + ', ' +
165- Math . round ( parts [ 2 ] * 255 ) ;
168+ var rgbStr = Math . round ( parts [ 0 ] * 255 ) + ', ' + Math . round ( parts [ 1 ] * 255 ) + ', ' + Math . round ( parts [ 2 ] * 255 ) ;
166169
167- if ( rgba ) return 'rgba(' + rgbStr + ', ' + parts [ 3 ] + ')' ;
170+ if ( rgba ) return 'rgba(' + rgbStr + ', ' + parts [ 3 ] + ')' ;
168171 return 'rgb(' + rgbStr + ')' ;
169- }
172+ } ;
170173
171174const equals = ( cstr1 , cstr2 ) => cstr1 && cstr2 && color ( cstr1 ) . rgb ( ) . string ( ) === color ( cstr2 ) . rgb ( ) . string ( ) ;
172175
173- const isValid = cstr => {
174- try { return cstr && ! ! color ( cstr ) ; }
175- catch { return false ; }
176- }
176+ const isValid = ( cstr ) => {
177+ try {
178+ return cstr && ! ! color ( cstr ) ;
179+ } catch {
180+ return false ;
181+ }
182+ } ;
177183
178- const mix = ( cstr1 , cstr2 , weight ) => color ( cstr1 ) . mix ( color ( cstr2 ) , weight / 100 ) . rgb ( ) . string ( ) ;
184+ const mix = ( cstr1 , cstr2 , weight ) =>
185+ color ( cstr1 )
186+ . mix ( color ( cstr2 ) , weight / 100 )
187+ . rgb ( )
188+ . string ( ) ;
179189
180190const mostReadable = ( baseColor , colorList = [ ] ) => {
181191 let bestColor ;
@@ -188,11 +198,11 @@ const mostReadable = (baseColor, colorList = []) => {
188198 bestColor = color ( cstr ) . rgb ( ) . string ( ) ;
189199 }
190200 }
191-
201+
192202 // Fall back to black/white if provided colors don't have proper contrast level
193203 return bestColor && color ( baseColor ) . level ( color ( bestColor ) )
194204 ? bestColor
195- : mostReadable ( baseColor , [ " #000" , " #fff" ] ) ;
205+ : mostReadable ( baseColor , [ ' #000' , ' #fff' ] ) ;
196206} ;
197207
198208module . exports = {
@@ -214,4 +224,4 @@ module.exports = {
214224 opacity,
215225 rgb,
216226 stroke
217- }
227+ } ;
0 commit comments