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.
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.
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
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
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