[PHP] Some Colour Code (COLOURlovers)

Ok so some may or may not know I have been playing about with the idea of making my sites colour scheme more dynamic so I have come up with a variety of functions and pieces of code.

***EDIT:// version 0.7 (All documented) Alot of the functions aren’t used in the COLOURlovers integration but can be used for extra stuff.***

**Core Functions (‘Analyse Functions’)
**


/****************************************************************/
/*                     ANALYSE FUNCTIONS                        */
/****************************************************************/
function darkValue($hexColour) {
// This function gives a 'darkValue' of the colour given as a parameter.
// The higher the number the darker the colour, the darkest possible 
// (black) is 765
    $values = array(
        '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5,
        '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'A' => 10,
        'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15,
        '0' => 0
    );
    $decimal = str_split($hexColour);
    $i = 0;
    $decimal_values = array();
    foreach ($decimal as $value) {
        $decimal_values[$i] = $values[$value];
        $i++;
    }
    $lightness = 0;
    $lightness += $decimal_values[0]*16;
    $lightness += $decimal_values[1];
    $lightness += $decimal_values[2]*16;
    $lightness += $decimal_values[3];
    $lightness += $decimal_values[4]*16;
    $lightness += $decimal_values[5];
    $darkness = 765 - $lightness;
    return $darkness;
}


function lightPercent($hexColour) {
// This function returns the lightPercent, where 100 equals 100 percent
// and is pure white. It's used to determine how light/bright a colour
// is.
    $darkness = darkValue($hexColour);
    $lightPercent = ($darkness/765)*100;
    $lightPercent = 100 - $lightPercent;
    return $lightPercent;
}


function colourContrast($colour_1, $colour_2) {
// This will return the percentage contrast of two colours, it will always
// be in it's positive form.
    $colour_one = lightPercent($colour_1);
    $colour_two = lightPercent($colour_2);
    $minus = ($colour_one - $colour_two);
    $squared = $minus * $minus;
    $contrast = sqrt($squared);
    return $squared/100;
}

**Modify Functions
**


/****************************************************************/
/*                      MODIFY FUNCTIONS                        */
/****************************************************************/
function splash_of_red($original, $amount) {
// Adds a 'splash' of red, the amount parameter is in decimal 
// format and can be a max of 255 (16 to power of 2), if the red 
// value is higher than 255 after being modified then it will be 
// returned as 255 (the max in RGB hex). The colour is then returned
// in hex format completely.
    $rgb_hex = str_split($original, 2);
    $red_dec = hexdec($rgb_hex[0]);
    $modified = $red_dec + $amount;
    if ($modified < 257) { 
        $mod_hex = dechex($modified);
        $rgb_hex[0] = $mod_hex;
        $final = implode('', $rgb_hex);
        return $final;
    } else {
        return $original;
    }
}
function splash_of_green($original, $amount) {
// Adds a 'splash' of green, the amount parameter is in decimal 
// format and can be a max of 255 (16 to power of 2), if the green 
// value is higher than 255 after being modified then it will be 
// returned as 255 (the max in RGB hex). The colour is then returned
// in hex format completely.
    $rgb_hex = str_split($original, 2);
    $red_dec = hexdec($rgb_hex[1]);
    $modified = $red_dec + $amount;
    if ($modified < 257) { 
        $mod_hex = dechex($modified);
        $rgb_hex[1] = $mod_hex;
        $final = implode('', $rgb_hex);
        return $final;
    } else {
        return $original;
    }
}
function splash_of_blue($original, $amount) {
// Adds a 'splash' of blue, the amount parameter is in decimal 
// format and can be a max of 255 (16 to power of 2), if the blue 
// value is higher than 255 after being modified then it will be 
// returned as 255 (the max in RGB hex). The colour is then returned
// in hex format completely.
    $rgb_hex = str_split($original, 2);
    $red_dec = hexdec($rgb_hex[2]);
    $modified = $red_dec + $amount;
    if ($modified < 257) { 
        $mod_hex = dechex($modified);
        $rgb_hex[2] = $mod_hex;
        $final = implode('', $rgb_hex);
        return $final;
    } else {
        return $original;
    }
}
function primary_colour($colour) {
// Returns the primary/base colour of the colour given as a 
// parameter. If the colour is mainly red it returns red, blue
// it returns blue, etc, etc. Still very primitive and only returns
// either red, blue or green.
    $rgb_hex = str_split($colour, 2);
    asort($rgb_hex);
    $i = 0;
    foreach($rgb_hex as $key => $value) {
        if ($i = 2) { $colour_pos = $key; }
    }
    switch($colour_pos) {
        case 0:
            return 'red';
            break;
        case 1:
            return 'green';
            break;
        case 2:
            return 'blue';
            break;
    }
}

**COLOURlovers Integration requires PEAR XML_RSS
**

/****************************************************************/
/*                   COLOURlovers INTEGRATION                   */
/****************************************************************/
/* Requires PEAR XML_RSS                                        */
/****************************************************************/

//header("Content-type: text/css"); Only needed if used in an external
// CSS file save with a .php ending.

// Include the PEAR XML_RSS package.
require_once "XML/RSS.php";

// Parse the Top 10 colour palletes feed from COLOURlovers.
$feed_url = 'http://feeds.feedburner.com/ColourloversCoup0Palettes/Top?format=xml';
$rss =& new XML_RSS($feed_url);
$rss->parse();

// This loop generates a random number between 1 and 5 and then gets that items
// description (which is the data we need).
$i = 0;
$target = rand(1, 5);
foreach ($rss->getItems() as $item) {
    if ($i == $target) {
        $top = $item['description'];
    }
    $i++;
}

// Gets rid of the image data and leaves just the alt atribute which contains...
// the palletes hex values.
$corrections = array (
'|.*" alt="|' => '',
'|" />|' => ''
);
$top = preg_replace(array_keys($corrections), array_values($corrections), $top);

// This splits the one string of colours into an array
$colours = explode('/', $top);

// This chunk of code gets the darkValue for each colour and then sorts them.
$darkness_and_colours = array();
foreach($colours as $colour) {
$darkness_and_colours[$colour] = darkValue($colour);
}
arsort($darkness_and_colours);
$i = 1;

// Puts colour values into an array for easy use.
foreach($darkness_and_colours as $key => $value) {
$colours_in_order[$i] = $key;
$i++;
}

//         Darkest
//$colours_in_order[1]
//$colours_in_order[2]
//$colours_in_order[3]
//$colours_in_order[4]
//$colours_in_order[5]
//         Lightest


**Final Notes
**
I’m still working on this and will keep this thread updated, i thought I would just share what I have done so far, you can see it in use at www.digitalcarnival.com where it picks a random palete from the top five on COLOURlovers (the example above).