Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Color comparing doesn't work even though both values are the same?

Asked by 5 years ago
Edited 5 years ago
workspace.Part.Color = Color3.new(100/255,100/255,100/255) 
workspace.Part.Value.Value = Color3.new(100/255,100/255,100/255) 
if workspace.Part.Color == workspace.Part.Value.Value then print'equal' end 

Try this out, it will never print equal even though these are the exact same for some reason. It will work with any other color though, I don't understand? Just put a part in workspace and put a color3 value in it and run this in the command bar

0
Please use code blocks. User#19524 175 — 5y
0
is the "part.Value" a color3value or not Elixcore 1337 — 5y
0
Yes, it obviously is. masterblokz 58 — 5y

1 answer

Log in to vote
1
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
5 years ago
Edited 5 years ago

Do not quote me on this but here is what I think is happening.

So I did some testy test and found out some interesting things.

First it is important to remember that color3.new() takes in number value from 0 to 1. A color3.fromRGB takes value from 0 to 255.

-- [Declaration Section]
local Baseplate          = workspace.Baseplate;
local Color              = workspace.Color;

-- [Initialization Section]
Baseplate.Color          = Color3.new(78/255, 34/255, 100/255);
Color.Value              = Color3.new(78/255, 34/255, 100/255);

-- [Output Section]

if Baseplate.Color == Color.Value then 
    print("Similar!");
else 
    print("Not Similar");
end;

I inserted a Color3Value on Workspace and currently am testing its value with Baseplate's Color.

When you run the code, you will notice that it prints Not Similar. The problem is that the number/255 gives us decimal numbers. From my understanding, the Color3Value takes in values from 0 to 255.

But if you check the Color3Value's Value when running the game, you will notice that the value is actually the exact same as the one we are assigning with. Do take a look at this Image. If anyone could explain why that is happening, it would be appreciated.

Neverthless, the fix for your code would be to use Color3.fromRGB when assigning values to Color3Value.

-- [Declaration Section]
local Baseplate          = workspace.Baseplate;
local Color              = workspace.Color;

-- [Initialization Section]
Baseplate.Color          = Color3.new(78/255, 34/255, 100/255);
Color.Value              = Color3.fromRGB(78, 34, 100);

-- [Output Section]

if Baseplate.Color == Color.Value then 
    print("Similar!");
else 
    print("Not Similar");
end;

The above example will print Similar.

[EDIT]

@hiimgoodpack had answered that Color3.new() way does not work because floating point errors. The if conditional statement does not run because those two numbers do not exactly match. Credits to hiim.

0
good answer. upvote from me User#19524 175 — 5y
0
Thanks, I forgot about .fromRGB, I had always used that before anyways do to not needing to type/255 for each number. Is there any difference between color3.new and fromRGB? masterblokz 58 — 5y
0
Yup. I already told you the difference on my answer. I will state it again: Color3.new() takes in values from 0-1 whereas Color3.fromRGB takes in values from 0-255.  Zafirua 1348 — 5y
0
so there is really no point in using Color3.new() anymore, right? masterblokz 58 — 5y
View all comments (2 more)
0
You are pretty much good to go with color3.fromRGB Zafirua 1348 — 5y
0
You use Color3.new() when you want to define it with values ranging from 0 to 1. If you only plan to use RGB values ranging from 0-255, you should use Color3.RGB as that is why the additional constructor was created. amanda 1059 — 5y
Ad

Answer this question