Colors.js 618 Bytes
Newer Older
1 2 3 4 5 6 7 8 9
/* eslint-env node */

function createRandomColorChannel() {
  let value = Math.floor(Math.random() * 256);
  value = (value + 255) / 2;
  return value;
}

function componentToHex(c) {
10 11 12 13 14
  var hex = Number(c).toString(16);
  if (hex.length < 2) {
    hex = "0" + hex;
  }
  return hex;
15 16 17 18 19 20 21 22
}

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

class Colors {

23
  static createRandomColor() {
24 25 26
    let r = createRandomColorChannel(),
      g = createRandomColorChannel(),
      b = createRandomColorChannel();
27
    return rgbToHex(r, g, b);
28 29 30 31 32
  }

}

module.exports = Colors;