@@ -16,11 +16,13 @@ export default postcss.plugin('postcss-lab-function', opts => {
16
16
if ( colorRegExp . test ( node . value ) ) {
17
17
const children = node . nodes . slice ( 1 , - 1 ) ;
18
18
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 ) ;
21
23
22
24
if ( isFunctionalLAB || isFunctionalLCH ) {
23
- node . value = 'rgb'
25
+ node . value = 'rgb' ;
24
26
25
27
const slashNode = children [ 3 ] ;
26
28
const alphaNode = children [ 4 ] ;
@@ -63,6 +65,46 @@ export default postcss.plugin('postcss-lab-function', opts => {
63
65
64
66
node . nodes . splice ( 3 , 0 , [ newComma ( ) ] ) ;
65
67
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
+ }
66
108
}
67
109
}
68
110
} ) ;
@@ -79,9 +121,10 @@ export default postcss.plugin('postcss-lab-function', opts => {
79
121
} ;
80
122
} ) ;
81
123
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;
84
126
const labRegExp = / ^ l a b $ / i;
127
+ const grayRegExp = / ^ g r a y $ / i;
85
128
const alphaUnitMatch = / ^ % ? $ / i;
86
129
const calcFuncMatch = / ^ c a l c $ / i;
87
130
const 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
94
137
const isSlash = node => Object ( node ) . type === 'operator' && node . value === '/' ;
95
138
const functionalLABMatch = [ isNumber , isNumber , isNumber , isSlash , isAlphaValue ] ;
96
139
const functionalLCHMatch = [ isNumber , isNumber , isHue , isSlash , isAlphaValue ] ;
140
+ const functionalGrayMatch = [ isNumber , isSlash , isAlphaValue ] ;
97
141
const matchFunctionalLAB = children => children . every (
98
142
( child , index ) => typeof functionalLABMatch [ index ] === 'function' && functionalLABMatch [ index ] ( child )
99
143
) ;
100
144
const matchFunctionalLCH = children => children . every (
101
145
( child , index ) => typeof functionalLCHMatch [ index ] === 'function' && functionalLCHMatch [ index ] ( child )
102
146
) ;
147
+ const matchFunctionalGray = children => children . every (
148
+ ( child , index ) => typeof functionalGrayMatch [ index ] === 'function' && functionalGrayMatch [ index ] ( child )
149
+ ) ;
103
150
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