Created
February 20, 2013 12:27
-
-
Save keithamus/4995188 to your computer and use it in GitHub Desktop.
RGB to HEX, HEX to RGB Can work with small hexes ('#000', '#f0c'), and RGB object ({r: 0, g: 0, b: 0})
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
function hex2rgb(hex) { | |
var int = parseInt(hex.replace(/^#/, '').replace(/^#/, '').replace(/^([\da-f])([\da-f])([\da-f])$/, '$1$1$2$2$3$3'), 16); | |
return { | |
r: (int >> 16) & 255, | |
g: int >> 8 & 255, | |
b: int & 255 | |
}; | |
} | |
function rgb2hex(r, g, b) { | |
if (isNaN(r)) g = r.g, b = r.b, r = r.r; | |
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment