Colors.js 937 Bytes
Newer Older
1 2
/* eslint-env node */

3 4 5 6
const COLORS = ["#ff1f5a","#ffd615","#f9ff21","#1e2a78", "#43ab92", "#f75f00", "#c93838", "#512c62", "#f6f078", "#01d28e", "#434982", "#730068"];

var currentColor = 0;

7 8
function createRandomColorChannel() {
  let value = Math.floor(Math.random() * 256);
9
  value = Math.floor((value + 255) / 2);
10 11 12 13
  return value;
}

function componentToHex(c) {
14 15 16 17 18
  var hex = Number(c).toString(16);
  if (hex.length < 2) {
    hex = "0" + hex;
  }
  return hex;
19 20 21 22 23 24 25 26
}

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

class Colors {

27 28 29 30 31 32 33
  static getNextColor() {
    if(currentColor === COLORS.length) {
      currentColor = 0;
    }
    return COLORS[currentColor++];
  }

34
  static createRandomColor() {
35 36 37
    let r = createRandomColorChannel(),
      g = createRandomColorChannel(),
      b = createRandomColorChannel();
38
    return rgbToHex(r, g, b);
39 40 41 42 43
  }

}

module.exports = Colors;