Is it possible to convert Hex to color3 and back?

So I’m making a system where a player can select a color in order to edit their character. I made a text box where the player would paste their hex value of a color in. The problem is, I don’t know how I would convert the hex value that the player has pasted in into a color3 value. Thanks for help!

1 Like

This is just a hard method I just came up with… you could make a color3 value for each hex value, then if a player inserts a hex value, grab it from the table, and get the color3 value.

There isn’t a built-in one in the Color3 library but you can do it with format strings.

2 Likes

Hi!

I believe I’ve had the same issue a few years ago so I went to look for the solution again. I couldn’t find it in my scrips, but I found a script which I believe does exactly what you need. :slight_smile:

https://gist.github.com/gcaptn/972c899b775c8b7262b2c686fafd8fb5

local function hex(c3)
    local r,g,b = math.floor(c3.R*255), math.floor(c3.G*255), math.floor(c3.B*255)
    return string.format("#%X%X%X", r, g, b)
end

local function c3(hex)      -- from https://gist.github.com/jasonbradley/4357406
    hex = hex:gsub("#","")
    local r, g, b = tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6))
    return Color3.new(r,g,b)
end
4 Likes

There is a method for this that doesnt require hacky string patterns

:ToHex()