ColorPalette / src /color_utils.py
HardikUppal's picture
Save current state after cleanup
a952689
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
def rgb_to_lab(rgb):
"""
Convert an RGB color to LAB color.
:param rgb: A tuple (R, G, B) with values ranging from 0 to 255.
:return: A LabColor object.
"""
srgb_color = sRGBColor(rgb[0], rgb[1], rgb[2], is_upscaled=True)
lab_color = convert_color(srgb_color, LabColor)
return lab_color
# Helper function to convert RGB to hex
def rgb_to_hex(rgb_color):
r, g, b = rgb_color
return "#%02X%02X%02X" % (int(r), int(g), int(b))
# Function to convert hex color to BGR
def hex_to_bgr(hex_color):
hex_value = hex_color.lstrip("#")
r, g, b = [int(hex_value[i:i + 2], 16) for i in (0, 2, 4)]
return [b, g, r]
def hex_to_rgb(hex_color):
hex_value = hex_color.lstrip("#")
r, g, b = [int(hex_value[i:i + 2], 16) for i in (0, 2, 4)]
return [r, g, b]
def calculate_color_distance_lab(color1, color2):
"""
Calculate the distance between two colors in LAB space using the CIE2000 formula.
:param color1: A tuple (R, G, B) with values ranging from 0 to 255.
:param color2: A tuple (R, G, B) with values ranging from 0 to 255.
:return: The distance between the two colors.
"""
lab1 = rgb_to_lab(color1)
lab2 = rgb_to_lab(color2)
distance = delta_e_cie2000(lab1, lab2)
return distance
# # Example usage
# color1 = (255, 215, 0) # Golden Blonde RGB
# color2 = (255, 248, 220) # Platinum Blonde RGB
# distance = calculate_color_distance_lab(color1, color2)
# print(f"Color distance in LAB space: {distance}")