Skip to content

Commit 43a9c51

Browse files
committed
Preserves unsupported color values - Fixes less#2986
1 parent 2d409ae commit 43a9c51

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed

lib/less/functions/color.js

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,46 +35,56 @@ colorFunctions = {
3535
return colorFunctions.rgba(r, g, b, 1.0);
3636
},
3737
rgba: function (r, g, b, a) {
38-
if (r instanceof Color) {
39-
return new Color(r.rgb, r.alpha);
38+
try {
39+
if (r instanceof Color) {
40+
if (g) {
41+
a = number(g);
42+
} else {
43+
a = r.alpha;
44+
}
45+
return new Color(r.rgb, a);
46+
}
47+
var rgb = [r, g, b].map(function (c) { return scaled(c, 255); });
48+
a = number(a);
49+
return new Color(rgb, a);
4050
}
41-
var rgb = [r, g, b].map(function (c) { return scaled(c, 255); });
42-
a = number(a);
43-
return new Color(rgb, a);
51+
catch (e) {}
4452
},
4553
hsl: function (h, s, l) {
4654
return colorFunctions.hsla(h, s, l, 1.0);
4755
},
4856
hsla: function (h, s, l, a) {
57+
try {
58+
var m1, m2;
4959

50-
var m1, m2;
51-
52-
function hue(h) {
53-
h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
54-
if (h * 6 < 1) {
55-
return m1 + (m2 - m1) * h * 6;
56-
}
57-
else if (h * 2 < 1) {
58-
return m2;
59-
}
60-
else if (h * 3 < 2) {
61-
return m1 + (m2 - m1) * (2 / 3 - h) * 6;
60+
function hue(h) {
61+
h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
62+
if (h * 6 < 1) {
63+
return m1 + (m2 - m1) * h * 6;
64+
}
65+
else if (h * 2 < 1) {
66+
return m2;
67+
}
68+
else if (h * 3 < 2) {
69+
return m1 + (m2 - m1) * (2 / 3 - h) * 6;
70+
}
71+
else {
72+
return m1;
73+
}
6274
}
63-
else {
64-
return m1;
65-
}
66-
}
6775

68-
h = (number(h) % 360) / 360;
69-
s = clamp(number(s)); l = clamp(number(l)); a = clamp(number(a));
76+
h = (number(h) % 360) / 360;
77+
s = clamp(number(s)); l = clamp(number(l)); a = clamp(number(a));
7078

71-
m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
72-
m1 = l * 2 - m2;
79+
m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
80+
m1 = l * 2 - m2;
7381

74-
return colorFunctions.rgba(hue(h + 1 / 3) * 255,
75-
hue(h) * 255,
76-
hue(h - 1 / 3) * 255,
77-
a);
82+
return colorFunctions.rgba(hue(h + 1 / 3) * 255,
83+
hue(h) * 255,
84+
hue(h - 1 / 3) * 255,
85+
a);
86+
}
87+
catch (e) {}
7888
},
7989

8090
hsv: function(h, s, v) {

test/css/colors.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,10 @@
9090
test-2: #5559;
9191
test-3: rgba(111, 111, 111, 0.6);
9292
test-4: rgba(85, 85, 85, 0.1);
93+
test-5: rgba(85, 85, 85, 0.6);
94+
test-6: rgba(85, 85, 85, 0.6);
95+
test-7: rgba(85, 85, 85, 0.5);
96+
test-8: rgba(var(--color-accent), 0.2);
97+
test-9: rgb(var(--color-accent));
98+
test-9: hsla(var(--color-accent));
9399
}

test/less/colors.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@
104104
test-4: fade(#5559, 10%);
105105
test-5: rgba(#55555599);
106106
test-6: rgba(#5559);
107+
test-7: rgba(#5559, 0.5);
108+
test-8: rgba(var(--color-accent), 0.2);
109+
test-9: rgb(var(--color-accent));
110+
test-9: hsla(var(--color-accent));
107111
}

0 commit comments

Comments
 (0)