Jump to content

rgba from tocolor


xXMADEXx

Recommended Posts

Hey guys. So, I'm writing a basic function for bordered text, but I can't figure out how to get RGBA from the tocolor number that is sent in the color argument. I'm trying to make it so that the back texts will have the same alpha as the main text.

Thanks in advance.

Link to comment

tocolor() converts your numbers to Hex and then it converts that Hex back to one decimal number.

tocolor(255,255,255) => 0xFFFFFF => 16777215 (without alpha);

tocolor(255,255,255,255) => 0xFFFFFFFF => 4294967295 (with alpha);

To get the Hex back from the decimal number just format the string "%x" like this:

("%x"):format(tocolor(r,g,b)) 

And that is your Hex.

If you want to go deeper and convert that Hex back to 3 or 4 decimals you do it like this:

local Hex = ("%x"):format(tocolor(r,g,b)); 
local Color = {}; 
for i = 1,6,2 do 
    table.insert(Color,tonumber(Hex:sub(i,i+1),16)); -- supply 16 as an argument because we are converting from base 16; 
end; 

And that is your RGB.

Link to comment
local color = tocolor ( 0, 0, 0, 255 ) 
local r = bitExtract ( color, 0, 8 ) 
local g = bitExtract ( color, 8, 8 ) 
local b = bitExtract ( color, 16, 8 ) 
local a = bitExtract ( color, 24, 8 ) 

tocolor() converts your numbers to Hex and then it converts that Hex back to one decimal number.

No, it does not. tonumber does, which is why you see it as a decimal number.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...