Random colors

Random RGB colors

function randomRGB() {
  var r = Math.floor(Math.random() * 256);
  var g = Math.floor(Math.random() * 256);
  var b = Math.floor(Math.random() * 256);
  return "rgb(" + r + ", " + g + ", " + b + ")";
}


Random HEX colors

function randomHexColor() {
  var hex = Math.floor(Math.random() * 16777215).toString(16);
  return "#" + hex;
}


Gradient colors

function gradientColor(startColor, endColor, steps) {
  var startRGB = hexToRGB(startColor);
  var endRGB = hexToRGB(endColor);
  var rStep = (endRGB[0] - startRGB[0]) / steps;
  var gStep = (endRGB[1] - startRGB[1]) / steps;
  var bStep = (endRGB[2] - startRGB[2]) / steps;
  var gradientColors = [];
  for (var i = 0; i < steps; i++) {
    var r = Math.round(startRGB[0] + rStep * i);
    var g = Math.round(startRGB[1] + gStep * i);
    var b = Math.round(startRGB[2] + bStep * i);
    gradientColors.push("rgb(" + r + ", " + g + ", " + b + ")");
  }
  return gradientColors;
}

function hexToRGB(hex) {
  var r = parseInt(hex.substring(1, 3), 16);
  var g = parseInt(hex.substring(3, 5), 16);
  var b = parseInt(hex.substring(5, 7), 16);
  return [r, g, b];
}