Generating a pleasing colour palette

What this does is randomly choose RGB values that are bright and colourful, while not being primary colours.  It gives the application a much nicer look than standard colours.  Below is the code to choose the random colour:

 // figure out a pleasing colour
 // adapted from http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
 $grc = M_PI / 3;
 $h = mt_rand(0, (M_PI / 3) * 85) / 100; // use random start value
 $h += fmod($i, 3) * $grc;
 $h = (1 / M_PI) * $h;

 $rgb = HSVtoRGB(array($h, 1, 1));

And the next portion is to convert the HSV values that were generated above in to useable RGB values for your css eg. rgba(R, G, B, 1.0)

function HSVtoRGB(array $hsv) {
// adapted from http://stackoverflow.com/questions/3597417/php-hsv-to-rgb-formula-comprehension
// original author: http://stackoverflow.com/users/127724/artefacto

list($H,$S,$V) = $hsv;
//1
$H *= 6;
 //2
 $I = floor($H);
 $F = $H - $I;
 //3
 $M = $V * (1 - $S);
 $N = $V * (1 - $S * $F);
 $K = $V * (1 - $S * (1 - $F));
 //4
 switch ($I) {
 case 0:
 list($R,$G,$B) = array($V,$K,$M);
 break;
 case 1:
 list($R,$G,$B) = array($N,$V,$M);
 break;
 case 2:
 list($R,$G,$B) = array($M,$V,$K);
 break;
 case 3:
 list($R,$G,$B) = array($M,$N,$V);
 break;
 case 4:
 list($R,$G,$B) = array($K,$M,$V);
 break;
 case 5:
 case 6: //for when $H=1 is given
 list($R,$G,$B) = array($V,$M,$N);
 break;
 }
 return array(round($R*255), round($G*255), round($B*255));
 }
Published
Categorized as Blog