GD dominant colo(u)r function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

GD dominant colo(u)r function

Post by mikeeeeeeey »

Hi guys,

Anyone have any idea how to start a function which selects the dominant colour in a 50x50 square? I can store all RGB values into an array using a foreach() and this...

Code: Select all

$rgb = imagecolorat($img, $xpos, $ypos;
		$r   = ($rgb >> 16) & 0xFF;
		$g   = ($rgb >>  & 0xFF;
		$b   = $rgb & 0xFF;
however I'm not sure how to put this together :cry:

Any help appreciated, thanks in advance.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

so by dominant colour do you want the mean or mode?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Generally speaking, you would convert the color data to a histgram format. The largest stack is the dominant color. :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

you could resample the image to 1x1 and test the resulting pixel color for the "mean" or average colour.

The mode would be trickier, but doable. I'd start by converting to an indexed format, thereby letting the image algorithm do the heavy lifting for you. Then you can simply tabulate the number of pixels that map to each colour and sort to find the most common.
Post Reply