如果我有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;
}


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

09-13 14:28