More operators for Color3

It’s been a long time coming, but we need more operators color Color3. Ever since the release of gui (2010, I believe), the uses for Color3s have exploded. Before, it’s use was exclusive only to Sparkles. Because there aren’t any operators for Color3, we have to do arithmetic on each component separately. For example, to tween from one color to another, we have to use code that looks something like this:

local function tweenColor(c0, c1, a) -- c0: starting color, c1: ending color, a: alpha
	local r0, g0, b0 =  c0.r, c0.g, c0.b
	local r1, g1, b1 =  c1.r, c1.g, c1.b
	return ( Color3.new(r0 + (r1 - r0) * a, g0 + (g1 - g0) * a, b0 + (b1 - b0) * a) )
end

Code like this isn’t really newb-friendly, and, considering that functions like these are called per frame, running this on several objects can be costly.

Here is a table of operators I shamelessly ripped off from Vector3:

[table]
[tr]
[td]Color3 u + Color3 v[/td]
[td]Adds each component of u to its corresponding component in v[/td]
[/tr]
[tr]
[td]Color3 u - Color3 v[/td]
[td]Subtracts each component of v to its corresponding component in v[/td]
[/tr]
[tr]
[td]Color3 u / Color3 v[/td]
[td]Divides each component of u by its corresponding component in v[/td]
[/tr]
[tr]
[td]Color3 u * Color3 v[/td]
[td]Multiplies each component of u by its corresponding component in v[/td]
[/tr]
[tr]
[td]number k * Color3 v
Color3 v * number k[/td]
[td]Multiplies each component of v by k[/td]
[/tr]
[tr]
[td]Color3 v / number k [/td]
[td]Divides each component of v by k[/td]
[/tr]
[tr]
[td]number k / Color3 v[/td]
[td]Returns a Color3 with k divided by each component of v[/td]
[/tr]
[/table]

With these operators in place, the code on top can be simplified to:

tweenedColor = c0 + (c1 - c0) * a
1 Like

A little bit hackish, but you can also just use Vector3s as “inbetween” value. On the moment you actually need the “real” color3, you convert it to a Color3 value. Now you can use all Vector3 functions on Color3. Yay.

Example.

local function MakeColor(r,g,b)
return Vector3.new(r or 0,g or 0 ,b or 0)
end
local function ConvertColor(v)
return Color3.new(v.x, v.y, v.z)
end
c = MakeColor(50/255,100/255,150/255)-
-- i herd u liek to tween colors
d = MakeColor(100/255,150/255,200/255)

local scr = Instance.new("ScreenGui", game.Players.LocalPlayer.PlayerGui)
local fr = Instance.new("Frame", scr)
fr.Size = UDim2.new(1,0,1,0)
fr.BackgroundColor3 = ConvertColor(c)
for i = 1, 90 do
local lf = i/90
fr.BackgroundColor3 = ConvertColor(c:Lerp(d,lf))
end
print("awesome")

But what happens when you add/subtract above/below 1/0? Does it constrain to preserve intended appearance or does it allow it to preserve logical behavior?

Also we need a color3.new that accepts 0-255 values so we don’t have to divide by 255 every time we want to make one.

[quote] But what happens when you add/subtract above/below 1/0? Does it constrain to preserve intended appearance or does it allow it to preserve logical behavior?

Also we need a color3.new that accepts 0-255 values so we don’t have to divide by 255 every time we want to make one. [/quote]

More than 1 looks as same as 1 and below 0 looks as same as 0.

Color3.new(1,1,1)~=Color3.new(99,5,47) --These are same white Color3.new(0,0,0)~=Color3.new(-99,0,0) --These are same black Color3.new(1,0,0)~=Color3.new(99,0,0) --These are same red
They’re rendered as the same color but if you’re going to do math on them, they’re going to act as numbers as you see.

Color3.new(5,0,0) - Color3.new(1,0,0) --Result looks pure red Color3.new(5,0,0) - Color3.new(4,0,0) --Result looks pure red Color3.new(5,0,0) - Color3.new(5,0,0) --Result looks black Color3.new(5,0,0) - Color3.new(9,0,0) --Result looks black

Also, we don’t need new Color3.new thing for characters. It’s only necessary when you’re using characters while creating a image. In roblox, you don’t need it. Dividing it by /255 is just plain stupid since it accepts ANY value between 0 and 1. With dividing by /255, you’re just wasting process power.
Lets say we want a random color, if we do:

Color3.new(math.random(0,255)/255,math.random(0,255)/255,math.random(0,255)/255)

then you’re wasting a HUGE amount of process power when you could do:

Color3.new(math.random(),math.random(),math.random())

even more efficiency:

local r=math.random local color = Color3.new(r(),r(),r())

I can’t seem to make out what your post is saying. Do you mind rewording it?