Javascript String hex2Rgb()

Description

Javascript String hex2Rgb()



var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;

/*//from   w w w.  j a  v a2  s  .c  o m
var colorHex = "#34538b"
var ColorRgb = sHex.colorRgb();
*/

String.prototype.hex2Rgb = function(){
 var sColor = this.toLowerCase();
 if(sColor && reg.test(sColor)){
  if(sColor.length === 4){
   var sColorNew = "#";
   for(var i=1; i<4; i+=1){
    sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1)); 
   }
   sColor = sColorNew;
  }

  var sColorChange = [];
  for(var i=1; i<7; i+=2){
   sColorChange.push(parseInt("0x"+sColor.slice(i,i+2))); 
  }
  return sColorChange.join(",");
 }else{
  return sColor; 
 }
};



PreviousNext

Related