Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert hex color to rgba in Emotion

I am converting styles of my app from Less to Emotion

in Less I have styles as follows:

@blue: #476882;
background: rgba(red(@blue), green(@blue), blue(@blue), 0.5);

while converting to Emotion I am doing this:

export const BLUE = '#476882'
background: rgba(red(${BLUE}), green(${BLUE}), blue(${BLUE}), 0.5);

however it is not working.

enter image description here

any suggestions to make this work?

like image 294
Rahul Yadav Avatar asked Nov 08 '22 11:11

Rahul Yadav


1 Answers

I couldn't find any method within emotion, but I personally solved it with an utility function that uses the hex-rgb package:

import hexRgb from 'hex-rgb';

export const rgba = (hex, alpha) => {
  const rgb = hexRgb(hex, { format: 'array' })
    .slice(0, -1)
    .join(',');
  return `${rgb},${alpha}`;
};

and then just use it like so: rgba(${rgba('#000', 0.15)})

like image 180
alliuca Avatar answered Nov 15 '22 06:11

alliuca