Hi there,
I am trying to get the main colors used in an image, say the 3 most used #hex ranges.
Any tips, suggestions?
| SitePoint Sponsor |




Hi there,
I am trying to get the main colors used in an image, say the 3 most used #hex ranges.
Any tips, suggestions?




ok, i am able to get the 2 most used colors in an image.
Where do i find a global hex color ranges?
Blueish, Redish, Greenish, Yellowish, Blackish, Pinkish Purpleish
found: http://www.december.com/html/spec/color16.html

If you have imagemagick you can use:
There is some discussion on a thread here about it: http://www.imagemagick.org/discourse...hp?f=1&t=12818PHP Code:<?php
exec("convert input.jpg -colors 256 -depth 8 -format \"%c\" histogram:info:", $a);
for ( $i=0; $i <= 9; $i++){
$find = preg_match("/#(.*)r/", $a[$i], $matches);
$colour = "#".trim($matches['1']);
$name = trim($matches['1']).".jpg";
exec("convert -size 100x20 xc:$colour $name");
echo '<br />' . $matches['1'];
echo "<img src=\"$name\"><br>";
}
?>




tx, rubble
but i am not using imagemagick.
Right now, i am able to get the 2 most used colors in hex or dec. Now i need a "table" to compare the value too and return one of the predefined colours. Assuming you can define 6 color ranges


I use Adobe Kuler. it will pick out colours from an image according to a theme (dark, light, bright, etc) and then allow you to make minor adjustments afterwards to get the type of swatches you want.

I would think you could do it with an array but only if your exact colour is in the array unless you could find a way to select the nearest number.
Something like this:
PHP Code:$colour_found = "FF0000";
$colours = array( "FF0000" => "red", "00FF00" => "green", "0000FF" => "blue");
echo $colours[$colour_found];




Well, how would you be able to tell the difference?
First I'd split the hex into 3 parts (R, G, B), and convert each part into decimal.
Then, get the largest number of the three. If they're all equal, measure the number and judge whether its white, gray or black. Otherwise, find out the percentages of the biggest number that the smaller numbers are and make a judgement of color on that.
Then, add all three numbers up. It'll span from 0 to 765. Use that number to get an idea of how light/dark it is.
If you think about it, and plan it out properly, I know you can do this on your own.
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
Bookmarks