Suppose I've parsed my mixin parameters and colours are provided using rgba
function.
Now I would like to mix two of those colours, but LESS mix
function requires parameter instances of type color
.
What I tried
...and doesn't work
- calling
color("rgba(0,0,0,.5)")
mix(@first, ~"@{second}")
where@second
is a string likergba(0,0,0,0.5)
How do I convert to color?
I'm afraid you won't find a built in function to do this in LESS.
The
color()
functions argument needs to be a hexadecimal or its shorthand representation.But you can further parse the string with javascript to
rgba()
function to get a variable of type color.You could do the first step in your original parsing. If you need to do all in LESS you can do something like this:
Unfortunately the string can not be directly read into the
rgba()
or similar functions as they expect multiple arguments. Even with string escaping (eg.~
) the output gets formated as a single variable (~"a, b, c"
becomesa, b, c
and acts as a single output string for the css), so we need to get each value individually and pass it to its respective argument/variable of thergba()
function. Using the functionunit()
we transform a string to a number that can be used further.For example:
resulting in CSS: