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

Inverting a Color3 using the smallest amount of lines possible?

I was wondering whether I could make some code that essentially reverses the value of each color segment (R,G,B).

I am wanting to reverse it completely whilst keeping the range from 0.5 equal between both new and original values.

Examples:

(0,0,0) = (1,1,1)

(0.25,0.7,0) = (0.75,0.3,1)

Here is what I have figured out - but it is not very good in terms of writing efficiency.

function invertColor(color)
	local am = 0.5
	local equation = {r = am-color.r,g = am-color.g,b = am-color.b}
	local r,g,b
	if color.r > 0.5 then
		r = am-equation.r
	else
		r = am+equation.r
	end
	if color.g > 0.5 then
		g = am-equation.g
	else
		g = am+equation.g
	end
	if color.b > 0.5 then
		b = am-equation.b
	else
		b = am+equation.b
	end
	return Color3.new(r,g,b)
end

Is there a way I can lower the amount of code I have to write?


Thank you!

~TDP

1 comment

0

You will need to check the range though, (x - 1) * -1

1 answer

I'm not quite sure, but this is what I came up with,

local function InvertColor3(color)
	return(Color3.new(1-color.r, 1-color.g, 1-color.b))
end

print(InvertColor3(Color3.new(0.5, 0.7, 0.2))) -- 0.5, 0.3, 0.8

print(InvertColor3(Color3.fromRGB(255, 100, 0))) -- 0, 0.607843, 1

This does what you asked in the question, I believe.

It just subtracts the color values from the max, or 1, throughout.

1 comment

0

The problem is that I want to be able to check whether it needs to add or subtract - and then do it.

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