Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit 2ee1743

Browse files
committed
Add support for gray(a / b) as lab(a 0 0 / b)
1 parent bde8f0e commit 2ee1743

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-6
lines changed

index.js

+55-6
Original file line numberDiff line numberDiff line change
@@ -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-])(lab?|lch?)\(/i;
83-
const colorRegExp = /^(lab?|lch?)$/i;
124+
const colorAnyRegExp = /(^|[^\w-])(lab|lch|gray)\(/i;
125+
const colorRegExp = /^(lab|lch|gray)$/i;
84126
const labRegExp = /^lab$/i;
127+
const grayRegExp = /^gray$/i;
85128
const alphaUnitMatch = /^%?$/i;
86129
const calcFuncMatch = /^calc$/i;
87130
const hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
@@ -94,11 +137,17 @@ const isPercentage = node => isCalc(node) || Object(node).type === 'number' && n
94137
const isSlash = node => Object(node).type === 'operator' && node.value === '/';
95138
const functionalLABMatch = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];
96139
const functionalLCHMatch = [isNumber, isNumber, isHue, isSlash, isAlphaValue];
140+
const functionalGrayMatch = [isNumber, isSlash, isAlphaValue];
97141
const matchFunctionalLAB = children => children.every(
98142
(child, index) => typeof functionalLABMatch[index] === 'function' && functionalLABMatch[index](child)
99143
);
100144
const 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 });

test/basic.css

+8
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@
1515
color: lch(40 68.8 34.5 / 50%);
1616
color: lch(100 50 0);
1717
}
18+
19+
.test-gray {
20+
color: gray(40);
21+
color: gray(40 / 1);
22+
color: gray(40 / .5);
23+
color: gray(40 / 100%);
24+
color: gray(40 / 50%);
25+
}

test/basic.expect.css

+8
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@
1515
color: rgba(178, 34, 34, 0.5);
1616
color: rgb(255, 216, 255);
1717
}
18+
19+
.test-gray {
20+
color: rgb(94,94,94);
21+
color: rgb(94,94,94);
22+
color: rgba(94,94,94, .5);
23+
color: rgb(94,94,94);
24+
color: rgba(94,94,94, 0.5);
25+
}

test/basic.preserve-true.expect.css

+13
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,16 @@
2727
color: rgb(255, 216, 255);
2828
color: lch(100 50 0);
2929
}
30+
31+
.test-gray {
32+
color: rgb(94,94,94);
33+
color: gray(40);
34+
color: rgb(94,94,94);
35+
color: gray(40 / 1);
36+
color: rgba(94,94,94, .5);
37+
color: gray(40 / .5);
38+
color: rgb(94,94,94);
39+
color: gray(40 / 100%);
40+
color: rgba(94,94,94, 0.5);
41+
color: gray(40 / 50%);
42+
}

0 commit comments

Comments
 (0)