Convert HEX to RGBA in Stylus
Asked Answered
B

2

13

I have some HEX colour values in Stylus similar to the following

$my-background ?= #123456
$my-foreground ?= #ABCDEF

and would like to use them in rgba values for opacity, so that

.my-class
    background rgba($my-background, .5)
    foreground rgba($my-foreground, .5)

or an alternative syntax is compiled into CSS as

.my-class {
    background rgba(18, 52, 86, .5);
    foreground rgba(171, 205, 239, .5);
}

Is there a quick and easy way to use HEX colour values in rgba using Stylus or a plugin for Stylus (such as nib)?

Brightness answered 12/12, 2013 at 15:18 Comment(0)
G
20

Actually, the rgba in Stylus works just like that, have you tried what you wrote? In Stylus

$my-background ?= #123456
$my-foreground ?= #ABCDEF

.my-class
    background rgba($my-background, .5)
    foreground rgba($my-foreground, .5)

Would actually compile to

.my-class {
  background: rgba(18,52,86,0.5);
  foreground: rgba(171,205,239,0.5);
}

This means you can just go and do things like rgba(black, 0.5), rgba(#FFF, 0.5) and use variables inside of it, just like in your case.

Gestalt answered 15/12, 2013 at 12:43 Comment(1)
@Brightness I gave your question a +1 because I also didn't try it before searching the internet.Lusty
B
1

In the end I made my own function to do this:

hextorgba(color, alpha = 1)
    'rgba(' + red(color) + ', ' + green(color) + ', ' + blue(color) + ', ' + alpha + ')'
Brightness answered 12/12, 2013 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.