Colors.js 589 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* eslint-env node */

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

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);
}

class Colors {

20
  static createRandomColor() {
21 22 23 24 25 26 27 28 29
    let r = createRandomColorChannel(),
      g = createRandomColorChannel(),
      b = createRandomColorChannel();
    return rgbToHex(r,g,b);
  }

}

module.exports = Colors;