JavaScript randomly generated colors and the conversion of hexadecimal colors to RGB color values

Source: Internet
Author: User


/** * Randomly generated color * @return Randomly generated hex color */function Randomcolor () {var colorstr=math.floor (Math.random () *0xffffff). ToString (  ). toUpperCase (); Return "#" + "000000". Substring (0,6-COLORSTR) +colorstr;}

  

/** * Hex color converted to RGB color * @param color to convert the hexadecimal color * @return RGB color */function colorhextorgb (color) {color=color.touppercase ();    var Regexphex=/^#[0-9a-fa-f]{3,6}$/;//hex if (regexphex.test (color)) {var hexarray=new Array ();    var count=1; for (Var i=1;i<=3;i++) {if (color.length-2*i>3-i) {Hexarray.push (number ("0x" +color.substring (count,count+2        )));      count+=2;        }else{Hexarray.push (Number ("0x" +color.charat (count) +color.charat (count));      Count+=1;  }} return "RGB (" +hexarray.join (",") + ")";  }else{return color; }}

  

/** * RGB color to hexadecimal color * @param color the RGB color to convert * @return hex Color * * Function Colorrgbtohex (color) {var regexprgb=/^ (rgb| RGB) ([0-9]{1,3},s?[ 0-9]{1,3},s? [0-9] {1,3}) $/;//rgb if (regexprgb.test (color)) {Color=color.replace (/((|) | rgb|    RGB) */g, ""). Split (",");    var colorhex= "#";      for (Var i=0;i<color.length;i++) {var hex=number (Color[i]). toString (16);      if (hex.length==1) hex= "0" +hex;    Colorhex+=hex;  } return Colorhex;  }else{return color; }}

  

JavaScript randomly generated colors and the conversion of hexadecimal colors to RGB color values

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.