简体   繁体   English

阻止颜色转换为RGB?

[英]Keep colors from being converted to RGB?

When assigning a hsl color using Javascript, it doesn't apply it as an HSL color, but rather converts it to RGB. 使用Javascript分配hsl颜色时,它不会将其用作HSL颜色,而是将其转换为RGB。

document.body.style.backgroundColor = "hsl(0,100%,50%)"
document.body.style.backgroundColor; // "rgb(255, 0, 0)"

I was hoping to assign an HSL color, then modify the saturation and brightness after the fact, but I can't do that easily since it gets converted to RGB. 我原本希望分配一种HSL颜色,然后在事后修改饱和度和亮度,但是由于将其转换为RGB,所以我不容易做到。

Is there a way to keep it from converting to RGB, or is converting it back to HSL manually necessary? 有没有办法防止它转换为RGB,还是需要手动将其转换回HSL?

For SVG elements, you can use an attribute instead of a style for the "fill" color, and it will keep the HSL format. 对于SVG元素,您可以使用属性而不是样式来填充颜色,它将保留HSL格式。

As for elements in general, storing the HSL value as a variable or attribute to pull from when modifying/reapplying the color may be the simplest solution. 对于一般元素,将HSL值存储为在修改/重新应用颜色时要提取的变量或属性可能是最简单的解决方案。

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

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