Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- local function Color3FromHex2(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
- local n = 100000
- local start = tick()
- for i = 1, n do
- local c = Color3FromHex("#ffffff")
- end
- print("#ffffff:", ((tick() - start) / n) * 1000, "ms")
- wait(0.1)
- local start = tick()
- for i = 1, n do
- local c = Color3FromHex("000000")
- end
- print("000000:", ((tick() - start) / n) * 1000, "ms")
- wait(0.1)
- local start = tick()
- for i = 1, n do
- local c = Color3FromHex("#f00")
- end
- print("#f00:", ((tick() - start) / n) * 1000, "ms")
- wait(0.1)
- local start = tick()
- for i = 1, n do
- local c = Color3FromHex("0a0")
- end
- print("0a0:", ((tick() - start) / n) * 1000, "ms")
- wait(0.1)
- local start = tick()
- for i = 1, n do
- local c = Color3FromHex("#F")
- end
- print("#F:", ((tick() - start) / n) * 1000, "ms")
- wait(0.1)
- local start = tick()
- for i = 1, n do
- local c = Color3FromHex2("ffffff")
- end
- print("hex2 ffffff:", ((tick() - start) / n) * 1000, "ms")
- wait(0.1)
- local start = tick()
- for i = 1, n do
- local c = Color3FromHex2("f00")
- end
- print("hex2 f00 (broken):", ((tick() - start) / n) * 1000, "ms")
- wait(0.1)
- local start = tick()
- for i = 1, n do
- local c = Color3FromHex(0xffffff)
- end
- print("0xffffff:", ((tick() - start) / n) * 1000, "ms")
- wait(0.1)
- print(Color3FromHex("#ffffff"))
- print(Color3FromHex("000000"))
- print(Color3FromHex("#f00"))
- print(Color3FromHex("0a0"))
- print(Color3FromHex("#F"))
- print(Color3FromHex(0xffffff))
- print(Color3FromHex(0xffffffff))
- print(Color3FromHex2("f00"))
- --[[print(0xffffff)
- print(tostring(0xffffff))
- print(0xffffff % 256, math.floor(0xffffff / 256) % 256, math.floor(0xffffff / (256^2)) % 256)]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement