我想用HSL颜色填充一些圆圈,这样我就可以改变它们的亮度值。我是JavaScript编程的新手,我在JavaScript里找不到任何关于HSL颜色的东西。我现在有这样的代码:
context.fillStyle = 'rgb(200, 100, 0)';我想要找到与rgb-function基本相当的东西,但是用HSL。
发布于 2021-04-23 00:23:22
您可以从HSV转换为rgb。在画布中,您可以使用以下内容:
ctx.fillStyle = "#0000FF";
ctx.fillStyle = "RGB(0,0,255)"
ctx.fillStyle = "RGBA(0,0,255,0.3)";
ctx.fillStyle = "HSL(120,100,50)";
ctx.fillStyle = "HSLA(120,100,50,0.3)";从hsv转换到rgba的函数如下所示:
function HSVtoRGB(h, s, v) {
var r, g, b, i, f, p, q, t;
if (arguments.length === 1) {
s = h.s, v = h.v, h = h.h;
}
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return {
r: Math.round(r * 255),
g: Math.round(g * 255),
b: Math.round(b * 255)
};
}https://stackoverflow.com/questions/67220568
复制相似问题