gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 10:10:44 26 4
gpt4 key购买 nike

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

在 Less 中我有如下样式:

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

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

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

但是它不起作用。

enter image description here

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

最佳答案

我在 emotion 中找不到任何方法,但我个人用一个使用 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}`;
};

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

关于css - 如何在 Emotion 中将十六进制颜色转换为 rgba,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48541666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com