gpt4 book ai didi

javascript - 获取 HSL 值,给定 x、y 和色调

转载 作者:行者123 更新时间:2023-11-29 22:01:27 25 4
gpt4 key购买 nike

给定一个看起来像这样的颜色选择器:

enter image description here

我正在尝试根据光标的 x 和 y 位置加上右侧 slider 的色调来计算 HSL 值。我的数学能力很弱,虽然我有很接近,但试图捕获一个非常浅的全强度颜色很麻烦,(它最终变得太灰)。这是我正在使用的当前功能:

getHSL = function(h, x, y) {
var hsl, hue, lightness, saturation;
hue = parseInt(h, 10);
if (hue === 360) {
hue = 0;
}
saturation = x;
lightness = (50 * (1 - (x / 100)) + 50) * (1 - (y / 100));
hue = Math.round(clamp(hue, 0, 360));
saturation = Math.round(clamp(saturation, 0, 100));
lightness = Math.round(clamp(lightness, 0, 100));
hsl = {
hue: hue,
saturation: saturation,
lightness: lightness
};
return hsl;
};

这是错误的做法吗?结果实际上非常接近正确,但与非常浅的全强度颜色有点偏差。

最佳答案

看起来您的颜色选择器是用 HSV(色调/饱和度/值)坐标表示的。

维基百科对此有一篇不错的文章,您可以根据那里提供的信息计算出转换公式。

或者,在 Google 上快速搜索返回,例如,此页面包含可用于将 HSV 转换为 HSL 的公式:http://codeitdown.com/hsl-hsb-hsv-color/ .假设您的 xy 都在 0 到 100 之间运行,则计算应为:

hsv_value = 1 - (y / 100);
hsv_saturation = x / 100;
lightness = (hsv_value / 2) * (2 - hsv_saturation);
saturation = (hsv_value * hsv_saturation) / (1 - Math.abs(2 * lightness - 1));

这会返回 [0,1] 范围内的 saturationlightness。另外,为什么要clamp您的xy 坐标?

关于javascript - 获取 HSL 值,给定 x、y 和色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23520909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com