Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8274765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:51:05+00:00 2026-06-08T07:51:05+00:00

I’m building a tool where i pick the dominant colors from an image and

  • 0

I’m building a tool where i pick the dominant colors from an image and i have done that bit pretty well, the problem i’m facing now is how to combine the returned colors into a broad palette range like the X11 color range http://en.wikipedia.org/wiki/Web_colors#X11_color_names

So for example if i got : Color RGB:rgb(102,102,153), i would want to chalk it up to the purple colors and rgb(51,102,204) to blue and so on and so forth. Now i really can’t figure out how to do this. Is there a library or something i can use or how should i code it? I’m using imagemagick and php btw.

Is it possible to generate an array containing the range of rgb for each base colors and then see if my new color lies in it?

Thanks in advance guys!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:51:07+00:00Added an answer on June 8, 2026 at 7:51 am

    OK, forget the previous code, here is a much more reliable version I put together from several sources which also uses the more common 0-360 for hue.

    I also added a test little routine that picks random colours then ‘tells’ you what they are!

    Seems more reliable, some fiddling might be needed around the cut-off values (especially orange/grey etc). I used this online colour picker for testing.

    <?php
    
    function RGBtoHSL($r,$g,$b) {  
    // based on code and formulas from:
    // http://www.had2know.com/technology/hsl-rgb-color-converter.html
    // http://colorgrader.net/index.php/dictionary-a-tutorials/color-theory/93-math-behind-colorspace-conversions-rgb-hsl.html
    // http://proto.layer51.com/d.aspx?f=1135
    
    
    $max = max($r, $g, $b);
    $min = min($r, $g, $b);
    $d = ($max - $min) / 255;
    $lum = round((($max+$min)/2)/255, 2);
    
    $sat = 0;
    if ($lum > 0) $sat = round($d/(1 - (2*$lum-1)), 2);
    
    $hue = 0;  
    if(($r==$g) && ($g==$b))  $hue = 0;  
    else if($r>=$g && $g>=$b) $hue = 60*($g-$b)/($r-$b);  
    else if($g>=$r && $r>=$b) $hue = 60  + 60*($g-$r)/($g-$b);  
    else if($g>=$b && $b>=$r) $hue = 120 + 60*($b-$r)/($g-$r);  
    else if($b>=$g && $g>=$r) $hue = 180 + 60*($b-$g)/($b-$r);  
    else if($b>=$r && $r>=$g) $hue = 240 + 60*($r-$g)/($b-$g);  
    else if($r>=$b && $b>=$g) $hue = 300 + 60*($r-$b)/($r-$g);  
    else $hue = 0;  
    $hue = round($hue, 0);  
    $hsl = array();
    $hsl['h'] = $hue;
    $hsl['s'] = $sat;
    $hsl['l'] = $lum;
    return $hsl;
    }  
    
    
    
    
    // example: pick 42 random colours then identify them
    echo "<div style='float: left; width: 1000px;'>";
    
    srand;
    for ($f=0; $f < 42; $f++) {
    
    $red = rand(0, 255); 
    $green = rand(0, 255); 
    $blue = rand(0, 255); 
    
    $hsl = RGBtoHSL($red, $green, $blue);
    
    $hue = $hsl['h'];
    $sat = $hsl['s'];
    $lum = $hsl['l'];
    
    $colour = "Red";            // default to red
    
    if ($hue >= 11 && $hue <= 45) {
    $colour = "Orange";
    if ($lum < 0.51) $colour = "Brown";
    }
    if ($hue >= 46 && $hue <= 62) $colour = "Yellow";
    if ($hue >= 63 && $hue <= 160) $colour = "Green";
    if ($hue >= 161 && $hue <= 262) $colour = "Blue";
    if ($hue >= 263 && $hue <= 292) $colour = "Purple";
    if ($hue >= 293 && $hue <= 349) $colour = "Pink";
    if ($sat < 0.07) $colour = "Grey";  // do grey before black/white
    if ($lum < 0.10) $colour = "Black";
    if ($lum > 0.97) $colour = "White";
    
    echo "<div style='float: left; width: 150px; border: 1px solid #000000; margin: 5px;'>";      
    echo "<div style='float: left; width: 20px; height: 120px; background-color: rgb($red, $green, $blue); margin-right: 10px;'></div>";
    echo "<p style='width: 33%; float: left;'>R=$red<br/>G=$green<br/>B=$blue</p>";
    echo "<p style='width: 33%; float: right;'>H=$hue<br/>S=$sat<br/>L=$lum</p>";
    echo "<p><b>$colour</b></p>";
    echo "</div>";
    }
    
    echo "</div>";
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.