How to retrieve the human name of a color by its hex code in PHP

How to retrieve the human name of a color by its hex code in PHP

Among all the weird desires of your clients, one of the most weird of them surely are the ones of designers. Lately i needed to fulfill a requirement that sound initially weird, detecting the name of the color by its hex code in PHP. Initially and after some research i found out how difficult this task was. I found a couple of implementations in JavaScript that had a lot of potential, however they weren't available in the PHP language, so basically i need to port some of those scripts in PHP, after a while i ended up creating the name-that-color library in PHP. The PHP version of name-that-color is a non-official PHP port of the NTC JS library to find out the name of the closest matching (or exact) color.

1. Install - include name-that-color

The preferred way to use this library in PHP is via Composer with the following command:

composer require ourcodeworld/name-that-color

After the installation you will be able to use the color interpreter class of the library. Alternatively, if you are not using composer, you can still use the wrapper. Just download the ColorInterpreter.php class from the repository and then use require_once to import it in your code:

<?php

require_once("ColorInterpreter.php");

$instance = new ColorInterpreter();

// Rest of the code ...
// $instance->name("#FFF");

With the ColorInterpreter class you will be able to guess the closest color according to its given code and the register in the class. It's worth to say that the art of guessing a color by its hexadecimal code its pretty tricky, as well of the appreciation of naming a color could vary as well. However, the original JS script offers a wide range of colors that may match your hex code, if it isn't registered in the class, it will return the closest color.

For more information about this library, please visit the official repository at Github here.

2. How to use

Initially, you only will need to cast the color interpreter class as its initialization takes a little execution time because it will create a quite huge color palette that it will use to compare your hex code with the available data. From the instance, you will be able to use 3 methods. Of our interest is only the name method useful:

<?php

// If you are using composer, include the class
use ourcodeworld\NameThatColor\ColorInterpreter;

$instance = new ColorInterpreter();

$result = $instance->name("#008559");

// 1. Print the human name e.g "Deep Sea"
echo $result["name"] . "\n";

// 2. Print the hex code of the closest color with a name e.g "#01826B"
echo $result["hex"] . "\n";

// 3. Print wheter the given hex code is exact as a color with a name
//    or if it has been derived
if($result["exact"]){
    echo "The given hex code is exact as the name";
}else{
    echo "A similar color with a name has been picked";
}

This method expects as first argument a string with a hexadecimal color code of 6 or 3 digits e.g #ffffff or #fff and returns an array with the data that you need to know according to the color guess process. The array contains 3 keys:

  • hex: the hex color of the closest color in the class.
  • name: the human name given to the color.
  • exact: boolean that determines wheter the color code is exact as the name or not.

The property that you are looking for is the name key that returns an human name for the hex color code, in this case is the Deep Sea color, but it works for almost every hex color that you provide, in case that it doesn't work with your color, you will receive an array with the same 3 items, however with the following values:

  • hex: #000000.
  • name: Invalid Color: $yourHexCodeString.
  • exact: false

This library is totally compatible with PHP 5.3, 5.6 and 7.

Happy coding !

This could interest you
Become a more social person