HTML Canvas Color RGB to Hex converter

Introduction

Convert RGB decimal values to a hexadecimal representation.

The valid decimal values for RGB are 0 - 255.

Any (r,g,b) argument values that fall out of that range should be rounded to the closest valid value.

The following are examples of expected output values:

rgb(255, 255, 255) // returns FFFFFF
rgb(255, 255, 300) // returns FFFFFF
rgb(0,0,0) // returns 000000
rgb(148, 0, 211) // returns 9400D3


function rgb(r, g, b) {
  return [r, g, b].reduce((a, b) => {
    if (b < 0) {
      return a + "00";
    }/*w  ww .  j a v a 2 s. com*/
    if (b > 255) {
      return a + "FF";
    }
    let hex = b.toString(16);
    if (hex.length % 2) {
      hex = "0" + hex;
    }
    return a + hex;
  }, "").toUpperCase();
}

console.log(rgb(173, 255, 47));



PreviousNext

Related