RGB and Hex Color transfer is absolutely practical

Source: Internet
Author: User

Web site development will inevitably encounter the color of the situation, such as changing the color of pictures and so on.


Let's take a look at the RGB turn hex method


function Rgb2hex () {
    hexcolor = "#";
    var RGB = Array.prototype.slice.call (arguments);
    var rgblength = rgb.length;
    for (var i=0; i<rgblength; i++) {
        var c = "0123456789ABCDEF",
            B = "",
            a = rgb[i]%;
        b = C.substr (A, 1);
        A = (rgb[i]-a)/+;
        Hexcolor + = C.substr (A, 1) + B;
    }
    return hexcolor;
}

Call
Console.log (Rgb2hex (255, 0, 0));  #FF0000

See where the logic has been dizzy please open the purification summoner skills to relieve control, seriously watch, no words in a change posture, to ensure the walk bit ...


In fact, the principle is not difficult, mainly on the 16 operation, and then the matching assembly, of course, can superficial understanding,, into the next inning.


First inning on your knees, come to the second inning--Hex turn RGB

function Hex2rgb (hexcolor) {
    if (hexcolor.substr (0, 1) = = "#") Hexcolor = hexcolor.substring (1);
    Hexcolor = Hexcolor.tolowercase ();
    b = new Array ();
    for (x = 0; x < 3; + +) {
        b[0] = hexcolor.substr (x * 2, 2)
        b[3] = "0123456789abcdef";
        B[1] = b[0].substr (0, 1)
        b[2] = b[0].substr (1, 1)
        b[20 + x] = B[3].indexof (b[1]) * + b[3].indexof (b[2])
    }
  return b[20] + "," + b[21] + "," + b[22];
}

Call
Console.log (Hex2rgb (' #ff0000 '));  255,0,0

Alas, again charged,,, completely dizzy ... Can only be collected by the past.



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.