New Color3 Functions

Benchmarking your code took around 630ms on my pc (well laptop).
If I edited your code a bit, it suddenly becomes 370ms:

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 str = tonumber(str,16)
	assert(str, "Input must be a hexadecimal string or number")
	return Color3FromHex(str)
end

Faster and cleaner, cuz i can.

EDIT: @Brad_Sharp can u not semi-ninja me?

1 Like

Well, that leaves out the lots of overloads for making hexadecimal colors. Although I didn’t test the number-case of the function, only the string cases.

Weird, @einsteinK - how’d you do your benchmarking?

local n = 100000
local start = tick()
for i = 1, n do
	local c = Color3FromHex(somestring)
end
print((tick() - start) / n)

is what I did for all the different cases.

Same, except leaving the “local c =” part out

EDIT: nvm I just printed math.floor(tick()/1000)

Or just…

hex2rgb = function (hex) --accepts a string (e.g. "#ffffff") hex = hex:gsub("#","") return tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6)) end

These look super useful. Great job.

2 Likes