From Hex code to Basic Color Name - PHP

From Hex code to Basic Color Name - Find The Nearest Color PHP

For an ecommerce store, I need a tool to convert a hex code from product image to a pre-defined basic color in my store. I look for the solution of this task and I found the basic alogrithm to handle this:


1/ Convert the HEX Code to RGB Value

2/ Calculate the distance between current color with other colors by using this formula:

Color 1 RGB Value: Red1, Green1, Blue 1 Color 2 RGB Value: Red2, Green2, Blue 2

=> Distance = sqrt((Red2-Red1)^2+(Green2-Green1)^2+(Blue2-Blue1)^2)

3/ Get the color with the smallest distance

Sourcecode and Example

Happy Coding!

Written on September 30, 2015