Having a colour represent another in a table

return {
	[Color3.fromRGB(107, 184, 217)] = Color3.fromRGB(89, 155, 181),
}

What I’m trying to have is when I check this table, if a UI element has the same colour as the first piece of data, ie.

Color3.fromRGB(107, 184, 217)

then it sets another UI element to be the the other color

Close.ImageColor3 = FavouriteColorSettings[FavouriteColorValue.Value] -- FavouriteColorValue.Value == Color3.fromRGB(107, 184, 217)

Basically what the table at the top is, the first data is the colour, then the second is the darker version of that colour. Reason I want it like this is I’m gonna allow players to change UI’s colours around and check to have whatever colour they have it set to get the colours shadow as well.

Error I get
[ bad argument #3 (Color3 expected, got nil)]

2 Likes

Well what I’d do is:
Make the value in the table a string:

return {
	[tostring(Color3.fromRGB(107, 184, 217))] = Color3.fromRGB(89, 155, 181),
}

Then when you need to index it you can do:

FavouriteColorSettings[tostring(FavouriteColorValue.Value)]

Also happy birthday!

1 Like

I’ve had problems storing colors in tables as-well, and without any fix in mind I just have bricks in RepStorage and i’ll call upon those for colors.

Until I find a fix of course, happy birthday by the way.

You probably don’t want to use Color3 for keys in something like this. The issue is that the colors aren’t the same. They may have the same R, G, and B values, but that doesn’t matter because they aren’t checked if they’re equal.

local t = {}
t[Color3.new()] = true
print(t[Color3.new()])
--> nil

You can check if they are the same with rawequal

print(rawequal(Color3.new(),Color3.new()))
--> false
local color = Color3.new()
print(rawequal(color,color))
--> true

Using some other representation of the color would be better in this case. Strings would work for this.

local t = {}
t[tostring(Color3.new())] = true
print(t[tostring(Color3.new())])
--> true

There is a topic similar to this here.

You can use tostring(color) I believe. I think this is just an issue with most Roblox types due to the way they are created. If you think of a color similar to an instance each time you create a color it creates a separate value entirely.

Colors, instances, vectors, etc use userdata values which act similarly to tables. Even if two table’s content are the same they won’t ever equal each other. This is more of a lua flaw than a Roblox one I guess.