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 839261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:26:20+00:00 2026-05-15T05:26:20+00:00

I don’t suppose anyone knows of a function (PHP, preferably) that can take a

  • 0

I don’t suppose anyone knows of a function (PHP, preferably) that can take a hex color code and give an approximate color name for that hex value. I don’t need a solution with 100s of colors. Even if it just amounted to the colors white, black, red, green blue, brown orange and yellow, I’d be pretty well in shape.

If you don’t know of an existing resource, does anyone know of a good way to approach this problem?

Thanks in advance for the help.

  • 1 1 Answer
  • 3 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-05-15T05:26:20+00:00Added an answer on May 15, 2026 at 5:26 am

    You have a list of colors here in wikipedia.

    A naive implementation would just calculate the distance between the color whose name you want to retrieve and every one of those and output the one that’s closer.

    If you see the colors as vectors in R3, you could calculate the distance as L1 norm:

    function distancel1(array $color1, array $color2) {
        return abs($color1[0] - $color2[0]) + 
            abs($color1[1] - $color2[1]) +
            abs($color1[2] - $color2[2]);
    }
    

    or the L2 norm:

    function distancel2(array $color1, array $color2) {
        return sqrt(pow($color1[0] - $color2[0], 2) +
            pow($color1[1] - $color2[1], 2) +
            pow($color1[2] - $color2[2], 2));
    }
    

    For converting the hexadecimal notation, see here.

    Example script:

    <?php
    
    $colors = array(
        "black"     => array(0, 0, 0),
        "green"     => array(0, 128, 0),
        "silver"    => array(192, 192, 192),
        "lime"      => array(0, 255, 0),
        "gray"      => array(128, 0, 128),
        "olive"     => array(128, 128, 0),
        "white"     => array(255, 255, 255),
        "yellow"    => array(255, 255, 0),
        "maroon"    => array(128, 0, 0),
        "navy"      => array(0, 0, 128),
        "red"       => array(255, 0, 0),
        "blue"      => array(0, 0, 255),
        "purple"    => array(128, 0, 128),
        "teal"      => array(0, 128, 128),
        "fuchsia"   => array(255, 0, 255),
        "aqua"      => array(0, 255, 255),
    );
    
    $value = "#819001";
    
    function html2rgb($color)
    {
        if ($color[0] == '#')
            $color = substr($color, 1);
    
        if (strlen($color) == 6)
            list($r, $g, $b) = array($color[0].$color[1],
                                     $color[2].$color[3],
                                     $color[4].$color[5]);
        elseif (strlen($color) == 3)
            list($r, $g, $b) = array($color[0].$color[0],
                $color[1].$color[1], $color[2].$color[2]);
        else
            return false;
    
        $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
    
        return array($r, $g, $b);
    }
    
    function distancel2(array $color1, array $color2) {
        return sqrt(pow($color1[0] - $color2[0], 2) +
            pow($color1[1] - $color2[1], 2) +
            pow($color1[2] - $color2[2], 2));
    }
    
    $distances = array();
    $val = html2rgb($value);
    foreach ($colors as $name => $c) {
        $distances[$name] = distancel2($c, $val);
    }
    
    $mincolor = "";
    $minval = pow(2, 30); /*big value*/
    foreach ($distances as $k => $v) {
        if ($v < $minval) {
            $minval = $v;
            $mincolor = $k;
        }
    }
    
    echo "Closest color: $mincolor\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't think that I'm mad, I understand how php works! That being said. I
Don't know if anyone can help me with this or if it's even possible.
Don't be scared of the extensive code. The problem is general. I just provided
I don't know why, but this code worked for me a month ago... maybe
Don't be frightened, its a very basic code. Just wanted to check with you
Don't they both have to convert to machine code at some point to execute
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't worry, I'm not going to ask that question, yet again... I am wanting
DON'T ASK WHY but... I have a regex that needs to be case insensitive

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.