@@ -353,15 +353,13 @@ export default class MasterCSS {
353353 variable . name = flatName
354354 if ( groups . length )
355355 variable . group = groups . join ( '.' )
356- if ( variable . type === 'color' ) {
357- if ( alpha ) {
358- const slashIndex = variable . value . indexOf ( '/' )
359- variable = {
360- ...variable ,
361- value : slashIndex === - 1
362- ? variable . value + ' / ' + ( alpha . startsWith ( '0.' ) ? alpha . slice ( 1 ) : alpha )
363- : ( variable . value . slice ( 0 , slashIndex + 2 ) + String ( + variable . value . slice ( slashIndex + 2 ) * + alpha ) . slice ( 1 ) )
364- }
356+ if ( variable . type === 'color' && variable . value && alpha ) {
357+ const slashIndex = variable . value . indexOf ( '/' )
358+ variable = {
359+ ...variable ,
360+ value : slashIndex === - 1
361+ ? variable . value + '/' + ( alpha . startsWith ( '0.' ) ? alpha . slice ( 1 ) : alpha )
362+ : ( variable . value . slice ( 0 , slashIndex + 1 ) + String ( + variable . value . slice ( slashIndex + 1 ) * + alpha ) . slice ( 1 ) )
365363 }
366364 }
367365 const currentMode = replacedMode ?? mode
@@ -472,23 +470,54 @@ export default class MasterCSS {
472470 }
473471 }
474472 } else {
475- const hexColorResult = / ^ # ( [ A - F a - f 0 - 9 ] { 3 , 4 } | [ A - F a - f 0 - 9 ] { 6 } | [ A - F a - f 0 - 9 ] { 8 } ) $ / . exec ( variableDefinition )
476- if ( hexColorResult ) {
477- const [ r , g , b , a ] = hexToRgb ( hexColorResult [ 1 ] )
478- addVariable ( name , { type : 'color' , value : `${ r } ${ g } ${ b } ${ a === 1 ? '' : ' / ' + a } ` , space : 'rgb' } )
479- } else {
480- const rgbFunctionResult = / ^ r g b \( * ( [ 0 - 9 ] { 1 , 3 } ) (?: * , * | + ) ( [ 0 - 9 ] { 1 , 3 } ) (?: * , * | + ) ( [ 0 - 9 ] { 1 , 3 } ) * (?: (?: , | \/ ) * ( .* ?) * ) ? \) $ / . exec ( variableDefinition )
481- if ( rgbFunctionResult ) {
482- addVariable ( name , { type : 'color' , value : rgbFunctionResult [ 1 ] + ' ' + rgbFunctionResult [ 2 ] + ' ' + rgbFunctionResult [ 3 ] + ( rgbFunctionResult [ 4 ] ? ' / ' + ( rgbFunctionResult [ 4 ] . startsWith ( '0.' ) ? rgbFunctionResult [ 4 ] . slice ( 1 ) : rgbFunctionResult [ 4 ] ) : '' ) , space : 'rgb' } )
483- } else {
484- const hslFunctionResult = / ^ h s l \( ( .* ?) \) $ / . exec ( variableDefinition )
485- if ( hslFunctionResult ) {
486- addVariable ( name , { type : 'color' , value : hslFunctionResult [ 1 ] , space : 'hsl' } )
487- } else {
488- addVariable ( name , { type : 'string' , value : variableDefinition } )
473+ // 1. HEX
474+ const hexMatch = / ^ # ( [ a - f 0 - 9 ] { 3 , 4 } | [ a - f 0 - 9 ] { 6 } | [ a - f 0 - 9 ] { 8 } ) $ / i. exec ( variableDefinition )
475+ if ( hexMatch ) {
476+ const [ r , g , b , a ] = hexToRgb ( hexMatch [ 1 ] )
477+ addVariable ( name , {
478+ type : 'color' ,
479+ value : `${ r } ${ g } ${ b } ${ a === 1 ? '' : `/${ a } ` } ` ,
480+ space : 'rgb' ,
481+ } )
482+ return
483+ }
484+
485+ // 2. COLOR FUNCTION
486+ const funcMatch = / ^ ( c o l o r | c o l o r - c o n t r a s t | c o l o r - m i x | h w b | l a b | l c h | o k l a b | o k l c h | r g b | h s l | l i g h t - d a r k ) \( ( .+ ) \) $ / i. exec ( variableDefinition )
487+ if ( funcMatch ) {
488+ let [ , space , rawArgs ] = funcMatch
489+ space = space . toLowerCase ( )
490+ const normalizedArgs = rawArgs
491+ . replace ( / \s * \/ \s * / g, '/' )
492+ . replace ( / \s * , \s * / g, ' ' )
493+ . replace ( / \s + / g, ' ' )
494+ . trim ( )
495+
496+ let alpha : string | undefined
497+ const alphaMatch = / ^ ( .+ ?) \/ ( [ ^ \s ] + ) $ / . exec ( normalizedArgs )
498+ const finalArgs = alphaMatch ? alphaMatch [ 1 ] . trim ( ) : normalizedArgs
499+ alpha = alphaMatch ? alphaMatch [ 2 ] : undefined
500+ if ( space === 'color' ) {
501+ const nested = / ^ ( [ a - z 0 - 9 - ] + ) ( .+ ) $ / i. exec ( finalArgs )
502+ if ( nested ) {
503+ const [ , nestedSpace , rest ] = nested
504+ addVariable ( name , {
505+ type : 'color' ,
506+ value : `${ nestedSpace } ${ rest } ` ,
507+ space : 'color' ,
508+ } , alpha )
509+ return
489510 }
490511 }
512+ addVariable ( name , {
513+ type : 'color' ,
514+ value : finalArgs ,
515+ space,
516+ } , undefined , alpha )
517+ return
491518 }
519+ // 3. Fallback
520+ addVariable ( name , { type : 'string' , value : variableDefinition } )
492521 }
493522 }
494523 }
0 commit comments