Skip to content

Commit

Permalink
Preserves unsupported color values - Fixes less#2986
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-dean committed Jul 18, 2018
1 parent 2d409ae commit 43a9c51
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
68 changes: 39 additions & 29 deletions lib/less/functions/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,56 @@ colorFunctions = {
return colorFunctions.rgba(r, g, b, 1.0);
},
rgba: function (r, g, b, a) {
if (r instanceof Color) {
return new Color(r.rgb, r.alpha);
try {
if (r instanceof Color) {
if (g) {
a = number(g);
} else {
a = r.alpha;
}
return new Color(r.rgb, a);
}
var rgb = [r, g, b].map(function (c) { return scaled(c, 255); });
a = number(a);
return new Color(rgb, a);
}
var rgb = [r, g, b].map(function (c) { return scaled(c, 255); });
a = number(a);
return new Color(rgb, a);
catch (e) {}
},
hsl: function (h, s, l) {
return colorFunctions.hsla(h, s, l, 1.0);
},
hsla: function (h, s, l, a) {
try {
var m1, m2;

var m1, m2;

function hue(h) {
h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
if (h * 6 < 1) {
return m1 + (m2 - m1) * h * 6;
}
else if (h * 2 < 1) {
return m2;
}
else if (h * 3 < 2) {
return m1 + (m2 - m1) * (2 / 3 - h) * 6;
function hue(h) {
h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
if (h * 6 < 1) {
return m1 + (m2 - m1) * h * 6;
}
else if (h * 2 < 1) {
return m2;
}
else if (h * 3 < 2) {
return m1 + (m2 - m1) * (2 / 3 - h) * 6;
}
else {
return m1;
}
}
else {
return m1;
}
}

h = (number(h) % 360) / 360;
s = clamp(number(s)); l = clamp(number(l)); a = clamp(number(a));
h = (number(h) % 360) / 360;
s = clamp(number(s)); l = clamp(number(l)); a = clamp(number(a));

m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
m1 = l * 2 - m2;
m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
m1 = l * 2 - m2;

return colorFunctions.rgba(hue(h + 1 / 3) * 255,
hue(h) * 255,
hue(h - 1 / 3) * 255,
a);
return colorFunctions.rgba(hue(h + 1 / 3) * 255,
hue(h) * 255,
hue(h - 1 / 3) * 255,
a);
}
catch (e) {}
},

hsv: function(h, s, v) {
Expand Down
6 changes: 6 additions & 0 deletions test/css/colors.css
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@
test-2: #5559;
test-3: rgba(111, 111, 111, 0.6);
test-4: rgba(85, 85, 85, 0.1);
test-5: rgba(85, 85, 85, 0.6);
test-6: rgba(85, 85, 85, 0.6);
test-7: rgba(85, 85, 85, 0.5);
test-8: rgba(var(--color-accent), 0.2);
test-9: rgb(var(--color-accent));
test-9: hsla(var(--color-accent));
}
4 changes: 4 additions & 0 deletions test/less/colors.less
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,8 @@
test-4: fade(#5559, 10%);
test-5: rgba(#55555599);
test-6: rgba(#5559);
test-7: rgba(#5559, 0.5);
test-8: rgba(var(--color-accent), 0.2);
test-9: rgb(var(--color-accent));
test-9: hsla(var(--color-accent));
}

0 comments on commit 43a9c51

Please sign in to comment.