Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[Colors] How can I find the opposite Color?

Is there an equation/function that is available to use to calculate the opposite of a given colour (sometimes known as complementary) without having to write a massive table??

For example, the opposite of white (255/255,255/255,255/255) is black (0/255,0/255,0/255)

[EDIT]

I've tried getting the opposite using this formula, but isn't 100% efficient:

Original Color = r,g,b Opposite/Complimentary = 255-r,255-g,255-b

The opposite color is the result of 255 take away the value of its respective color.

Tested code on a TextLabel:

while true do
	Redrandom = math.random(0,255)
	Greenrandom = math.random(0,255)
	Bluerandom = math.random(0,255)
	script.Parent.BackgroundColor3 = Color3.new(Redrandom/255,Greenrandom/255,Bluerandom/255) -- Original Color
	script.Parent.TextColor3 = Color3.new((255-Redrandom)/255,(255-Greenrandom)/255,(255-Bluerandom)/255) --Opposite Color
	script.Parent.TextStrokeColor3 = Color3.new((255-Redrandom)/255,(255-Greenrandom)/255,(255-Bluerandom)/255) --Opposite Color
	wait(math.random(1,10)/10)
end

I understand there's a different function to create a color using RGB colors up to 255, but I prefer this method.

2 comments

0

Doing my research, your method seems like it should work. What exactly is wrong with this, I understand that it's not 100% efficient, but what doesn't work?

0

Nothing's wrong, but I'm trying to get it as efficient as possible

1 answer

From what I can tell, this method is perfectly fine, but if you're really concerned about performance, I would imagine this would be faster than all that division (division is relatively slow):

local Colour1 = Color3.new(0.5, 0.5, 1.0) -- A light blue colour.
local Colour2 = Color3.new(1 - Colour1.r, 1 - Color1.g, 1 - Colour1.b) -- Probably some orange colour.

However, my main advice to you is that you should not worry about performance unless it is a really big issue. Optimising your scripts will often take more effort than it's worth and will likely increase your code's complexity.

1 comment

1

In this case, the efficiency difference isn't going to be even close to noticeable.

This discussion is preserved as it was. New answers, comments, and votes are closed.