Search code examples
cssjsonsasscolorslibsass

Convert a Hex-String to a Hex-Color value in SCSS


I have stored a lot of colors in json-format. I use rootbeer via gulp to turn them into sass maps that can be processed by the scss:

{ "blue": "33A2FF" }

to

$colors: ( "blue": "33A2FF" );

I can use the colors just fine with the #{} syntax, but as soon as any color-functions like lightness() are applied the compiler fails because the color-type is entirely gone.

Even working with unquote() will not allow sass to treat this as nothing else but a string, but it needs to be a color.

My question is if there is any way for me to force the compiler to treat this as a color instead? The alternative will be to omit all uses of the built-in color functions.

Alternatively I could rewrite all hey as rgb, with seperate values for r, g and b and then use the rgb() syntax to return them as color, as seen here. But that would require a significant rework. So maybe anyone has an idea of how to solve this.


Solution

  • I found utilities that allow the processing of strings in a hex format to rgb(): https://github.com/KittyGiraudel/SassyJSON/tree/master/stylesheets/decode/helpers/color