Skip to content

Commit 3d555a6

Browse files
committed
Support for comma-less color functions
1 parent 58c89ee commit 3d555a6

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

packages/less/src/less/functions/color.js

+37-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import Dimension from '../tree/dimension';
22
import Color from '../tree/color';
33
import Quoted from '../tree/quoted';
44
import Anonymous from '../tree/anonymous';
5+
import Expression from '../tree/expression';
6+
import Operation from '../tree/operation';
57
let colorFunctions;
68

79
function clamp(val) {
@@ -56,7 +58,27 @@ function scaled(n, size) {
5658
}
5759
colorFunctions = {
5860
rgb: function (r, g, b) {
59-
const color = colorFunctions.rgba(r, g, b, 1.0);
61+
let a = 1
62+
/**
63+
* Comma-less syntax
64+
* e.g. rgb(0 128 255 / 50%)
65+
*/
66+
if (r instanceof Expression) {
67+
const val = r.value
68+
r = val[0]
69+
g = val[1]
70+
b = val[2]
71+
/**
72+
* @todo - should this be normalized in
73+
* function caller? Or parsed differently?
74+
*/
75+
if (b instanceof Operation) {
76+
const op = b
77+
b = op.operands[0]
78+
a = op.operands[1]
79+
}
80+
}
81+
const color = colorFunctions.rgba(r, g, b, a);
6082
if (color) {
6183
color.value = 'rgb';
6284
return color;
@@ -79,7 +101,20 @@ colorFunctions = {
79101
catch (e) {}
80102
},
81103
hsl: function (h, s, l) {
82-
const color = colorFunctions.hsla(h, s, l, 1.0);
104+
let a = 1
105+
if (h instanceof Expression) {
106+
const val = h.value
107+
h = val[0]
108+
s = val[1]
109+
l = val[2]
110+
111+
if (l instanceof Operation) {
112+
const op = l
113+
l = op.operands[0]
114+
a = op.operands[1]
115+
}
116+
}
117+
const color = colorFunctions.hsla(h, s, l, a);
83118
if (color) {
84119
color.value = 'hsl';
85120
return color;
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
foo {
2+
color: #0080ff;
3+
color: rgba(0, 128, 255, 0.5);
4+
color: hsl(198, 28%, 50%);
5+
color: hsla(198, 28%, 50%, 0.5);
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
foo {
2+
color: rgb(0 128 255);
3+
color: rgb(0 128 255 / 50%);
4+
color: hsl(198deg 28% 50%);
5+
color: hsl(198deg 28% 50% / 50%);
6+
}

0 commit comments

Comments
 (0)