@@ -16,11 +16,13 @@ export default postcss.plugin('postcss-lab-function', opts => {
1616 if ( colorRegExp . test ( node . value ) ) {
1717 const children = node . nodes . slice ( 1 , - 1 ) ;
1818 const isLab = labRegExp . test ( node . value ) ;
19- const isFunctionalLAB = matchFunctionalLAB ( children ) ;
20- const isFunctionalLCH = matchFunctionalLCH ( children ) ;
19+ const isGray = grayRegExp . test ( node . value ) ;
20+ const isFunctionalLAB = ! isGray && matchFunctionalLAB ( children ) ;
21+ const isFunctionalLCH = ! isGray && matchFunctionalLCH ( children ) ;
22+ const isFunctionalGray = isGray && matchFunctionalGray ( children ) ;
2123
2224 if ( isFunctionalLAB || isFunctionalLCH ) {
23- node . value = 'rgb'
25+ node . value = 'rgb' ;
2426
2527 const slashNode = children [ 3 ] ;
2628 const alphaNode = children [ 4 ] ;
@@ -63,6 +65,46 @@ export default postcss.plugin('postcss-lab-function', opts => {
6365
6466 node . nodes . splice ( 3 , 0 , [ newComma ( ) ] ) ;
6567 node . nodes . splice ( 2 , 0 , [ newComma ( ) ] ) ;
68+ } else if ( isFunctionalGray ) {
69+ node . value = 'rgb' ;
70+
71+ const alphaNode = children [ 2 ] ;
72+
73+ const rgbValues = lab2rgb (
74+ ...[
75+ children [ 0 ] . value ,
76+ 0 ,
77+ 0
78+ ] . map (
79+ number => parseFloat ( number )
80+ )
81+ ) . map (
82+ sourceValue => Math . max ( Math . min ( parseInt ( sourceValue * 2.55 ) , 255 ) , 0 )
83+ ) ;
84+
85+ node . removeAll ( )
86+ . append ( newParen ( '(' ) )
87+ . append ( newNumber ( rgbValues [ 0 ] ) )
88+ . append ( newComma ( ) )
89+ . append ( newNumber ( rgbValues [ 1 ] ) )
90+ . append ( newComma ( ) )
91+ . append ( newNumber ( rgbValues [ 2 ] ) )
92+ . append ( newParen ( ')' ) ) ;
93+
94+ if ( alphaNode ) {
95+ if ( isPercentage ( alphaNode ) && ! isCalc ( alphaNode ) ) {
96+ alphaNode . unit = '' ;
97+ alphaNode . value = String ( alphaNode . value / 100 ) ;
98+ }
99+
100+ if ( alphaNode . value !== '1' ) {
101+ node . value += 'a' ;
102+
103+ node
104+ . insertBefore ( node . last , newComma ( ) )
105+ . insertBefore ( node . last , alphaNode )
106+ }
107+ }
66108 }
67109 }
68110 } ) ;
@@ -79,9 +121,10 @@ export default postcss.plugin('postcss-lab-function', opts => {
79121 } ;
80122} ) ;
81123
82- const colorAnyRegExp = / ( ^ | [ ^ \w - ] ) ( l a b ? | l c h ? ) \( / i;
83- const colorRegExp = / ^ ( l a b ? | l c h ? ) $ / i;
124+ const colorAnyRegExp = / ( ^ | [ ^ \w - ] ) ( l a b | l c h | g r a y ) \( / i;
125+ const colorRegExp = / ^ ( l a b | l c h | g r a y ) $ / i;
84126const labRegExp = / ^ l a b $ / i;
127+ const grayRegExp = / ^ g r a y $ / i;
85128const alphaUnitMatch = / ^ % ? $ / i;
86129const calcFuncMatch = / ^ c a l c $ / i;
87130const hueUnitMatch = / ^ ( d e g | g r a d | r a d | t u r n ) ? $ / i;
@@ -94,11 +137,17 @@ const isPercentage = node => isCalc(node) || Object(node).type === 'number' && n
94137const isSlash = node => Object ( node ) . type === 'operator' && node . value === '/' ;
95138const functionalLABMatch = [ isNumber , isNumber , isNumber , isSlash , isAlphaValue ] ;
96139const functionalLCHMatch = [ isNumber , isNumber , isHue , isSlash , isAlphaValue ] ;
140+ const functionalGrayMatch = [ isNumber , isSlash , isAlphaValue ] ;
97141const matchFunctionalLAB = children => children . every (
98142 ( child , index ) => typeof functionalLABMatch [ index ] === 'function' && functionalLABMatch [ index ] ( child )
99143) ;
100144const matchFunctionalLCH = children => children . every (
101145 ( child , index ) => typeof functionalLCHMatch [ index ] === 'function' && functionalLCHMatch [ index ] ( child )
102146) ;
147+ const matchFunctionalGray = children => children . every (
148+ ( child , index ) => typeof functionalGrayMatch [ index ] === 'function' && functionalGrayMatch [ index ] ( child )
149+ ) ;
103150
104- const newComma = ( ) => parser . comma ( { value : ',' } )
151+ const newComma = ( ) => parser . comma ( { value : ',' } ) ;
152+ const newNumber = value => parser . number ( { value } ) ;
153+ const newParen = value => parser . paren ( { value } ) ;
0 commit comments