Hex color and RGB cross-turn

Source: Internet
Author: User

Dd

Color transfer//16 binary color range RGB format color value conversion between//-------------------------------------//16 binary color values regular expression var reg =  /^# ([0-9a-fa-f]{3}| [0-9a-fa-f] {6}) $/;/* rgb color conversion to 16 binary  */string.prototype.colorhex = function () {    var  that = this;    if (/^ (rgb| RGB)/.test (that)) {        var acolor = that.replace (/(?: \ \ (|\) |rgb| RGB) */g, ""). Split (",");        var strhex =  "#";         for (var i=0; i<acolor.length; i++) {             var hex = number (AColor[i]). ToString (+);             if (hex ===  "0") {                hex +=  hex;            }             strHex += hex;        }         if (strhex.length !== 7) {             strHex = that;        }         return strhex;    }else if (Reg.test ( That) {        var anum = that.replace (/#/, ""). Split ("" );         if (anum.length === 6) {             return that;         }else if (anum.length === 3) {             var numhex =  "#";             for (var i=0;  i<anum.length; i+=1) {                 numHex +=  (Anum[i]+anum[i]);             }            return  numhex;        }    }else{         return that;    }};// ---------------------------------- ---------------/* 16 color into RGB format  */string.prototype.colorrgb = 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;         }        //  handling six-bit color values         var sColorChange = [];         for (var i=1; i<7; i+=2) {             scolorchange.push (parseint ("0x" +scolor.slice (i,i+2)));         }        return  "RGB ("  + scolorchange.join (",")  +  " ) ";    }else{        return scolor;     }};

Here's how to call the above function:

if Lickcolor is RGB Domain , you can switch to Hex Color Field

LinkColor = Linkcolor.colorhex ();

If Lickcolor is a hexadecimal color field, it can be converted to an RGB domain by calling

LinkColor = Linkcolor.colorrgb ();


Hex color and RGB cross-turn

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.