Mark Niemann-Ross

23 July

Colored by Kuler

You'll notice that this blog changes color each time you visit. I've been experimenting with the Kuler Website and have set up a php script that randomly chooses a color scheme, and then assigns color to the various objects in the CSS.

Here's the php code that drives it...

First - here's the function that sorts the hex color values from lightest to darkest. It does this by breaking the hex string into its RGB components, then sorting on the sum of the three values. Therefore, #FFFFFF will be lighter than #000000
	function hexColorCmp($aValue,$bValue) {
	 	$aValueSplit = str_split($aValue, 2);
		$aValueSum = hexdec($aValueSplit[0]) + hexdec($aValueSplit[1]) + hexdec($aValueSplit[2]);
	 	$bValueSplit = str_split($bValue, 2);
		$bValueSum = hexdec($bValueSplit[0]) + hexdec($bValueSplit[1]) + hexdec($bValueSplit[2]);
		return $aValueSum - $bValueSum;
	 	}
This is the function that gets the rss feed from kuler, converts it to xml, then grabs the color data and theme information.
	function init() {
		do {	
			$thisThemeID = rand(11,16065);
			$kulerURL = 'http://kuler.adobe.com/kuler/API/rss/search.cfm?searchQuery=themeID:' . $thisThemeID;
			$kulerRoot = new SimpleXMLElement($kulerURL, NULL, TRUE);
			} while ($kulerRoot->channel->recordCount == "0") ;
		$kulerItem = $kulerRoot->channel->item->children('http://kuler.adobe.com/kuler/API/rss/');
		$this->previewKulerURL = $kulerItem->themeItem->themeImage;
		$this->nameKuler = $kulerItem->themeItem->themeTitle;
		$this->themeURL = 'http://kuler.adobe.com/#themeID/' . $thisThemeID;
		foreach ($kulerItem->themeItem->themeSwatches->swatch as $swatchValues) {
			$this->swatchHexValues[] = $swatchValues->swatchHexColor ;
			//echo $swatchValues->swatchHexColor;
			}
		usort ($this->swatchHexValues,array($this, "hexColorCmp"));

		for ($i=0;$i<5;$i++) {
		   if (empty($this->swatchHexValues[$i])) $this->swatchHexValues[$i] = dechex(16777216/($i+1));
		   }
	 }

$swatchHexValues[] is then used to assign colors.

Sometimes it looks good - sometimes not. My algorithm for choosing foreground vs background color is pretty simplistic.


posted at 17:38:12 on 07/23/07 by mnr - Category: Code

Comments

No comments yet

Add Comments

This item is closed, it's not possible to add new comments to it or to vote on it
posted at on by - Category: