SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to get the main color from an image??

    Hi there,

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

  2. #2
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,443
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    If you have imagemagick you can use:
    PHP 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>";
    }
    ?>
    There is some discussion on a thread here about it: http://www.imagemagick.org/discourse...hp?f=1&t=12818

  4. #4
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    SitePoint Evangelist WebMachine's Avatar
    Join Date
    Jun 2007
    Location
    Ontario, Canada
    Posts
    430
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,443
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    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]; 

  7. #7
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rubble View Post
    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 almost i mean red with a range so FD000, FF5151 etc are also read as RED.

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •