简体   繁体   English

按一定比例更改 CSS 颜色模式的所有不透明度值

[英]Change all opacity values of a CSS color pattern by a certain ratio

So we have this string representing a color pattern in CSS:所以我们有这个字符串代表 CSS 中的颜色模式:

const COLOR = 'linear-gradient(to top, rgba(255, 0, 0, 0.3), purple, rgba(100, 100, 200), rgba(255, 0, 0, 0.8))';

It is possible that we may have more rgba(....) or even rgb(...) substrings.我们可能有更多的 rgba(....) 甚至 rgb(...) 子字符串。

Is there an efficient regexp or method to increase the opacity value of each rgba() substrings that have the opacity specified by 10% for example?是否有一种有效的正则表达式或方法来增加每个rgba()子字符串的不透明度值,例如,不透明度指定为 10%?

function changeOpacity(text, change = 1) {

    ...

    return(newTextValue);
}

console.log(changeOpacity(COLOR, 1.1));

So the resulting value would be:所以结果值将是:

linear-gradient(to top, rgba(255, 0, 0, 0.33), purple, rgba(100, 100, 200), rgba(255, 0, 0, 0.88))

Try this:尝试这个:

 const COLOR = 'linear-gradient(to top, rgba(255, 0, 0, 0.3), purple, rgba(100, 100, 200), rgba(255, 0, 0, 0.8))'; function changeOpacity(text, change = 1) { newstr = text.replace(/\d\.\d/g, x => (parseFloat(x) * change).toFixed(2)); return(newstr); } console.log(changeOpacity(COLOR, 1.1));

I don't know your exact requirement but you can easily change the opacity of rgba(....) or rgb(....) in a gradient.我不知道您的确切要求,但您可以轻松更改渐变中 rgba(....) 或 rgb(....) 的不透明度。

linear-gradient(to top, rgba(255, 0, 0, 0.3), purple 40%, rgba(100, 100, 200) 50%, rgba(255, 0, 0, 0.8))线性渐变(到顶部,rgba(255, 0, 0, 0.3),紫色 40%,rgba(100, 100, 200) 50%,rgba(255, 0, 0, 0.8))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM