Almost all color palette creator sites and programs give you hex values to copy and paste. Most also give RGB values but not all. I can’t remember specific instances but I do recall having to use hex to RGB conversion websites frequently the last time I designed a big GUI. Hex values are easier to copy and paste since you don’t have to go back and forth with 3 different numbers.
It’s a small thing but it would be very convenient and doesn’t seem like it would take long to implement.
local function fromHex(str)
local r,g,b = str:match("(%h%h)(%h%h)(%h%h)")
r = tonumber(r,16)/256
g = tonumber(g,16)/256
b = tonumber(b,16)/256
return Color3.new(r,g,b)
end
Should work I think?
Of course, a real implementation would be nice.
When you call Color3.new(num) where num > 1 it will still give the color (num, 0, 0), so it seems intended behaviour that the numbers are not clamped when they are put into the function.
Therefore, the Color3.new(hexnumber) variant wouldn’t work, since that would just result in (hexnumber, 0, 0), there’s no way to distinguish between the two type of calls, since in Lua there is no difference between hexadecimal numerical input and simply decimal numerical input apart from their representation before they are parsed:
> print(0xA0 == 160)
true
What would be nice is to have two things:
In Studio, we should be able to paste “#RRGGBB” and “0xRRGGBB” into Color3 fields in the Properties view.
There should be a function Color3.hex(number) that treats the passed number as a hexadecimal number, and parses the color channels from that accordingly.
function DecimalToHex(IN)
local B,K,OUT,I,D=16,“0123456789ABCDEF”,“”,0
while IN>0 do
I=I+1
IN,D=math.floor(IN/B),math.fmod(IN,B)+1
OUT=string.sub(K,D,D)…OUT
end
return OUT
end
function HexToRGB(hex)
local hex = hex:gsub(“#”,“”)
return tonumber(“0x”…hex:sub(1,2)), tonumber(“0x”…hex:sub(3,4)), tonumber(“0x”…hex:sub(5,6))
end
function RGBToHex(RVal, GVal, BVal)
local RVal=DecimalToHex(RVal);
if (string.len(RVal)<2) then
RVal=(‘0’…RVal)
end
local GVal=DecimalToHex(GVal);
if (string.len(GVal)<2) then
GVal=(‘0’…GVal)
end
local BVal=DecimalToHex(BVal);
if (string.len(BVal)<2) then
BVal=(‘0’…BVal)
end
return ‘#’ … RVal … GVal … BVal;
end
Additive Color3 function
function AddColor3(Color1, Color2)
local r1,g1,b1 = Color1.r,Color1.g,Color1.b
local r2,g2,b2 = Color2.r,Color2.g,Color2.b
return Color3.new((r1+r2)/2,(g1+g2)/2,(b1+b2)/2)
end