def test_cmc_accuracy(self): # Test 2:1 result = delta_e_cmc(self.color1, self.color2, pl=2, pc=1) expected = 1.443 self.assertAlmostEqual( result, expected, 3, "DeltaE CMC (2:1) formula error. Got %.3f, expected %.3f (diff: %.3f)." % (result, expected, result - expected)) # Test against 1:1 as well result = delta_e_cmc(self.color1, self.color2, pl=1, pc=1) expected = 1.482 self.assertAlmostEqual( result, expected, 3, "DeltaE CMC (1:1) formula error. Got %.3f, expected %.3f (diff: %.3f)." % (result, expected, result - expected)) # Testing atan H behavior. atan_color1 = LabColor(lab_l=69.417, lab_a=-12.612, lab_b=-11.271) atan_color2 = LabColor(lab_l=83.386, lab_a=39.426, lab_b=-17.525) result = delta_e_cmc(atan_color1, atan_color2) expected = 44.346 self.assertAlmostEqual( result, expected, 3, "DeltaE CMC Atan test formula error. Got %.3f, expected %.3f (diff: %.3f)." % (result, expected, result - expected))
def labDiff(self, color1, color2): # Find the difference for two lab CIE colors: https://python-colormath.readthedocs.io/en/latest/delta_e.html lab1 = LabColor(color1[0], color1[1], color1[2]) lab2 = LabColor(color2[0], color2[1], color2[2]) delta_e = delta_e_cie1976(lab1, lab2) return delta_e
def color_distance_cie2000(self, rgb_1, rgb_2, Kl=1, Kc=1, Kh=1): """计算色差""" lab1 = RGB2Lab(rgb_1[::-1]) lab2 = RGB2Lab(rgb_2[::-1]) color1 = LabColor(lab_l=lab1[0], lab_a=lab1[1], lab_b=lab1[2]) color2 = LabColor(lab_l=lab2[0], lab_a=lab2[1], lab_b=lab2[2]) delta_e = delta_e_cie2000(color1, color2, Kl=Kl, Kc=Kc, Kh=Kh) return delta_e
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 __init__(self, background, foreground, name=None): """Initialize with base shades. Accent colors may remain empty.""" self.name = name self.bg = LabColor(*background, illuminant=DAYLIGHT_NOON) self.fg = LabColor(*foreground, illuminant=DAYLIGHT_NOON) self.contrast = self.fg.lab_l - self.bg.lab_l self.base_shades = {} self.accent_colors = {}
def test_cmc_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_cmc(standard, sample)
def getdelta_e_cie2000(l1, a1, b1, l2, a2, b2): # Reference color. color1 = LabColor(lab_l=l1, lab_a=a1, lab_b=b1) # Color to be compared to the reference. color2 = LabColor(lab_l=l2, lab_a=a2, lab_b=b2) # This is your delta E value as a float. delta_e = np.round(delta_e_cie2000(color1, color2, Kl=1, Kc=1, Kh=1), 5) return delta_e
def compare_similar_hist(self, rgb_1, rgb_2): lab_l, lab_a, lab_b = self.RGB2Lab(rgb_1) color1 = LabColor(lab_l, lab_a, lab_b) lab_l, lab_a, lab_b = self.RGB2Lab(rgb_2) color2 = LabColor(lab_l, lab_a, lab_b) delta_e = delta_e_cie2000(color1, color2) # delta_e = delta_e_cmc(color1, color2) return delta_e
def comparePixels(arr1, arr2, eps=0.001): rgb_pixel1 = [[arr1]] rgb_pixel2 = [[arr2]] lab1 = color.rgb2lab(rgb_pixel1) lab2 = color.rgb2lab(rgb_pixel2) color1 = LabColor(lab1[0][0][0], lab1[0][0][1], lab1[0][0][2]) color2 = LabColor(lab2[0][0][0], lab2[0][0][1], lab2[0][0][2]) if delta_e_cie1976(color1, color2) <= eps: return True return False
def color_diff(img1, img2, img_scan): deltas = [] for i in range(img1.shape[0]): for j in range(img1.shape[1]): color1_lab = LabColor(img_scan[i][j], img1[i][j][0], img1[i][j][1]) color2_lab = LabColor(img_scan[i][j], img2[i][j][0], img2[i][j][1]) delta_e = delta_e_cie2000(color1_lab, color2_lab) deltas.append(delta_e) return deltas
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 color_distance_lab2(rgb_1, rgb_2): from colormath.color_objects import LabColor from colormath.color_diff import delta_e_cie1976 lab1 = RGB2Lab(rgb_1[::-1]) lab2 = RGB2Lab(rgb_2[::-1]) color1 = LabColor(lab_l=lab1[0], lab_a=lab1[1], lab_b=lab1[2]) color2 = LabColor(lab_l=lab2[0], lab_a=lab2[1], lab_b=lab2[2]) delta_e = delta_e_cie2000(color1, color2) # print(delta_e) return 200 - delta_e
def assignbins(lab_pixel_arr): print("assignbins") midpts = [] x = [-127, 0, 127] mid_pts = [p for p in itertools.product(x, repeat=3)] arr_bins = [] signature_list = [] for i in range(32): #print "a1____" for j in range(32): l_bin = [] for x in range(len(mid_pts)): a = [mid_pts[x], 0] l_bin.append(a) for k in range(8): ##print "a3____" for l in range(8): #print "a4____" temp_arr = lab_pixel_arr[i * 8 + k][j * 8 + l] score = 1000 min_bin = 1000 for m in range(len(l_bin)): if (score > delta_e_cie2000( temp_arr, LabColor(l_bin[m][0][0], l_bin[m][0][1], l_bin[m][0][2]))): score = delta_e_cie2000( temp_arr, LabColor(l_bin[m][0][0], l_bin[m][0][1], l_bin[m][0][2])) min_bin = m #print min_bin l_bin[min_bin][1] += 1 temp_pixel = [] for x in range(3): temp_pixel.append((l_bin[min_bin][0][x] + temp_arr.get_value_tuple()[x]) / 2) temp_pixel = tuple(temp_pixel) l_bin[min_bin][0] = temp_pixel #print "a____" arr_bins.append(l_bin) print("after assignbins") for i in arr_bins: for j in range(len(i)): signature_list.append(i[j][1]) return signature_list
def color_distance_lab2(rgb_1, rgb_2): from colormath.color_objects import LabColor from colormath.color_diff import delta_e_cie1976 lab1 = RGB2Lab(rgb_1[::-1]) lab2 = RGB2Lab(rgb_2[::-1]) # Reference color. color1 = LabColor(lab_l=lab1[0], lab_a=lab1[1], lab_b=lab1[2]) # Color to be compared to the reference. color2 = LabColor(lab_l=lab2[0], lab_a=lab2[1], lab_b=lab2[2]) # This is your delta E value as a float. delta_e = delta_e_cie1976(color1, color2) return delta_e
def compare_similar_hist(rgb_1, rgb_2): # print("rgb1",rgb_1) # print("rgb2",rgb_2) lab_l, lab_a, lab_b = RGB2Lab(rgb_1) color1 = LabColor(lab_l, lab_a, lab_b) lab_l, lab_a, lab_b = RGB2Lab(rgb_2) color2 = LabColor(lab_l, lab_a, lab_b) delta_e = delta_e_cie2000(color1, color2) # delta_e = delta_e_cmc(color1, color2) return delta_e
def __init__(self, name, background, foreground, shade_specs): self.name = name self.slug = slugify(name) self.bg = LabColor(*background) self.fg = LabColor(*foreground) self.contrast = self.fg.lab_l - self.bg.lab_l self.color_coords = {} self.color_coords["fg_0"] = foreground self.color_coords["bg_0"] = background self.build_shades(shade_specs)
def color_distance(rgb_1, rgb_2): """ :param rgb_1: rgb :param rgb_2: rgb :return: """ lab1 = RGB2Lab(rgb_1[::-1]) lab2 = RGB2Lab(rgb_2[::-1]) color1 = LabColor(lab_l=lab1[0], lab_a=lab1[1], lab_b=lab1[2]) color2 = LabColor(lab_l=lab2[0], lab_a=lab2[1], lab_b=lab2[2]) delta_e = delta_e_cie2000(color1, color2, Kl=3, Kc=1, Kh=1) # Kl 亮度, Kc 饱和度, Kh 色调 的权重 return delta_e, lab1, lab2
def __init__(self, name, background, foreground, shade_specs=None): """Initialize with base shades. Accent colors may remain empty.""" self.name = name self.slug = slugify(name) self.bg = LabColor(*background, illuminant="d50") self.fg = LabColor(*foreground, illuminant="d50") self.contrast = self.fg.lab_l - self.bg.lab_l self.base_shades = {} self.accent_colors = {} if shade_specs: self.build_shades(shade_specs)
def deltaE(color1, color2): """ :param color1: openCV lab (numpy array) :param color2: :return: """ C1 = LabColor( float(color1[0]) / 255 * 100, float(color1[1] - 127.0), float(color1[2] - 127.0)) C2 = LabColor( float(color2[0]) / 255 * 100, float(color2[1] - 127.0), float(color2[2] - 127.0)) return delta_e_cie2000(C1, C2)
def color_distance(self, rgb_1, rgb_2): """ :param rgb_1: rgb :param rgb_2: rgb :return: """ lab1 = RGB2Lab(rgb_1[::-1]) lab2 = RGB2Lab(rgb_2[::-1]) color1 = LabColor(lab_l=lab1[0], lab_a=lab1[1], lab_b=lab1[2]) color2 = LabColor(lab_l=lab2[0], lab_a=lab2[1], lab_b=lab2[2]) delta_e = delta_e_cie2000(color1, color2) return delta_e
def test_cie2000_accuracy_2(self): """ Follow a different execution path based on variable values. """ # These values are from ticket 8 in regards to a CIE2000 bug. c1 = LabColor(lab_l=32.8911, lab_a=-53.0107, lab_b=-43.3182) c2 = LabColor(lab_l=77.1797, lab_a=25.5928, lab_b=17.9412) result = delta_e_cie2000(c1, c2) expected = 78.772 self.assertAlmostEqual( result, expected, 3, "DeltaE CIE2000 formula error. Got %.3f, expected %.3f (diff: %.3f)." % (result, expected, result - expected))
def select_task_by_priority(conn, L, A, B): result = [] root = tk.Tk() cur = conn.cursor() cur.execute("SELECT Partnumber,Colorname,L,A,B FROM reading ") rows = cur.fetchall() color1 = LabColor(lab_l=L, lab_a=A, lab_b=B) for row in rows: color2 = LabColor(lab_l=row[2], lab_a=row[3], lab_b=row[4]) if (delta_e_cie1976(color1, color2) < 100): result.append(row[0:2]) result.extend(delta_e_cie1976(color1, color2)) print(result)
def get_lab_color(self): """ Return the Lab color instance for this swatch. :return: colormath.color_objects.LabColor """ return LabColor(self.lab_l, self.lab_a, self.lab_b)
def fade_colors_rgb(self,rgbcolor1,rgbcolor2,speed=0.1): """ Values for color conversion: Best result for me: target_illuminant=d50 target_rgb=sRGB target_illuminant= 'a' 'b' 'c' 'd50' 'd55' 'd65' 'd75' 'e' 'f2' 'f7' 'f11' target_rgb= 'adobe_rgb' 'apple_rgb' 'best_rgb' 'bruce_rgb' 'cie_rgb' 'colormatch_rgb' 'don_rgb_4' 'eci_rgb' 'ekta_space_ps5' 'ntsc_rgb' 'pal_secam_rgb' 'prophoto_rgb' 'smpte_c_rgb' 'srgb' 'wide_gamut_rgb' """ rgb1 = RGBColor(rgbcolor1[0],rgbcolor1[1],rgbcolor1[2]) rgb2 = RGBColor(rgbcolor2[0],rgbcolor2[1],rgbcolor2[2]) l1 = rgb1.convert_to('lab',target_illuminant='d50') l2 = rgb2.convert_to('lab',target_illuminant='d50') lab1 =[l1.lab_l,l1.lab_a,l1.lab_b] lab2 =[l2.lab_l,l2.lab_a,l2.lab_b] for i in range(0,self.fade_steps+1): l=self.transition3(i,self.fade_steps,lab1,lab2) lab=LabColor(l[0],l[1],l[2]) r=lab.convert_to('rgb') rgb=[r.rgb_r,r.rgb_g,r.rgb_b] self.set_color_rgb(rgb) sleep(speed)
def XYZ_to_Lab(cobj, *args, **kwargs): """ Converts XYZ to Lab. """ illum = cobj.get_illuminant_xyz() temp_x = cobj.xyz_x / illum["X"] temp_y = cobj.xyz_y / illum["Y"] temp_z = cobj.xyz_z / illum["Z"] if temp_x > color_constants.CIE_E: temp_x = math.pow(temp_x, (1.0 / 3.0)) else: temp_x = (7.787 * temp_x) + (16.0 / 116.0) if temp_y > color_constants.CIE_E: temp_y = math.pow(temp_y, (1.0 / 3.0)) else: temp_y = (7.787 * temp_y) + (16.0 / 116.0) if temp_z > color_constants.CIE_E: temp_z = math.pow(temp_z, (1.0 / 3.0)) else: temp_z = (7.787 * temp_z) + (16.0 / 116.0) lab_l = (116.0 * temp_y) - 16.0 lab_a = 500.0 * (temp_x - temp_y) lab_b = 200.0 * (temp_y - temp_z) return LabColor( lab_l, lab_a, lab_b, observer=cobj.observer, illuminant=cobj.illuminant)
def calculate_new_color(palette, colors, distance_L, distance_A, distance_B, param, Phi, count): ''' this function converts all the colors of an image to new_colors ''' w = [1 / count for i in range(count)] start = time.time() # print("distance_L", distance_L) # print("distance_A", distance_A) # print("distance_B", distance_B) new_color = defaultdict(int) # new_color = dict() t = LabColor(0.0, 0.0, 0.0) a = numpy.array(Phi) palette_lab = [RGB2LAB1(p) for p in palette] for mylab in colors.keys(): # w = [0 for z in range(count)] # print("color in RGB", LAB2RGB1(mylab)) # print("color in lab space", mylab) tempphi = [0 for z in range(count)] if (param != 0): for index, p in enumerate(palette_lab): temp_lab = (mylab.lab_l, mylab.lab_a, mylab.lab_b) p1 = (p.lab_l, p.lab_a, p.lab_b) r = CalculateLABDistance2(temp_lab, p1) tempphi[index] = math.exp(-r * param) b = numpy.array(tempphi) try: w = numpy.linalg.solve(a, b) except Exception as e: print(str(e)) # print("weight", w) delta_L = 0 delta_A = 0 delta_B = 0 scale = 0 for weight in w: scale = scale + max(weight, 0) for index, weight in enumerate(w): if (weight > 0): # print("weight/scale", weight/scale, " distance_L[index]", distance_L[index]) delta_L = delta_L + weight / scale * distance_L[index] delta_A = delta_A + weight / scale * distance_A[index] delta_B = delta_B + weight / scale * distance_B[index] t.lab_a = delta_A + mylab.lab_a t.lab_b = delta_B + mylab.lab_b t.lab_l = mylab.lab_l # t.lab_l = delta_L + mylab.lab_l # print(t) # print("new color in lab space", t) rgb = LAB2RGB1(t) # print("new color in rgb", rgb) LAB = (mylab.lab_l, mylab.lab_a, mylab.lab_b) new_color[LAB] = rgb #print("size of new_color list", len(new_color)) # print("key, value", new_color.keys(), new_color.values()) end = time.time() #print('calculate_new_color took', end - start) return new_color
def get_colors(image_path): try: modified_image = blur_and_resize(image_path) color_dict = {} y = 0 results = {} for co in modified_image.getcolors((50 * 50)): l = luminance(co[1][0], co[1][1], co[1][2]) if check_against_dict(l, color_dict): color_dict[y] = json.dumps({ 'luminance': l, 'rgb': { 'red': co[1][0], 'green': co[1][1], 'blue': co[1][2] } }) for h in lab_def_colors: c = json.loads(color_dict[y])['rgb'] lab = convert_color( sRGBColor(c['red'], c['green'], c['blue']), LabColor) delta_e = delta_e_cie2000( LabColor(lab_def_colors[h]['l'], lab_def_colors[h]['a'], lab_def_colors[h]['b']), lab) if results.get(h) is None or delta_e < results[h]: results[h] = delta_e y += 1 return sorted(results.items(), key=lambda x: x[1]) except Exception as e: print(e)
def get_color_attributes(self): """ Calculates the color attributes of the vertices. :return: None """ # First, get colors in LAB and then convert to RGB hex colors to pass to the model. # TODO better way to calculate number of rows? num_rows = 10 #math.ceil(math.sqrt(len(self.loader.vertex_ids))) # constant # 100 here and 36 for l or 128 here and 20 for l f_lab_range = 100.0 colors_lab = np.ndarray((100, 3), dtype=float) colors_lab[:, 0] = 36 pos = self.loader.positions[:, 1:3] colors_lab[:, 1:3] = ( (2 * f_lab_range * pos[:, 0:2]) / num_rows) - f_lab_range colors_res = [] # TODO not always 100? for i in range(100): currcol = colors_lab[i] lab = LabColor(currcol[0], currcol[1], currcol[2]) res = convert_color(lab, sRGBColor) colors_res.append(res.get_rgb_hex()) # Set the colors in the model self.model.set_colors(colors_res)
def lab_gradient(slab, elab, soff, eoff, off): svals = slab.get_value_tuple() evals = elab.get_value_tuple() return LabColor(*[ linear_gradient(start_value, end_value, soff, eoff, off) for start_value, end_value in zip(svals, evals) ])
def cielab2rgb(self, c): from colormath.color_objects import LabColor, sRGBColor from colormath.color_conversions import convert_color lab = LabColor(c[0], c[1], c[2]) rgb = convert_color(lab, sRGBColor) return np.array( [rgb.clamped_rgb_r, rgb.clamped_rgb_g, rgb.clamped_rgb_b])