Jump to content

rgba from tocolor


xXMADEXx

Recommended Posts

Posted

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.

Posted
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.

I used to know how to code, but then I took an arrow in the knee.

Project Redivivus - Remaking Old School MTA With New Code

MTA 0.6 Nightly 1 released

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...