Introducing ColorCompass: Find the Perfect Name for Your RGB or Hexadecimal Color

Hello, Dev.to community! Today, I’d like to introduce a solution born out of a specific need I faced during another project: ColorCompass. This Python library is crafted to help you determine the closest named color to a given RGB value, using an efficient Euclidean distance calculation across a broad array of predefined colors.

A little backstory

The roots of ColorCompass trace back to a challenge I encountered while working on another project called DWD (Dream Wall Decor) a project leveraging generative art AI and color analysis from images.

The hiccup arose when I aimed to construct a prompt for the generative art AI using RGB or hexadecimal colors. Oddly enough, none of the generative art AIs I encountered accepted RGB or hex values. They solely operated with named colors for generating images with specific hues.

In my quest for a solution, I experimented with libraries like webcolors. However, they fell short. For instance, if you query the color name for rgb(255,0,0) (a pure red), you get “red”. But, intriguingly, querying for rgb(254,0,0) (which appears virtually identical to the human eye) results in an error. This inconsistency was prevalent across multiple libraries.

This is where ColorCompass shines—it always returns a color name. This is due to its approach of gauging the proximity of the provided code to the colors in its database, ensuring there’s always a named color output.

How Does It Work?

The Euclidean Distance Calculation

The crux of ColorCompass lies in utilizing the Euclidean Distance formula to find the matching color name for a given RGB input. Given an RGB value, the Euclidean Distance between two points (color values, in our context) in a three-dimensional space (R, G, B) is calculated as:

Distance = √(R_2 – R_1)^2 + (G_2 – G_1)^2 + (B_2 – B_1)^2

Here,

  • (R_1, G_1, B_1) and (R_2, G_2, B_2) are the RGB values of the two colors being compared.
  • The formula basically measures the straight-line distance between two points in a 3D space (your two colors, in the RGB color space).

More info about the eucledian distance here.

The Algorithmic Approach

  1. Input Color Value: A user inputs an RGB color value that they’d like to map to a named color.

  2. Distance Calculation: For the input color, ColorCompass calculates the Euclidean Distance between the input RGB value and all stored RGB values in the library’s color database, effectively identifying which stored color is (has the minimal Euclidean Distance) to the provided input.

  3. Return Color: The algorithm identifies the color name associated with the RGB value in the database that has the smallest Euclidean Distance to the input color. This color name is returned to the user as the closest match.

Give It a Go!

To get started, install the library:

pip install colorcompass

Enter fullscreen mode Exit fullscreen mode

Then, use the get_color_name function to fetch the name of your color:

from colorcompass import get_color_name

# Define your target color as an RGB list target_color_rgb = [152, 251, 152]

# Or you can define your target color as an hexadecimal string target_color_hexadecimal = "#98fb98"

# Use the function to find the closest color name color_name_rgb = get_color_name(target_color_rgb)
color_name_hexadecimal = get_color_name(target_color_hexadecimal)

# Print the discovered color name, it should be 'mint green' for this case print(color_name_rgb)
print(color_name_hexadecimal)

Enter fullscreen mode Exit fullscreen mode

Acknowledgements

A crucial part of ColorCompass’s inception and functionality lies in the extensive list of color names and their respective codes. While these were modified to fit the library’s specifications, a nod to xkcd by Randall Munroe is essential. Their expansive collection made this project achievable.

In Closing

ColorCompass is a boon for designers and developers seeking to enrich their work with descriptive color names, especially when dealing with the quirks of generative art AIs. I hope it proves as invaluable to you as it has for me!

For questions, feedback, or contributions, please visit the ColorCompass GitHub Repository.

原文链接:Introducing ColorCompass: Find the Perfect Name for Your RGB or Hexadecimal Color

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容