Convert RGB to Hexadecimal with JavaScript

2

I have to convert rgb value to hex when moving the inputs (range), but when I move nothing happens, the hexadecimal color should appear below as well as the color is changed at the top.

The color is only loaded when the page loads, when moving the ranges nothing happens.

Follow the code: link

    
asked by anonymous 16.03.2018 / 15:46

1 answer

4

One way to do this is with the following functions:

function componentToHex(c) {
    var hex = c.toString(16);
    return hex.length == 1 ? "0" + hex : hex;
}

function rgbToHex(r, g, b) {
    return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}

alert(rgbToHex(0, 51, 255)); // #0033ff

For more information, visit this link .

    
16.03.2018 / 15:51