New Color3 Functions

Hi, we just enabled a few new functions to make Color3s easier to use.

Color3 Color3.fromRGB(r, g, b)
Works like Color3.new but takes numbers from 0-255 instead of 0-1

Color3 Color3.fromHSV(h, s, v)
Creates a Color3 from hsv values. The values have a range from 0 to 1.

h, s, v Color3.toHSV(Color3)
Gives you the hsv values of a Color3

Color3 Color3:lerp(Color3, alpha)
Lets you interpolate between two Color3 objects. Alpha is a number from 0 to 1.

These are live now. Let me know if you find any bugs!

70 Likes

The RGB addition makes me a happy dev.

9 Likes

Seems to be missing HEX and CMYK additions. I am happy to see these additions though.

2 Likes

COLOR3 LERP COLOR3 LERP COLOR3 LERP COLOR3 LERP COLOR3 LERP COLOR3 LERP COLOR3 LERP COLOR3 LERP COLOR3 LERP aaaaaa

Thank you ROBLOX team :heart:

5 Likes

Vector3.new(r,g,b):lerp(Vector3.new(r2,g2,b2), x)

That’s really hacky. Inefficient, even. Color3 lerp is much faster and a much more efficient method to do this

1 Like

I use it to animate a bunch of imagebuttons using .RenderStepped and don’t have any issues. While I do think it’d be nice to have, I don’t think it’s a ridiculous workaround.

Why do you convert to Vector3 instead of just interpolating the values individually? Internally it does a linear interpolation like this:

result = Color3.new(
   (b.r - a.r)*alpha + a.r,
   (b.g - a.g)*alpha + a.g,
   (b.b - a.b)*alpha + a.b
)

Which is what I used to do in lua until now

3 Likes

What’s bad about linear? The X is where you can do your fun(ctions).

I don’t understand. Vector3 lerp is just doing a linear interpolation. I’m wondering why you were converting your Color3 to a Vector3 and then interpolating, instead of just interpolating the numbers individually.

They do the same thing and mine is easier to read, at least for me.

This excites me

2 Likes

Any chance we’ll get Color3.fromHEX(str) sometime?

Yes! Lerping! Finally!!! Time to start coloring.

It would probably be Color3.FromHEX(Number)
Hexadecimals are converted to numbers naturally in Lua, just takes a bit of formatting.
.#FF8000
for Lua would just be 0xFF8000

Oh cool! Thanks for scalar multiplication and addition:

scalar * color : Color3 . new ( 0 , 0 , 0 ) : lerp ( color , scalar ) color1 + color2 : Color3 . new ( 0 , 0 , 0 ) : lerp ( color1 : lerp ( color2 , 0.5 ) , 2 )

Thought I would put this to the test to see if they would run at similar speeds, and if not which would be faster.

I have predeclared colors run through your method, Sharksie’s method, and the new Color3:Lerp() function a thousand times and averaged.

Results:

Sharksie: 0.0000036404132843018 seconds
ScriptOn: 0.0000064222812652588 seconds
Color3:Lerp(): 0.0000012698173522949 seconds

From my results, it seems your method runs around 1.76 times slower than Sharksie’s, and 5.05 times slower than the new function.

Not the most organized code, but it gets the job done. This is the source code on PasteBin.

Quickly back on the topic of these new functions:
WOOT!

12 Likes

Well, if you’re going for speed, consider what you’re actually telling the computer to do.
In Sharksies code, he’s having the computer go and fetch a.r, a.g, and a.b twice.

Uh. Here’s some benchmark code.
http://pastebin.com/4dnMr0w7

scripton time:			didnt bother; too slow.
sharksie time:			0.00000302
improved sharksie time:		0.00000225
localised ialpha time:		0.00000236
built-in lerp time:		0.00000111
localized built-in lerp time:	0.00000071
no Color3 time:			0.00000026
6 Likes

Hue, try this one:

local c3 = Color3.new
local floor = math.floor
local function Color3FromHex(str)
	if type(str) == "number" then -- in case of hexadecimal numbers, we can do this!
		return c3(floor(str / (256^2)), floor(str / 256) % 256, str % 256)
	end
	assert(type(str) == "string" and str:gsub("^#", ""):match("^%x+$"), "Input must be a hexadecimal string or number")
	str = str:gsub("^#", ""):upper()
	if #str == 3 then
		str = str:sub(1, 1):rep(2) .. str:sub(2, 2):rep(2) .. str:sub(3, 3):rep(2)
	elseif #str == 1 then
		str = str:rep(6)
	end
	if #str == 6 then
		return c3(tonumber(str:sub(1, 2), 16) / 255, tonumber(str:sub(2, 3), 16) / 255, tonumber(str:sub(4, 5), 16) / 255)
	else
		return c3()
	end
end

Estimated computation time is roughly 0.0000035 seconds, from 100k benchmark testing.

Example of usage:

print(Color3FromHex("#ffffff"))
print(Color3FromHex("000000"))
print(Color3FromHex("#f00"))
print(Color3FromHex("0a0"))
print(Color3FromHex("#F"))
print(Color3FromHex(0xffffff))
print(Color3FromHex(0xffffffff)) -- oh boy, this is one weird Color3

Benchmarking results for different cases:

#ffffff: 0.0048357105255127 ms
000000: 0.0038546824455261 ms
#f00: 0.0050603127479553 ms
0a0: 0.0048992609977722 ms
#F: 0.0039971542358398 ms
hex2 ffffff: 0.0023480939865112 ms
hex2 f00 (broken): 0.0027719211578369 ms
0xffffff: 0.0014664435386658 ms

Hex2 is einstein’s modification.

Full code found here:

2 Likes

Can’t you do?

tonumber(someHexString, 16)