我正在尝试获得RYB空间中颜色的颜色和谐。我已经成功实现了RGB和声,但是无法使RYB和声正常工作。他们被FAR关闭。
我的程序获取HEX / HTML颜色,将其转换为RGB,将RGB转换为RYB,将RYB转换为HSL,并通过增加色相从那里执行和声计算。我使用https://www.rapidtables.com/convert/color/rgb-to-hsl.html上的公式将RYB转换为HSL。在该页面上,它提供了将RGB转换为HSL的公式。我只是使用RYB值代替RGB。下面是我用来将RGB转换为RYB的公式:
var r = color[0], g = color[1], b = color[2];
// Remove the whiteness from the color.
var w = Math.min(r, g, b);
r -= w;
g -= w;
b -= w;
var mg = Math.max(r, g, b);
// Get the yellow out of the red+green.
var y = Math.min(r, g);
r -= y;
g -= y;
// If this unfortunate conversion combines blue and green, then cut each in
// half to preserve the value's maximum range.
if (b && g) {
b /= 2.0;
g /= 2.0;
}
// Redistribute the remaining green.
y += g;
b += g;
// Normalize to values.
var my = Math.max(r, y, b);
if (my) {
var n = mg / my;
r *= n;
y *= n;
b *= n;
}
// Add the white back in.
r += w;
y += w;
b += w;
// And return back the ryb typed accordingly.
return [r, y, b];
}
在RYB中获得红色的互补色时,它应该是绿色。当获得RGB中红色的互补色时,它应该是青色。无论如何,我的程序都会显示青色。 该程序应该给我这个:http://prntscr.com/o16ava 相反,它给了我:http://prntscr.com/o16b08
答案 0 :(得分:0)
它无法正常工作,因为正如您在评论中提到的那样,当您将ryb转换为hsl时,您会将其视为rgb。
当您运行上面发布的代码时,值分别为:红色= 255,绿色= 0,蓝色= 0
它返回值:红色= 255,黄色= 0,蓝色= 0
夸奖:红色= 0,黄色= 255,蓝色= 255
如果随后将其传递给旨在将rgb转换为hsl的函数,它将像告诉它进行转换一样进行计算:红色= 0,绿色= 255,蓝色= 255
青色。
您需要将ryb转换回rgb,或获取一个旨在将ryb转换为hsl的函数。