Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i convert ARGB to HEX in JavaScript?

I want to convert ARGB colors to CSS-compatible hex

for example:

-1 should be converted to #FFFFFF

or

-16777216 to #000000

How can I do that in JavaScript?

like image 275
CodeChiller Avatar asked Sep 25 '12 09:09

CodeChiller


1 Answers

This discards any alpha component

function argbToRGB(color) {
    return '#'+ ('000000' + (color & 0xFFFFFF).toString(16)).slice(-6);
}

Your colors are signed 32-bits and full alpha (0xFF) makes the numbers negative. This code works even for unsigned numbers.

like image 103


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!