js-011-color conversion (RGB to 16 binary; 16 to RGB)

Source: Internet
Author: User

In the development of web pages, often need to color settings, and often need to encounter the issue of color conversion, such as: RGB to 16 binary, 16 to RGB, a few days ago in the test, found that the color of the site to convert a class of 16 colors (for example: #000080, # FA08FA) conversion always prompt color is illegal, looked at the JS source code, found that its conversion method is wrong. Look for a little Niang and Gu uncle, finally wrote a color conversion of small method, the source code is as follows:

  

STRING.PROTOTYPE.COLORHEX2RGB =function(){    varreg =/^# ([0-9a-fa-f]{3}|[ 0-9A-FA-F]{6}) $/; varScolor = This. toLowerCase (); if(Scolor &&reg.test (Scolor)) {        if(Scolor.length = = 4){            varScolornew = "#";  for(varI=1; i<4; I+=1) {scolornew+ = Scolor.slice (i,i+1). Concat (Scolor.slice (i,i+1)); } Scolor=scolornew; }                varScolorchange = [];  for(varI=1; i<7; i+=2) {Scolorchange.push (parseint ("0x" +scolor.slice (i,i+2))); }                return"RGB (" + Scolorchange.join (",") + ")"; }Else{        returnScolor; }}; String.prototype.colorRgb2Hex=function(){    varreg =/^# ([0-9a-fa-f]{3}|[ 0-9A-FA-F]{6}) $/; varthat = This; if(/^ (rgb| RGB)/. Test (that)) {        varAcolor = That.replace (/(?: \ \ (|\) |rgb| RGB) */g, ""). Split (","); varStrhex = "#";  for(vari=0; i<acolor.length; i++){            varHex = number (Acolor[i]). toString (16); if(Hex = = = "0") {hex+=Hex; } Strhex+=Hex; }                if(Strhex.length!== 7) {Strhex=that ; }                returnstrhex.touppercase (); }Else if(Reg.test (that)) {varAnum = That.replace (/#/, ""). Split (""); if(Anum.length = = 6){            returnthat ; }Else if(Anum.length = = 3){            varNumhex = "#";  for(vari=0; i<anum.length; I+=1) {Numhex+ = (anum[i]+Anum[i]); }                        returnnumhex.touppercase (); }    }Else{        returnthat.touppercase (); }};

Examples of Use:

"#000080". Colorhex2rgb ()     ----> rgb (0,0,128)"#FA0080". Colorhex2rgb ()     ----> rgb (250,0,128 )"#888". Colorhex2rgb ()          ----> RGB (136,136,136)"#888888". Colorhex2rgb ()     ----> RGB ( 136,136,136)"RGB (128,128,128)". Colorrgb2hex ()   ---->  #808080 "RGB (255,128,128)". Colorrgb2hex ()   ---->  #FF8080"RGB (128,0,255)". Colorrgb2hex ()       ---->  #8000FF

At this point, js-011-color conversion (RGB to 16 binary, 16 to RGB) successfully completed, I hope this article can give beginners JavaScript you a reference.

Finally, very grateful to the pro-stop, I hope this article can be pro helpful. Warmly welcome the kiss to discuss together and progress together. Thank you so much! ^_^

js-011-color conversion (RGB to 16 binary; 16 to RGB)

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.