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

[Colors] How can I find the opposite Color?

Asked by 8 years ago
Edited 8 years ago

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.

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? Vrakos 109 — 8y
0
Nothing's wrong, but I'm trying to get it as efficient as possible fahmisack123 385 — 8y

1 answer

Log in to vote
2
Answered by 8 years ago
Edited 8 years ago

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
In this case, the efficiency difference isn't going to be even close to noticeable. Perci1 4988 — 8y
Ad

Answer this question