Skip to content

Commit

Permalink
Support for comma-less color functions
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-dean committed Jul 29, 2020
1 parent 58c89ee commit 3d555a6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
39 changes: 37 additions & 2 deletions packages/less/src/less/functions/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Dimension from '../tree/dimension';
import Color from '../tree/color';
import Quoted from '../tree/quoted';
import Anonymous from '../tree/anonymous';
import Expression from '../tree/expression';
import Operation from '../tree/operation';
let colorFunctions;

function clamp(val) {
Expand Down Expand Up @@ -56,7 +58,27 @@ function scaled(n, size) {
}
colorFunctions = {
rgb: function (r, g, b) {
const color = colorFunctions.rgba(r, g, b, 1.0);
let a = 1
/**
* Comma-less syntax
* e.g. rgb(0 128 255 / 50%)
*/
if (r instanceof Expression) {
const val = r.value
r = val[0]
g = val[1]
b = val[2]
/**
* @todo - should this be normalized in
* function caller? Or parsed differently?
*/
if (b instanceof Operation) {
const op = b
b = op.operands[0]
a = op.operands[1]
}
}
const color = colorFunctions.rgba(r, g, b, a);
if (color) {
color.value = 'rgb';
return color;
Expand All @@ -79,7 +101,20 @@ colorFunctions = {
catch (e) {}
},
hsl: function (h, s, l) {
const color = colorFunctions.hsla(h, s, l, 1.0);
let a = 1
if (h instanceof Expression) {
const val = h.value
h = val[0]
s = val[1]
l = val[2]

if (l instanceof Operation) {
const op = l
l = op.operands[0]
a = op.operands[1]
}
}
const color = colorFunctions.hsla(h, s, l, a);
if (color) {
color.value = 'hsl';
return color;
Expand Down
6 changes: 6 additions & 0 deletions packages/test-data/css/_main/colors2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
foo {
color: #0080ff;
color: rgba(0, 128, 255, 0.5);
color: hsl(198, 28%, 50%);
color: hsla(198, 28%, 50%, 0.5);
}
6 changes: 6 additions & 0 deletions packages/test-data/less/_main/colors2.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
foo {
color: rgb(0 128 255);
color: rgb(0 128 255 / 50%);
color: hsl(198deg 28% 50%);
color: hsl(198deg 28% 50% / 50%);
}

0 comments on commit 3d555a6

Please sign in to comment.