gpt4 book ai didi

javascript - 如何获取接近另一种颜色的颜色的 rgb 值?

转载 作者:行者123 更新时间:2023-11-30 08:58:15 26 4
gpt4 key购买 nike

如果我有 RGB 颜色。如何创建一个 javascript 函数,当另一个 RGB 值接近初始 RGB 值时返回 true,否则返回 false?

最佳答案

我用过这个,对我来说效果很好:

// assuming that color1 and color2 are objects with r, g and b properties
// and tolerance is the "distance" of colors in range 0-255
function isNeighborColor(color1, color2, tolerance) {
if(tolerance == undefined) {
tolerance = 32;
}

return Math.abs(color1.r - color2.r) <= tolerance
&& Math.abs(color1.g - color2.g) <= tolerance
&& Math.abs(color1.b - color2.b) <= tolerance;
}

并且根据您的特定问题,颜色距离的含义可能会有所不同,例如,在您的情况下,您可能需要将 && 更改为 ||

关于javascript - 如何获取接近另一种颜色的颜色的 rgb 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11506302/

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