brandColor = rgb(2,5,2);
pageBackgroundColor :{
backgroundColor: brandColor,
},我想增加背景颜色的不透明度。我知道我既可以写"rgba(2,5,2,0.5)“,也可以使用内联样式来响应本机。,但是我想使用"brandColor“变量。,我在5-6个不同的地方使用这个变量,不想手动更新每个地方的颜色。
发布于 2018-03-24 19:51:12
如果将brandColor变量设置为字符串,则可以使用字符串操作添加不透明度-
const brandColor = "rgba(2, 5, 2)";
function addOpacity(rgbString, opacity) {
return rgbString.split(')')[0] + "," + opacity + ")"
}
<View backgroundColor={addOpacity(brandColor, 0.5)} />发布于 2020-06-26 10:14:25
这是一个更老的问题,但也许有人像我一样正在寻找这个问题。我得到了一个错误,因为带有3个参数的rgba是无效格式。我的解决办法:
const brandColor = "rgba(2, 5, 2, 1)";
function addOpacity(rgbString, opacity) {
return rgbString.split(', 1)')[0] + "," + opacity + ")"
}
<View backgroundColor={addOpacity(brandColor, 0.5)} />这样,我们仍然可以使用基本函数brandColor,并且可以更改“1”作为其他参数的不透明度:)
https://stackoverflow.com/questions/49469012
复制相似问题