Is there an efficient way to check if a part's color is SIMILAR TO a color?

I simply need to check whether a part’s color is SIMILAR TO a color.

if Part.Color == Color3.fromRGB(205, 201, 166) then
    -- Other code
end

This code would check if the part’s color was EQUAL TO a certain color, but I want the script to round it so that it doesn’t have to be EXACT, but close enough. I figured it’d have to do with coverting RGB color to BrickColor, but I don’t know how to do that.

3 Likes

Just compare the R, G, and B values.

1 Like

You can use the Euclidean Color Difference formula:

From wikipedia:
image

The lower the value, the more similar the colors are. The higher the value, the less similar the colors are.

Here is the function:

local Color_1 = Color3.fromRGB(205, 201, 166)
local Color_2 = Color3.fromRGB(205, 201, 166)

local function Similarity(Color_1: Color3, Color_2: Color3)
    local R1, G1, B1 = Color_1.R, Color_1.G, Color_1.B
    local R2, G2, B2 = Color_2.R, Color_2.G, Color_2.B
    local R, G, B = (R1 - R2), (G1 - G2), (B1 - B2)

    return math.sqrt((R^2) +( G^2) + (B^2))
end

print(Similarity(Color_1, Color_2))  -- Output: 0
9 Likes

I think you can use the HSL color space to achieve a similar effect. In this color space (Hue, Saturation, Lightness) you can make the lightness a bit looser.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.