Detection of similarity between colors with rgba or Hex value using Python

Detection of similarity between colors with rgba or Hex value using Python

4 min read

How about finding a similarity between two different colors having hex value or rgba value using Python programming language.

Isn’t it cool to find out which color is most similar to color shade in any given list. For example, you have some rgba value of color and one list full list of different rgba value and you want to find out which color is most similar to some rgba value of color.

So let’s start our journey to solve this problem.

First Phase:

Parse the hexadecimal value – extract the value of red, green, blue from the hexadecimal value.

 # Function to Parse Hexadecimal Value
def parse_hex_color(string):
    if string.startswith("#"):
        string = string[1:]

    r = int(string[0:2], 16) # red color value
    g = int(string[2:4], 16) # green color value
    b = int(string[4:6], 16) # blue color value
    return r, g, b, 255

Function for finding similarity between two color

Now, our program need arises to create an function that can find color similarity between two color. So question arises how does this function works.

Euclidian Distance

So This function calculate the distance between two different color value.

# Calculate distance btw two color
def color_similarity(base_col_val,oth_col_val):
    return math.sqrt(sum((base_col_val[i]-oth_col_val[i])**2 for i in range(3)))

Let assume some color value for example:

base_color = "#ff0000"
rand_color = "#546324"

So let calculate the similarity between these two color.

import math

# Function to Parse Hexadecimal Value
def parse_hex_color(string):
    if string.startswith("#"):
        string = string[1:]

    r = int(string[0:2], 16) # red color value
    g = int(string[2:4], 16) # green color value
    b = int(string[4:6], 16) # blue color value
    return r, g, b, 255

# Calculate distance btw two color
def color_similarity(base_col_val,oth_col_val):
    return math.sqrt(sum((base_col_val[i]-oth_col_val[i])**2 for i in range(3)))

base_color = "#ff0000"
rand_color = "#546324"

print("Color Distance between two-color ", color_similarity(parse_hex_color(base_color),parse_hex_color(rand_color)))

Output:

Color Distance between two-color 200.84322243

Note: Less distance value show more similarity between the color’s.

Flow Chart for the similarity between two color

Other examples:

Programmer can use this method to find the most similar color inside the given color list.

Find Out the most similar color inside the given color list

Let assume some random color_list for different colors.

color_list = ["#467829", "#4587ab", "#1628cd", "#278bad"]

To make this possible, the programmer has to add one more section to the code for finding out the least distance value.

# Code for finding out minimum distance
for x in color_list:
    if color_similarity(base_red,x)>r:
        r,rs=color_similarity(base_red,x),x

Let put up the final code.

import math

# Function to Parse Hexadecimal Value
def parse_hex_color(string):
    if string.startswith("#"):
        string = string[1:]

    r = int(string[0:2], 16) # red color value
    g = int(string[2:4], 16) # green color value
    b = int(string[4:6], 16) # blue color value
    return r, g, b, 255

# Calculate distance btw two color
def color_similarity(base_col_val,oth_col_val):
    return math.sqrt(sum((base_col_val[i]-oth_col_val[i])**2 for i in range(3)))

base_color = "#ff0000"
color_list = ["#467829", "#4587ab", "#1628cd", "#278bad"]

# Max intial value
r = 1000

# Code for finding out minimum distance
for x in color_list:
    if color_similarity(parse_hex_color(base_color),parse_hex_color(x))<r:
        r,rs=color_similarity(parse_hex_color(base_color),parse_hex_color(x)),x

print("Most similar color inside the list is  ", rs)

Output:

Most similar color inside the list is #467829

Flow Chart for Most similar color inside the list using python

There are other methods also but I have used the simplest method for finding out the similarity between two-color.

Finally our journey comes to end.

Tags:
Choose your Reaction!
Leave a Comment