88 */
99import cssColors from './cssColors' ;
1010
11- // Inspiration : https://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hexadecimal-in-javascript
12- /* const getHexa = n => {
13- let n = _n;
14- if (n < 0) {
15- n = 0xffffffff + n + 1;
16- }
17- let hexa = `00000000${n.toString(16).toUpperCase()}`.substr(-8);
18- if (hexa.startsWith('00')) {
19- hexa = hexa.substring(2);
20- }
21- return hexa;
22- }; */
23-
2411const getCssHexa = ( n , alpha ) => {
25- // let hex = getHexa(n & 0xffffff);
2612 let hex = `00000000${ ( n & 0xffffff ) . toString ( 16 ) . toUpperCase ( ) } ` . substr ( - 6 ) ;
2713 if ( ! Number . isNaN ( alpha ) && alpha !== undefined ) {
2814 let a = alpha . toString ( 16 ) . toUpperCase ( ) ;
2915 if ( a . length === 1 ) a = `0${ a } ` ;
30- /* if (hex.length === 8) {
31- hex = hex.substring(2) + a;
32- } else {
33- hex += a;
34- } */
3516 hex += a ;
3617 }
3718 return hex ;
@@ -235,12 +216,6 @@ const getMinMax = rgb => {
235216} ;
236217
237218const getHsl = rgb => {
238- /* const r = rgb[0] / 255;
239- const g = rgb[1] / 255;
240- const b = rgb[2] / 255;
241- const cmin = Math.min(r, g, b);
242- const cmax = Math.max(r, g, b);
243- const delta = cmax - cmin; */
244219 const { cmin, cmax, delta, r, g, b } = getMinMax ( rgb ) ;
245220 let h = 0 ;
246221 let s = 0 ;
@@ -264,11 +239,6 @@ const getHsl = rgb => {
264239} ;
265240
266241const getHsv = rgb => {
267- /* const r = rgb[0] / 255;
268- const g = rgb[1] / 255;
269- const b = rgb[2] / 255;
270- const max = Math.max(r, g, b);
271- const min = Math.min(r, g, b); */
272242 const { cmax, delta, r, g, b } = getMinMax ( rgb ) ;
273243 if ( delta === 0 ) return [ 0 , 0 , Math . round ( cmax * 100 ) ] ;
274244
@@ -417,31 +387,62 @@ const getCssColor = (color, format, noAlpha) => {
417387 return value ;
418388} ;
419389
420- const validateColor = _color => ( _color && _color . format && _color . name ? _color : parse ( _color ) ) ;
390+ let cssColorsTranslated ;
391+ let language ;
392+
393+ const validateColor = ( _color , translate ) => {
394+ let color = _color ;
395+ let isTranslated = false ;
396+ if ( ! ( _color && _color . format && _color . name ) ) {
397+ color = _color ;
398+ if ( translate && typeof color === 'string' ) {
399+ if ( ! cssColorsTranslated || translate ( 'language' ) !== language ) {
400+ language = translate ( 'language' ) ;
401+ cssColorsTranslated = { } ;
402+ Object . keys ( cssColors ) . forEach ( name => {
403+ cssColorsTranslated [ translate ( name ) ] = name ;
404+ } ) ;
405+ }
406+ color = cssColorsTranslated [ color ] || color ;
407+ isTranslated = color !== _color ;
408+ }
409+ color = parse ( color ) ;
410+ if ( color . name && translate ) {
411+ color . translated = translate ( color . name ) ;
412+ if ( isTranslated && color . translated ) {
413+ color . name = color . translated ;
414+ }
415+ if ( color . error ) color . error = translate ( color . error ) ;
416+ }
417+ } else if ( color . error && translate ) {
418+ color . error = translate ( color . error ) ;
419+ }
420+ return color ;
421+ } ;
421422
422- const getComponents = ( _color , format ) => {
423- const color = validateColor ( _color ) ;
423+ const getComponents = ( _color , format , translate = value => value ) => {
424+ const color = validateColor ( _color , translate ) ;
424425 const components = { } ;
425426 if ( format === 'rgb' ) {
426- components . r = { value : color . rgb [ 0 ] , format : 'integer' , min : 0 , max : 255 , name : 'R' } ;
427- components . g = { value : color . rgb [ 1 ] , format : 'integer' , min : 0 , max : 255 , name : 'G' } ;
428- components . b = { value : color . rgb [ 2 ] , format : 'integer' , min : 0 , max : 255 , name : 'B' } ;
427+ components . r = { value : color . rgb [ 0 ] , format : 'integer' , min : 0 , max : 255 , name : translate ( 'R' ) } ;
428+ components . g = { value : color . rgb [ 1 ] , format : 'integer' , min : 0 , max : 255 , name : translate ( 'G' ) } ;
429+ components . b = { value : color . rgb [ 2 ] , format : 'integer' , min : 0 , max : 255 , name : translate ( 'B' ) } ;
429430 } else if ( format === 'hsv' ) {
430- components . h = { value : color . hsv [ 0 ] , format : 'integer' , min : 0 , max : 360 , name : 'H' , unit : '°' } ;
431- components . s = { value : color . hsv [ 1 ] , format : 'integer' , min : 0 , max : 100 , name : 'S' , unit : '%' } ;
432- components . v = { value : color . hsv [ 2 ] , format : 'integer' , min : 0 , max : 100 , name : 'V' , unit : '%' } ;
431+ components . h = { value : color . hsv [ 0 ] , format : 'integer' , min : 0 , max : 360 , name : translate ( 'H' ) , unit : '°' } ;
432+ components . s = { value : color . hsv [ 1 ] , format : 'integer' , min : 0 , max : 100 , name : translate ( 'S' ) , unit : '%' } ;
433+ components . v = { value : color . hsv [ 2 ] , format : 'integer' , min : 0 , max : 100 , name : translate ( 'V' ) , unit : '%' } ;
433434 } else if ( format === 'hsl' ) {
434- components . h = { value : color . hsl [ 0 ] , format : 'integer' , min : 0 , max : 360 , name : 'H' , unit : '°' } ;
435- components . s = { value : color . hsl [ 1 ] , format : 'integer' , min : 0 , max : 100 , name : 'S' , unit : '%' } ;
436- components . l = { value : color . hsl [ 2 ] , format : 'integer' , min : 0 , max : 100 , name : 'L' , unit : '%' } ;
435+ components . h = { value : color . hsl [ 0 ] , format : 'integer' , min : 0 , max : 360 , name : translate ( 'H' ) , unit : '°' } ;
436+ components . s = { value : color . hsl [ 1 ] , format : 'integer' , min : 0 , max : 100 , name : translate ( 'S' ) , unit : '%' } ;
437+ components . l = { value : color . hsl [ 2 ] , format : 'integer' , min : 0 , max : 100 , name : translate ( 'L' ) , unit : '%' } ;
437438 } else if ( format === 'hex' ) {
438439 let { hex } = color ;
439440 if ( color . raw && typeof color . raw === 'string' && color . raw . startsWith ( '#' ) ) {
440441 hex = color . raw . substring ( 1 ) ;
441442 }
442- components . hex = { value : hex , format : 'hex' , name : 'HEX' , unit : '#' } ;
443+ components . hex = { value : hex , format : 'hex' , name : translate ( 'HEX' ) , unit : '#' } ;
443444 } else {
444- components . value = color . value ;
445+ components . value = translate ( color . value ) ;
445446 components . format = 'unknown' ;
446447 }
447448 return components ;
0 commit comments