How do I use an if statement with color3

I am trying to check if a color3 value is over a certain number but it shows an error.

You will need to break it down into its components, Color3s don’t have comparisons.
if part.Color.R > 250/255 and part.Color.G > 250/255
But there are a dozen ways to compare colors, and this probably isn’t the best way. What do you actually want to compare?

1 Like

I’m just trying to check if a parts color equals a certain color.

if yourColor == Color3.new(.1, .3, .5) then
–color is identical
end

thats an exact comparison between two colors. “over a certain number” is vague, unless you mean like specific r/g/b amounts or general light/darkness

2 Likes

I mean specific rgb amounts. Like: if Color.R == 250/255 then

Then do just that

if yourColor.r == 250/255 then
--red is exactly 250/255
elseif yourColor.r >= 250/255 then
--red is more than 250/255
end
1 Like

I think it may be important to note that you should use a “tolerance” level to see if something is the same color. Floating point errors can easily ruin that if statement. Consider this code:

workspace.Part.Color = Color3.new(250/255, 0, 0)
print(workspace.Part.Color.R == 250/255, workspace.Part.Color.R, 250/255)

The output of the code above is: false 0.98039215803146 0.98039215686275

It may be better to do something like this

if (math.abs(yourColor.R - 255 / 255) < 0.001) then
end

(which would evaluate to true in the case of the example code I provided)

workspace.Part.Color = Color3.new(250/255, 0, 0)
print(math.abs(workspace.Part.Color.R - 250/255) < 0.001, workspace.Part.Color.R, 250/255)
print(workspace.Part.Color.R - 250 / 255)

true 0.98039215803146 0.98039215686275
1.1687185663689e-09

3 Likes