def difference(a, b): # HLS print(a, b) #c1 = HSLColor(hsl_h = a[0], hsl_s = a[2]/100.0, hsl_l = a[1]/100.0) #c2 = HSLColor(hsl_h = b[0], hsl_s = a[2]/100.0, hsl_l = a[1]/100.0) c1 = RGBColor(a[0], a[1], a[2]) c2 = RGBColor(b[0], b[1], b[2]) #c1.convert_to('lab') #c2.convert_to('lab') print(c1.delta_e(c2)) return c1.delta_e(c2)
def difference(a, b): # HLS print a, b #c1 = HSLColor(hsl_h = a[0], hsl_s = a[2]/100.0, hsl_l = a[1]/100.0) #c2 = HSLColor(hsl_h = b[0], hsl_s = a[2]/100.0, hsl_l = a[1]/100.0) c1 = RGBColor(a[0], a[1], a[2]) c2 = RGBColor(b[0], b[1], b[2]) #c1.convert_to('lab') #c2.convert_to('lab') print c1.delta_e(c2) return c1.delta_e(c2)
def hex_me_up(self): self.hex_value = webcolors.rgb_to_hex(self.rgb) snapped, colour_name = swatchbook.closest_delta_e('css3', self.hex_value) snapped_rgb = webcolors.hex_to_rgb(snapped) hsv = self.rgb_to_hsv(*snapped_rgb) target = RGBColor(*snapped_rgb) original = RGBColor(*self.rgb) cdist = target.delta_e(original, method="cmc") prom = Decimal(self.prominence).quantize(TWOPLACES) dist = Decimal(cdist).quantize(TWOPLACES) ELITE = False self.css = { 'r': self.rgb[0], 'g': self.rgb[1], 'b': self.rgb[2], 'hue': hsv[0], 'hex': snapped, 'name': colour_name, 'distance': float(dist), 'prominence': float(prom), 'elite': ELITE, } return self.css
def closest_delta_e(self, hex): """ Calculates the Delta E difference between a hex value and others in the specified palette and returns the closest match (CMC method) http://en.wikipedia.org/wiki/Color_difference#CMC_l:c_.281984.29 """ incumbent = RGBColor(*webcolors.hex_to_rgb(hex)) shortest_dist = None nearest_colour = None for key, details in self.colours().items(): candidate = RGBColor(*webcolors.hex_to_rgb(key)) cdist = incumbent.delta_e(candidate, method="cmc") if nearest_colour is None: nearest_colour = (candidate, key, details) shortest_dist = cdist elif cdist < shortest_dist: shortest_dist = cdist nearest_colour = (candidate, key, details) details = nearest_colour[2] name = details['name'] hex = self.hex(name) return hex, name
def closest_delta_e(self, hex): """ Calculates the Delta E difference between a hex value and others in the specified palette and returns the closest match (CMC method) http://en.wikipedia.org/wiki/Color_difference#CMC_l:c_.281984.29 """ incumbent = RGBColor(*webcolors.hex_to_rgb(hex)) shortest_dist = None nearest_colour = None for key, details in self.colours().items(): candidate = RGBColor(*webcolors.hex_to_rgb(key)) cdist = incumbent.delta_e(candidate, method="cmc") if nearest_colour is None: nearest_colour = (candidate, key, details) shortest_dist = cdist elif cdist < shortest_dist: shortest_dist = cdist nearest_colour = (candidate, key, details) details = nearest_colour[2] name = details['name'] hex = self.hex(name) return hex, name
def color_distance(color_a, color_b): from colormath.color_objects import RGBColor colora = RGBColor(rgb_r = color_a.rR, rgb_g = color_a.rG, rgb_b = color_a.rB) colorb = RGBColor(rgb_r = color_b.rR, rgb_g = color_b.rG, rgb_b = color_b.rB) return colora.delta_e(colorb, mode='cmc', pl=1, pc=1)
def find_related_pixel(self): self.is_parse = True try: fileimage = cStringIO.StringIO(urllib.urlopen(self.picture_url_high).read()) img = Image.open(fileimage) imgResze = img.resize((1, 1), Image.ANTIALIAS) pixels = list(imgResze.getdata()) except: print 'error color pixel' self.delete() return True color_insta = RGBColor(pixels[0][0],pixels[0][1],pixels[0][2]) color_hex = color_insta.get_rgb_hex() color_hex = color_hex.replace("#", "") self.color = color_hex self.r_color = pixels[0][0] self.g_color = pixels[0][1] self.b_color = pixels[0][2] pixel_asso = None for subscription in self.subscriptions.all(): print 'Subscription => %s' % subscription for mosaic in subscription.mosaics.all(): print 'Mosaic : %s' % mosaic.name try: img = img.resize((mosaic.pixel_size, mosaic.pixel_size), Image.ANTIALIAS) filepath = settings.MEDIA_ROOT + '/pics/%s_%s.png' % (mosaic.pixel_size,self.id) img.save(filepath, 'PNG') except: print 'error save pixel min' return True color_insta = RGBColor(pixels[0][0],pixels[0][1],pixels[0][2]) color_hex = color_insta.get_rgb_hex() color_hex = color_hex.replace("#", "") delta_e = 100 for pixel in mosaic.pixels.filter(pic__isnull=True): pixel_color = RGBColor(pixel.r_color,pixel.g_color,pixel.b_color) delta_e_new = color_insta.delta_e(pixel_color) #print '#'+pixel.color #print color_insta.get_rgb_hex() #print delta_e_new if delta_e_new < delta_e: delta_e = delta_e_new pixel_asso = pixel print 'minimum delta_e %s' % delta_e if pixel_asso: if delta_e < 25: print 'set pixel %s with delta %s' % (pixel_asso.id,delta_e) pixel_asso.pic = self pixel_asso.save() self.add_to_fake_mosaic(mosaic, pixel_asso) self.save()
class Color(object): @classmethod def from_string(cls, string): if not string.startswith('#'): raise ValueError(string) if len(string) == 4: args = [int(x * 2, 16) for x in string[1:]] elif len(string) == 7: args = [int(string[i:i + 2], 16) for i in xrange(1, 7, 2)] else: raise ValueError(string) return cls(*args) @classmethod def from_index(cls, index): return cls(*INDEX_TO_COLOR[index]) def __init__(self, red, green, blue): self.color = RGBColor(red, green, blue) @property def red(self): return self.color.rgb_r @property def green(self): return self.color.rgb_g @property def blue(self): return self.color.rgb_b def __eq__(self, other): return self.to_int() == other.to_int() def __ne__(self, other): return not self.__eq__(other) def __hash__(self): return self.to_int() def to_string(self): return self.color.get_rgb_hex() def to_int(self): return COLOR_TO_INDEX[self.red, self.green, self.blue] def difference_to(self, color): return self.color.delta_e(color.color) def closest(self, colors): rv = {} for color in colors: rv[color.difference_to(self)] = color return rv[min(rv)]
def get_distance(pix1, pix2): '''Computes distance beween two colors. based on colormath if available, or computes a simple euclidian distance on RGB values ''' if HasColormath: col1 = RGBColor(pix1[0], pix1[1], pix1[2]) col2 = RGBColor(pix2[0], pix2[1], pix2[2]) #dist = col1.delta_e(col2, mode='cmc', pl=1, pc=1) dist = col1.delta_e(col2) else: dist = sqrt ( pow(pix1[0]-pix2[0],2)+pow(pix1[1]-pix2[1],2)+pow(pix1[2]-pix2[2],2) ) return dist
def closest_colour(requested_colour): """Find the colour in kelly_colours that is closest to this one. requested_colour is an RGB tuple.""" rlab = RGBColor(*requested_colour).convert_to("lab") if (30 < rlab.lab_l < 70) and abs(rlab.lab_a) + abs(rlab.lab_b) == 0: # This is a greyscale colour return "#817066" better_colours = {} for key, bits in kelly_colours.items(): lab_diff = rlab.delta_e(bits[1]) better_colours[lab_diff] = key return better_colours[min(better_colours.keys())]
def update_shape_dominant_delta(shape, save=True): """ Update shape dominant_delta """ if not shape.dominant_rgb0 or not shape.dominant_rgb1: return c0 = RGBColor() c1 = RGBColor() c0.set_from_rgb_hex(shape.dominant_rgb0) c1.set_from_rgb_hex(shape.dominant_rgb1) shape.dominant_delta = c0.delta_e(c1) if save: shape.save()
def compare_colors(self, color0, color1): """ see https://de.wikipedia.org/wiki/Delta_E 0,0 … 0,5 kein bis fast kein Unterschied 0,5 … 1,0 Unterschied kann für das geübte Auge bemerkbar sein 1,0 … 2,0 merklicher Farbunterschied 2,0 … 4,0 wahrgenommener Farbunterschied 4,0 … 5,0 wesentlicher Farbunterschied, der selten toleriert wird oberhalb 5,0 die Differenz wird als andere Farbe bewertet """ c0 = RGBColor(color0[0], color0[1], color0[2]) c1 = RGBColor(color1[0], color1[1], color1[2]) return c0.delta_e(c1, mode='cie1976')
def update_shape_dominant_delta(shape, save=True): """ Update shape dominant_delta """ if not shape.dominant_rgb0 or not shape.dominant_rgb1: return c0 = RGBColor() c1 = RGBColor() c0.set_from_rgb_hex(shape.dominant_rgb0) c1.set_from_rgb_hex(shape.dominant_rgb1) shape.dominant_delta = c0.delta_e(c1) if save: shape.save()
def _get_color(self): self.nearest = None self.shortest_distance = 100 chosen_name = None for color_dict in (COLOURS, GREYSCALE): for name, color in color_dict.iteritems(): desired_rgb = color[0] target = RGBColor(*desired_rgb) cdist = target.delta_e(RGBColor(*self.rgb), method='cmc') if self.nearest is None or cdist < self.shortest_distance: self.nearest = name self.nearest_rgb = desired_rgb self.shortest_distance = cdist self.distance = cdist # print 'Checking', name (hue_lo, hue_hi) = color[1] if hue_lo > hue_hi: h = self.spin(self.hue) hue_lo = self.spin(hue_lo) hue_hi = self.spin(hue_hi) else: h = self.hue sat_range = color[2] or DEFAULT_SAT val_range = color[3] or DEFAUL_VAL if h in range(hue_lo, hue_hi + 1) and \ self.sat in range(sat_range[0], sat_range[1] + 1) and \ self.val in range(val_range[0], val_range[1] + 1): # TODO set up desirable hues, sat and b per named colour target = RGBColor(*desired_rgb) self.distance = cdist chosen_name = name self.nearest_hex = webcolors.rgb_to_hex(self.nearest_rgb) return chosen_name return None
def lab_distance(self, rgb1, rgb2): lab1 = RGBColor(rgb1[0],rgb1[1], rgb1[2]).convert_to('lab') lab2 = RGBColor(rgb2[0],rgb2[1], rgb2[2]).convert_to('lab') return round(lab1.delta_e(lab2), 2)
def distance(self, r, g, b): target = RGBColor(r, g, b) return target.delta_e(RGBColor(*self.rgb), method="cmc")