import numpy as np
from colormath.color_objects import LabColor, sRGBColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
np.set_printoptions(np.inf)
class Color(object):
def __init__(self, color_file = 'color_list.txt'):
self.color_name_en = []
self.color_name_zh = []
self.color_HEX = []
self.color_RGB = []
self.color_Lab = []
self.readColorFile(color_file)
def readColorFile(self, color_file):
with open(color_file, 'r', encoding='utf-8') as f:
colors = f.readlines()
for color in colors:
lc = color.split('\t')
self.color_name_en.append(lc[0].strip())
self.color_name_zh.append(lc[1].strip())
self.color_HEX.append(lc[2].strip())
rgb = lc[3].strip().split(',')
rgb = [int(v) for v in rgb]
self.color_RGB.append(rgb)
self.rgbToLab()
def rgbToLab(self):
for rgb in self.color_RGB:
srgb = sRGBColor(*rgb)
lab = convert_color(srgb, LabColor)
self.color_Lab.append(lab)
def getMinCIE2000Distance(self, rgb):
srgb = sRGBColor(*rgb)
tlab = convert_color(srgb, LabColor)
min_index = -1
min_delta_e = np.inf
for idx, lab in enumerate(self.color_Lab):
delta_e = delta_e_cie2000(tlab, lab)
if delta_e < min_delta_e:
min_delta_e = delta_e
min_index = idx
return min_delta_e, min_index
def calculateRGBDistance(self, rgb=(0,0,0)):
array_rgb = np.array(self.color_RGB)
print(array_rgb)
print(array_rgb.shape)
temp1_rgb = (rgb - array_rgb) / 255
temp2_rgb = temp1_rgb[:,0]*temp1_rgb[:,0] + temp1_rgb[:,1]*temp1_rgb[:,1] + temp1_rgb[:,2]*temp1_rgb[:,2]
def __getitem__(self, index):
info = f"英文代码 : {self.color_name_en[index]}\n"
info += f"形象颜色 : {self.color_name_zh[index]}\n"
info += f"HEX格式 : {self.color_HEX[index]}\n"
info += f"RGB格式 : {self.color_RGB[index]}\n"
info += f"Lab格式 : {self.color_Lab[index]}\n\n"
return info
def __len__(self):
assert len(self.color_name_en) == len(self.color_name_zh)
assert len(self.color_name_en) == len(self.color_HEX)
assert len(self.color_name_en) == len(self.color_RGB)
return len(self.color_name_en)
if __name__ == '__main__':
c = Color()
rgb = (123, 145, 111)
delta_e, index = c.getMinCIE2000Distance(rgb)
print(delta_e, index)
print(c[index])
CIELAB色差计算_lanmengyiyu的博客-CSDN博客_lab色差
【图像处理】颜色距离_Denny#的博客-CSDN博客_颜色距离公式
色差计算(颜色之间的相似度计算)_程序猿也可以很哲学的博客-CSDN博客_色差计算