title: Javascript - Random Color Function (control brightness and contrast)
body: <p>Few days ago I started to search for a JavaScript function that generate random colors.</p>
  
  <p>I found a script and made some changes of my own:</p>
  
  <pre><code>function getRandColor(brightness)
  {
      //6 levels of brightness from 0 to 5, 0 being the darkest
      var rgb = [Math.random() * 256, Math.random() * 256, Math.random() * 256];
      var mix = [brightness*51, brightness*51, brightness*51]; //51 =&gt; 255/5
      var mixedrgb = [rgb[0] + mix[0], rgb[1] + mix[1], rgb[2] + mix[2]].map(function(x){ return Math.round(x/2.0)})    
      var rgb = mixedrgb[2] | (mixedrgb[1] &lt;&lt; 8) | (mixedrgb[0]  &lt;&lt; 16);
      return  '#' + rgb.toString(16);
  }
  </code></pre>
  
  <p>I would like add a contrast parameter to the function. How can this be done?</p>
  
  <p>Also, If you think there is a faster way to do the calculations, please share. Thanks.</p>
  
  <hr>
  
  <p>After a lot of search and testing (because I am very bad in this colors things) I made a function to convert hsl to hex color. I am using hsl because it is much easy to work with hsl properties to make the colors you want.</p>
  
  <p>The Function is really bad and ugly, so help me to make it better. For, example the random function returns duplicate values even in 100 calls.</p>
  
  <pre><code>  function RandomValue(MinValue,MaxValue)
  {
      return parseInt(Math.random()*(MaxValue-MinValue+1), 10)+MinValue;
  }
  
  function GetRandomColor()
  {
      var h = RandomValue(1, 360);   // color hue between 1 and 360
      var s = RandomValue(20, 100);  // saturation 0-100%
      var l = RandomValue(50, 70);   // lightness  0-70%
  
      /* Convert the HSL values to HEX */
  
      return HSLtoRGB(h, s, l);
  }
  
  function HSLtoRGB(h, s, v)
  {
      var r, g, b;
      var i;
      var f, p, q, t;
  
      h = Math.max(0, Math.min(360, h));
      s = Math.max(0, Math.min(100, s));
      v = Math.max(0, Math.min(100, v));
  
      s /= 100;
      v /= 100;
  
      if(s == 0) 
      {
          r = g = b = v;
          return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
      }
  
      h /= 60; 
      i = Math.floor(h);
      f = h - i; 
      p = v * (1 - s);
      q = v * (1 - s * f);
      t = v * (1 - s * (1 - f));
  
      switch(i) 
      {
          case 0:
          r = v;
          g = t;
          b = p;
          break;
  
          case 1:
          r = q;
          g = v;
          b = p;
          break;
  
          case 2:
          r = p;
          g = v;
          b = t;
          break;
  
          case 3:
          r = p;
          g = q;
          b = v;
          break;
  
          case 4:
          r = t;
          g = p;
          b = v;
          break;
  
          default: 
          r = v;
          g = p;
          b = q;
      }
  
      var rgb = Math.round(b * 255) | (Math.round(g * 255) &lt;&lt; 8) | (Math.round(r * 255)  &lt;&lt; 16);
  
      var m=rgb.toString(16);
  
      switch(m.length)
      {
          case 5:return  '#' + rgb.toString(16) + '0';break;
          case 4:return  '#' + rgb.toString(16) + '00';break;
          default:return  '#' + rgb.toString(16);break;
      }
  }
  
  
  for (var i = 0; i &lt; 500; i++) {
      var div = document.createElement('div');
      div.style.backgroundColor = GetRandomColor() ;
      document.body.appendChild(div);
  }
  </code></pre>