如何在 Emotion 中将十六进制颜色转换为 rgba

Rah*_*dav 5 css less emotion reactjs

我正在将应用程序的样式从 Less 转换为Emotion

在 Less 我有如下样式:

@blue: #476882;
background: rgba(red(@blue), green(@blue), blue(@blue), 0.5);
Run Code Online (Sandbox Code Playgroud)

在转换为情感时,我正在这样做:

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

但是它不起作用。

在此处输入图片说明

有什么建议可以使这项工作?

all*_*uca 2

我在情感中找不到任何方法,但我个人用一个使用hex-rgb包的实用函数解决了它:

import hexRgb from 'hex-rgb';

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

然后像这样使用它:rgba(${rgba('#000', 0.15)})