jeremyparkerphotography.com — I have implemented a color-changing background algorithm for my photography website that is pretty cool. It's an interesting use of PHP to calculate the average color of a given photo and use that as the background color. You can see it by viewing any of the photos on the site.
Jul 13, 2005 View in Crawl 4
JerwareJul 13, 2005
Cool, thanks! But I copied all the DLLs * averagecolor.exe to a folder, along with a .gif, ran the program and got the following error:Illegal division by zero at averagecolor.txt line 24.I tried copying the averagecolor.txt script to the folder, but got the same problem. Hm...
jammerdelrayJul 14, 2005
very neat digg
tikzJul 14, 2005
awesome
tenorsJul 14, 2005Submitter
Hey again. Wow! This thread has been pretty busy while I was away. As I promised, I have put the code up on my site. Again, you are free to use this code but a link back to my site or credit would be nice. You can find the code here: <a class="user" href="http://www.jeremyparkerphotography.com/averagecolor.txt">http://www.jeremyparkerphotography.com/averagecolor.txt</a>To answer a few questions, no I don't do this calculation every time the page is loaded. I have a custom backend to add photos to the site. When I add the photo, the colors get calculated and are then stored in a database. Then it's a simple lookup rather than a complex floating point calculation.Also, yes, I wrote all of the code for the site myself rather than using a pre-made software solution. I just thought it would be more fun to do it all myself. It gave me something to do on the weekends for a few weeks anyway ;)Enjoy!
davymacJul 14, 2005
this was so simple yet so brilliant.. thank you my good man
jrlambsJul 14, 2005
The thing that kills me is I didn't even know this mod existed until I read the article, now that I know it exists I've downloaded the mod and am now playing the unlocked games. Thanks Hilary!
straudlaterJul 17, 2005
Follow up to this story: I've combined textamerica and Jeremy's script to make my website change its color scheme every time I've uploaded a new photo. <a class="user" href="http://digg.com/programming/Textamerica_Color_changing_background_algorithm_=_Dynamic_color_scheme">http://digg.com/programming/Textamerica_Color_changing_background_algorithm_=_Dynamic_color_scheme</a>
dsturmFeb 28, 2006
Hey, just great, but I think it would be much faster, not to check every pixel, but to reduce the image to 1x1px and check the color... Should be up to 10 times faster!
wauwtieFeb 13, 2007
I thought I just kick this post in the present time again with a new comment. Digg.com was the one and only place where I found this neat little script to get an average background color. The biggest problem (at least for me it was), was to find a gallery script that was table-less so you can take full effort of the script. [intermezzo] I'm am not affiliated with the two scripts I am going to mention!This little post can hopefully help those noobs (like myself) that want to achieve this effect in their own photo-album. 1) First of all you need to install a table-less gallery script. I for myself found Phormer ( <a class="user" href="http://p.horm.org/er/">http://p.horm.org/er/</a> ) -> Table-less and no database needed. Perfect for the noob like mentioned above!-)2) You need the little "average color snippet" from the link above, made by Jeremy but kind of 'enhanced' and kindly hosted by Murpheek ( <a class="user" href="http://www.murpheek.com/perl.txt">http://www.murpheek.com/perl.txt</a> )If you can script you're done now already. If not,... read on.3) Download 2 changed files over here -> <a class="user" href="http://wauwtie.nl/phormerBGaverage/">http://wauwtie.nl/phormerBGaverage/</a> called index.txt and css.txt (The index.txt is in fact the original index file from Phormer with the snippet of Jeremy added. the css.txt file is in fact the original dynamic css file of Phormer with the bodybackgroundcolor removed) 4) change the name of the 2 files in -> index.php and css.php and upload them to the root of the Phormer installation (The directory where the index of Phormer lives in after the installation)5) Done! Disclaimer! -> Neither Jeremy (the author of the snippet) or Aiden (Phormer) does have anything to do with this 'update / enhancement / or (if you wish) abuse of there php skills! And,.... Please don't blame me for eventually bad scripting!-) In fact,.. please make it better! This post is just for the poor surfer that like me stumbled on this post and really wants to have an album with a average background changing color! Godspeed to all! Or as I like to say,.. Grüezi
ilovebackdropsMay 22, 2008
Great work and very much inspiring. Just one suggestion, keep the contrast as low as possible.
sinrizeApr 15, 2009
This article isnt online - <a class="user" href="http://www.icurtain.co.uk/blog.php?article=179">http://www.icurtain.co.uk/blog.php?article=179</a> - this one actually works
sinrizeMay 17, 2009
PHP Colour Changing Background Algorithm - <a class="user" href="http://www.icurtain.co.uk/blog.php?article=179">http://www.icurtain.co.uk/blog.php?article=179</a> 1. public function getAverageColour(){ 2. $colours = array('red'=>0,'green'=>0,'blue'=>0); 3. $count = 0; 4. $rgbstring = null; 5. 6. for ($i=0;$iimage);$i++){ 7. for ($j=0;$jimage);$j++){ 8. $rgb = imagecolorat($this->image, $i, $j); 9. $ici = imagecolorsforindex($this->image, $rgb); 10. //print_r($ici); 11. foreach($colours as $key => $value){ 12. $colours[$key] = $colours[$key] + $ici[$key]; 13. } 14. $count++; 15. } 16. } 17. foreach($colours as $key => $value){ 18. $colours[$key] = dechex(intval($colours[$key] / $count)); 19. $rgbstring = $rgbstring . sprintf("%02s", $colours[$key]); 20. } 21. return $rgbstring; 22. }