forked from amussey/hex-to-rgb.js
/
hex-to-rgb.min.js
4 lines (4 loc) · 1.11 KB
/
hex-to-rgb.min.js
1
2
3
4
// hex-to-rgb.js -- Functions for Hex/RGB conversion
// Copyright (c)2014 Andrew Mussey, amussey.com
// For license and usage examples, see github.com/amussey/hex-to-rgb
function hexToRgb(e,t){var n=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);var r=function(){if(this.alpha==undefined){return"rgb("+this.r+", "+this.g+", "+this.b+")"}if(this.alpha>1){this.alpha=1}else if(this.alpha<0){this.alpha=0}return"rgb("+this.r+", "+this.g+", "+this.b+", "+this.alpha+")"};if(t==undefined){return n?{r:parseInt(n[1],16),g:parseInt(n[2],16),b:parseInt(n[3],16),toString:r}:null}if(t>1){t=1}else if(t<0){t=0}return n?{r:parseInt(n[1],16),g:parseInt(n[2],16),b:parseInt(n[3],16),alpha:t,toString:r}:null}function rgbToHex(e,t,n){function s(e){var t=e.toString(16);return t.length==1?"0"+t:t}if(t==undefined||n==undefined){if(typeof e=="string"){var r=/^rgb[a]?\(([\d]+)[ \n]*,[ \n]*([\d]+)[ \n]*,[ \n]*([\d]+)[ \n]*,?[ \n]*([.\d]+)?[ \n]*\)$/i.exec(e);return rgbToHex(parseInt(r[1]),parseInt(r[2]),parseInt(r[3]))}if(e.r==undefined||e.g==undefined||e.b==undefined){return null}return rgbToHex(e.r,e.g,e.b)}var i=e;return"#"+s(i)+s(t)+s(n)}