# Color3.new(0xD91E18) or Color3.new("D91E18")

Whichever one’s more efficient.

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.

4 Likes
``````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.

2 Likes

Would you be using this at runtime (in scripts) or only in Studio?

Color3.new(0xD91E18) would be nice to have.
Would be even nicer if it accepted both a string and a hex number.

Small correction needed, should be `tonumber(x, 16)/255` (255 instead of 256)

I always learned 255, but 256 seems more likely because 2^256 and 0-255 are 256 different values.

You want the result to be between 0 and 1, not between 0 and 255/256. You have to divide by the maximum value, so 255.

2 Likes

And it’s `%x` instead of `%h` for hexadecimal values in patterns .

Wat. Thats a thing? Wish I knew that before I made my RGB to Hex stuff.

Oops, I guess %h is for string.format?
I only remember stuff like %s, %w and %d…

Anyway, back to the topic.

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.

Here are some fun color-related functions:

Hex Color Function

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("#","")
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

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

Edit: How do you make those nice code-boxes?

1 Like

– CODE

or

``````[code]
-- CODE
[/code]``````

```lua