chevron-left chevron-right

Piotr Nalepa Blog webmasterski

How to find a similar colour in your colour guidelines with JavaScript?

This version
Published
Author
Category
JavaScript
Feedback
Write to me · Post on X

This post is 6 years old. Browsers and tools may have changed since then, so check the code against current documentation before you use it.

In my work I often find myself in situation where designers are using colors that are not defined in the style guidelines, but are somewhat similar. This is a difficult situation for every frontend developer that would like to use only colors predefined in guidelines for consistency.

So I decided to build a library that will help me in such cases. First, I'll try to explain some terms. Keep in minds I'm not an expert in the field of color theory, but I will do my best to explain it clearly and without any mistakes.

Section 1Euclidean distance

In general, the euclidean distance is a straight-line between 2 points in Euclidean space. The space can be multidimensional and that's the cause it's not so easy to check color similarity. Illustration for n=3, repeated application of the Pythagorean theorem yields the formula Making Euclidean distance calculations eases identification of similar colors.

Section 2HEX color vs LAB color

You might be wondering what is LAB color, but first let's explain what is HEX color actually.

RGB color spectrum

HEX color is a representation of RGB colour space, where colors are defined by a hex triplet is a six-digit, three-byte hexadecimal number, like #bada55. It's a digital color space describing colors visible to machines.

CIELab color spectrum

Lab colour describes all the colors visible to the human eye and was created to serve as a device-independent model to be used as a reference. This approach is different from the one represented by RGB color space.

Section 3The library usage

As I mentioned at the beginning of this blog post, I've created a small library that helps to find the most similar color of all predefined colors.

First of all, go ahead and install the library in your project with the following command:

Example 1
npm install @sunpietro/color-finder

Then in your project import the library:

Example 2
import ColorFinder from '@sunpietro/color-finder';

And now you can start using it. Let's in the following example how it's used:

Example 3JavaScript
import ColorFinder from '@sunpietro/color-finder';

const colors = [
  '#b3a212',
  '#bb3300',
  '#09a1b3',
  '#bada55',
  '#ff34aa',
  '#123456'
];

const finder = ColorFinder(colors);

console.log(finder.findClosestColor('#bab330'));

/** Console Output
 * 
 * #b3a212
 */

There are 2 more methods available and you can read about them in projects docs.

For the browser, you can download the library file and start using it as a regular script. The library will be available in the browser global namespace: window.ColorFinder. The rest is like in the example above.

Section 4Summary

I hope that you find this tool useful. Personally, it help me quite a lot when working with designs using incorrect colors, other than specified in styling guidelines.

If you want to start using it without building your own solutions, then feel free to check the demo page.

About the author

Staff Software Engineer

I've been writing this blog since 2009 and building for the web professionally for 14 years. Today I lead the frontend of the Cosmos platform at Bishop Fox, where I've been since day one of the project. Before that I worked at OLX, KUDO, Auto1 and eZ Systems. Here I write about what I've learned in practice: JavaScript, TypeScript, React, CSS and frontend architecture. I've also spoken at conferences across Poland and Europe. Got a question or an idea for a post? Let me know!