Last active
January 19, 2017 19:56
-
-
Save letsgetrandy/99e8a77e73755253baa2 to your computer and use it in GitHub Desktop.
This file contains 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 Color(r, g, b) { | |
if (Array.isArray(r)) { | |
colors = r; | |
} else if (!g && !b) { | |
colors = this.asRGB(r); | |
} else { | |
colors = [r, g, b]; | |
} | |
this.r = colors[0]; | |
this.g = colors[1]; | |
this.b = colors[2]; | |
}; | |
Color.prototype = { | |
asHex: function() { | |
return [this.r, this.g, this.b].map(function(dec) { | |
return (256 + dec).toString(16).slice(1); | |
}).join(''); | |
}, | |
asRGB: function(hex) { | |
if (!hex) return [this.r, this.g, this.b]; | |
return hex.replace(/\#([0-9a-f])([0-9a-f])([0-9a-f])$/i, function(m, r, g, b) { | |
// expand 3-digit hex into 6-digit | |
return '#' + r + r + g + g + b +b; | |
}).match(/\#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i) | |
.slice(1).map(function(a){return parseInt(a, 16);}); | |
}, | |
toString: function() { | |
return this.asHex(); | |
}, | |
valueOf: function() { | |
return (this.r << 16) + (this.g << 8) + this.b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment