def test_cie1994_domain_error(self): # These values are from ticket 98 in regards to a CIE1995 # domain error exception being raised. c1 = LabColor(lab_l=50, lab_a=0, lab_b=0) c2 = LabColor(lab_l=50, lab_a=-1, lab_b=2) try: delta_e_cie1994(c1, c2) except ValueError: self.fail("DeltaE CIE1994 domain error.")
def test_cie1994_negative_square_root(self): """ Tests against a case where a negative square root in the delta_H calculation could happen. """ standard = LabColor(lab_l=0.9, lab_a=1, lab_b=1) sample = LabColor(lab_l=0.7, lab_a=0, lab_b=0) delta_e_cie1994(standard, sample)
def mapColor(colorMap, color): """ 获得颜色映射 Args: colorMap 颜色映射表 color 颜色 Return: dstColor 匹配到的颜色值 """ # 计算最近颜色值 colors = colorMap.keys() dstColor = None minDist = float("Inf") b, g, r = color labColor = convert_color(sRGBColor(r, g, b, True), LabColor) for key in colors: colorList = colorMap[key] for hexColor in colorList: r, g, b = hex2Rgb(hexColor) rgb = sRGBColor(r, g, b, True) lab = convert_color(rgb, LabColor) dist = delta_e_cie1994(lab, labColor) if dstColor is None or dist < minDist: minDist = dist colorName = key dstColor = hexColor return dstColor, colorName
def delta_e(self, other_color, mode='cie2000', *args, **kwargs): """ Compares this color to another via Delta E. Valid modes: cie2000 cie1976 """ if not isinstance(other_color, ColorBase): raise InvalidArgument('delta_e_cie2000', 'other_color', other_color) # Convert the colors to Lab if they are not already. lab1 = self.convert_to('lab', *args, **kwargs) lab2 = other_color.convert_to('lab', *args, **kwargs) mode = mode.lower() if mode == 'cie2000': return delta_e_cie2000(lab1, lab2) elif mode == 'cie1994': return delta_e_cie1994(lab1, lab2, **kwargs) elif mode == 'cie1976': return delta_e_cie1976(lab1, lab2) elif mode == 'cmc': return delta_e_cmc(lab1, lab2, **kwargs) else: raise InvalidDeltaEMode(mode)
def test_cie1994_accuracy_graphic_arts(self): result = delta_e_cie1994(self.color1, self.color2) expected = 1.249 self.assertAlmostEqual( result, expected, 3, "DeltaE CIE1994 (graphic arts) formula error. Got %.3f, expected %.3f (diff: %.3f)." % (result, expected, result - expected))
def test_cie94(): for p in numpy.eye(3): for q in numpy.eye(3): cp = convert_color(sRGBColor(*p), LabColor) cq = convert_color(sRGBColor(*q), LabColor) assert abs( delta_e_cie1994(cp, cq) - cie94(rgb2lab(p), rgb2lab(q))) < 1e-2
def color_distance(rgb_a: sRGBColor, rgb_b: sRGBColor, dist_type: str = 'sRGB'): # https://en.wikipedia.org/wiki/Color_difference func_switcher = { 'CIE76': delta_e_cie1976, 'CIE94': lambda color1, color2: delta_e_cie1994( color1, color2, K_L=2, K_C=1, K_H=1, K_1=0.048, K_2=0.014) } if dist_type in func_switcher.keys(): # https://books.google.com/books?id=OxlBqY67rl0C&pg=PA31&vq=1.42&dq=jnd+gaurav+sharma#v=onepage&q=1.42&f=false # https://en.wikipedia.org/wiki/CIELAB_color_space # https://en.wikipedia.org/wiki/Just-noticeable_difference # JND = 2.3 if CIE76 lab_a = convert_color(rgb_a, LabColor) lab_b = convert_color(rgb_b, LabColor) delta_e_func = func_switcher.get(dist_type) dist = delta_e_func(lab_a, lab_b) return dist # Euclidean with sRGB as default # https://www.compuphase.com/cmetric.htm avg_r = (rgb_a.rgb_r + rgb_b.rgb_r) * 255 / 2 w_r = 2 + avg_r / 256 delta_r = (rgb_a.rgb_r - rgb_b.rgb_r) * 255 w_g = 4 delta_g = (rgb_a.rgb_g - rgb_b.rgb_g) * 255 w_b = 2 + (255 - avg_r) / 256 delta_b = (rgb_a.rgb_b - rgb_b.rgb_b) * 255 dist = math.sqrt(w_r * pow(delta_r, 2) + w_g * pow(delta_g, 2) + w_b * pow(delta_b, 2)) return dist
def delta_e(self, other_color, mode='cie2000', *args, **kwargs): """ Compares this color to another via Delta E. Valid modes: cie2000 cie1976 """ if not isinstance(other_color, ColorBase): raise InvalidArgument('delta_e_cie2000', 'other_color', other_color) # Convert the colors to Lab if they are not already. lab1 = self.convert_to('lab', *args, **kwargs) lab2 = other_color.convert_to('lab', *args, **kwargs) mode = mode.lower() if mode == 'cie2000': return color_diff.delta_e_cie2000(lab1, lab2) elif mode == 'cie1994': return color_diff.delta_e_cie1994(lab1, lab2, **kwargs) elif mode == 'cie1976': return color_diff.delta_e_cie1976(lab1, lab2) elif mode == 'cmc': return color_diff.delta_e_cmc(lab1, lab2, **kwargs) else: raise InvalidDeltaEMode(mode)
def test_cie1994_accuracy_textiles(self): result = delta_e_cie1994( self.color1, self.color2, K_1=0.048, K_2=0.014, K_L=2) expected = 1.204 self.assertAlmostEqual(result, expected, 3, "DeltaE CIE1994 (textiles) formula error. Got %.3f, expected %.3f (diff: %.3f)." % ( result, expected, result - expected))
def test_cie1994_accuracy_textiles(self): result = delta_e_cie1994(self.color1, self.color2, K_1=0.048, K_2=0.014, K_L=2) expected = 1.204 self.assertAlmostEqual( result, expected, 3, "DeltaE CIE1994 (textiles) formula error. Got %.3f, expected %.3f (diff: %.3f)." % (result, expected, result - expected))
def DeltaCalculator(CIE, LabCH, delta): lab_reference = LabColor(lab_l=CIE[0], lab_a=CIE[1], lab_b=CIE[2]) lab = LabColor(lab_l=LabCH[0], lab_a=LabCH[1], lab_b=LabCH[2]) if delta == 'delta_e_cie1976': return delta_e_cie1976(lab, lab_reference) elif delta == 'delta_e_cie1994': return delta_e_cie1994(lab, lab_reference) elif delta == 'delta_e_cie2000': return delta_e_cie2000(lab, lab_reference) else: return delta_e_cmc(lab, lab_reference)
def distance(spectrum_pnt, pnt, mode): color1 = LabColor(lab_l=spectrum_pnt[0], lab_a=spectrum_pnt[1], lab_b=spectrum_pnt[2]) color2 = LabColor(lab_l=pnt[0], lab_a=pnt[1], lab_b=pnt[2]) if mode == 'cie1976': return color_diff.delta_e_cie1976(color1, color2) elif mode == 'cie1994': return color_diff.delta_e_cie1994(color1, color2, K_L=1, K_C=1, K_H=1, K_1=0.045, K_2=0.015) elif mode == 'cie2000': return color_diff.delta_e_cie2000(color1, color2, Kl=1, Kc=1, Kh=1) elif mode == 'cmc': return color_diff.delta_e_cmc(color1, color2, pl=2, pc=1) else: return np.sqrt((spectrum_pnt[0] - pnt[0])**2 + (spectrum_pnt[1] - pnt[1])**2 + (spectrum_pnt[2] - pnt[2])**2)
#im2px[x,y] = (r,g,b) im2.save("/tmp/tmp.png") else: m = re.match(r'(..)(..)(..)', sys.argv[1]) assert m (r, g, b) = map(lambda x: int(x, 16), m.group(1, 2, 3)) c0str = '%02X%02X%02X to ' % (r, g, b) c0 = rgb2lab(r, g, b) fp = open('/tmp/tmp.html', 'w') fp.write('<html>\n') fp.write('<table>\n') fp.write( '<tr><th>input</th><th>palette</th><th>cie1976</th><th>cie1994</th><th>cie2000</th><th>cmc</th></tr>\n' ) for (idx, c1) in enumerate(p8_lab): c1str = '%02X%02X%02X' % p8_rgb[idx] delta0 = delta_e_cie1976(c0, c1) delta1 = delta_e_cie1994(c0, c1) delta2 = delta_e_cie2000(c0, c1) delta3 = delta_e_cmc(c0, c1) fp.write(' <tr>\n') fp.write(' <td bgcolor=#%s>%s</td>\n' % (c0str, c0str)) fp.write(' <td bgcolor=#%s>%s</td>\n' % (c1str, c1str)) fp.write(' <td>%f</td><td>%f</td><td>%f</td><td>%f</td>\n' % (delta0, delta1, delta2, delta3)) fp.write(' </tr>\n') fp.write('</table>\n') fp.write('</html>\n')
def color_distance(clr1, clr2): return delta_e_cie1994(to_colormath(clr1), to_colormath(clr2))
This module shows some examples of Delta E calculations of varying types. """ # Does some sys.path manipulation so we can run examples in-place. # noinspection PyUnresolvedReferences import example_config from colormath.color_objects import LabColor from colormath.color_diff import delta_e_cie1976, delta_e_cie1994, \ delta_e_cie2000, delta_e_cmc # Reference color. color1 = LabColor(lab_l=0.9, lab_a=16.3, lab_b=-2.22) # Color to be compared to the reference. color2 = LabColor(lab_l=0.7, lab_a=14.2, lab_b=-1.80) print("== Delta E Colors ==") print(" COLOR1: %s" % color1) print(" COLOR2: %s" % color2) print("== Results ==") print(" CIE1976: %.3f" % delta_e_cie1976(color1, color2)) print(" CIE1994: %.3f (Graphic Arts)" % delta_e_cie1994(color1, color2)) # Different values for textiles. print(" CIE1994: %.3f (Textiles)" % delta_e_cie1994(color1, color2, K_1=0.048, K_2=0.014, K_L=2)) print(" CIE2000: %.3f" % delta_e_cie2000(color1, color2)) # Typically used for acceptability. print(" CMC: %.3f (2:1)" % delta_e_cmc(color1, color2, pl=2, pc=1)) # Typically used to more closely model human perception. print(" CMC: %.3f (1:1)" % delta_e_cmc(color1, color2, pl=1, pc=1))
def compare_thread_colors(color1, color2): # K_L=2 indicates textiles return delta_e_cie1994(color1, color2, K_L=2)
# white = (60, 100, 70) # green = (6, 35, 13) # yellow = (34, 43, 8) # orange = (40, 20, 6) my_white = rgb2lab((232, 232, 236)) my_green = rgb2lab((41, 202, 121)) my_white2 = rgb2lab((217, 233, 247)) print("my_white : %s" % my_white) print("my_green : %s" % my_green) print("my_white2 : %s" % my_white2) print("my cie2000 white->green : %s" % my_delta_e_cie2000(my_white, my_green)) print("my cie2000 white->white2: %s\n\n" % my_delta_e_cie2000(my_white, my_white2)) white = rgb_to_labcolor(232, 232, 236) green = rgb_to_labcolor(41, 202, 121) white2 = rgb_to_labcolor(217, 233, 247) distance = delta_e_cie2000(white, green) print("white : %s" % white) print("green : %s" % green) print("white2 : %s" % white2) print("cie2000 white->green : %s" % delta_e_cie2000(white, green)) print("cie2000 white->white2: %s\n" % delta_e_cie2000(white, white2)) print("cie1994 white->green : %s" % delta_e_cie1994(white, green)) print("cie1994 white->white2: %s\n" % delta_e_cie1994(white, white2)) print("cie1976 white->green : %s" % delta_e_cie1976(white, green)) print("cie1976 white->white2: %s\n" % delta_e_cie1976(white, white2))
def test_cie1994_accuracy_graphic_arts(self): result = delta_e_cie1994(self.color1, self.color2) expected = 1.249 self.assertAlmostEqual(result, expected, 3, "DeltaE CIE1994 (graphic arts) formula error. Got %.3f, expected %.3f (diff: %.3f)." % ( result, expected, result - expected))
C1 = sqrt(a1 * a1 + b1 * b1) C2 = sqrt(a2 * a2 + b2 * b2) dC = C1 - C2 dL = L1 - L2 dH = sqrt(max(0, (a1 - a2)**2 + (b1 - b2)**2 - dC * dC)) SL = 1 SC = 1 + K1 * C1 SH = 1 + K2 * C1 dE = sqrt((dL / SL)**2 + (dC / SC)**2 + (dH / SH)**2) return dE a = [0.1, 0.2, 0.3] b = [0.6, 0.5, 0.4] print(color_difference_cie1994(a, b)) from colormath.color_objects import LabColor, XYZColor, sRGBColor from colormath.color_conversions import convert_color from colormath.color_diff import delta_e_cie1994 a = sRGBColor(*a) b = sRGBColor(*b) a = convert_color(a, LabColor, target_illuminant='d65') b = convert_color(b, LabColor, target_illuminant='d65') print(delta_e_cie1994(a, b))