Created
November 15, 2011 22:04
-
-
Save nijikokun/1368528 to your computer and use it in GitHub Desktop.
Actual Hex to RGB/ARGB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Math.RGB() | |
// Actual Hex to RGB values, Invalid hex returns 0,0,0 or black. | |
// | |
// By Nijikokun | |
// | |
// Usage: | |
// 8 bit | |
// Math.RGB(0xFF) | |
// | |
// 12 bit | |
// Math.RGB(0xFeF) | |
// | |
// 24 bit | |
// Math.RGB(0xFFEEFF) | |
// | |
// 32 bit, with given alpha | |
// Math.RGB(0x85FFEEFF) | |
// ^ Alpha | |
Math.RGB = function(bit) { | |
var len = bit.toString(16).length; | |
var total = parseInt(bit); | |
var def = { a: 0, r: 0, g: 0, b: 0 } | |
if(total == 0) | |
return def; | |
switch(len) { | |
case 2: return { a: 0, r: total, g: total, b: total } | |
case 3: | |
var s = bit.toString(16); | |
var a = parseInt(s[0], 16), b = parseInt(s[1], 16), c = parseInt(s[2], 16); | |
return { a: 0, r: a + a * 16, g: b + b * 16, b: c + c * 16 } | |
case 6: return { a: 0, r: bit >> 16, g: bit >> 8 & 0xFF, b: bit & 0xFF } | |
case 8: return { a: bit >> 24, r: bit >> 16 & 0xFF, g: bit >> 8 & 0xFF, b: bit & 0xFF } | |
} | |
return def; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment