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 assignColorNames(data, names): from colormath.color_objects import RGBColor result = {} for key in data: rgb = data[key] #print "=== RGB Example: RGB->LAB ===" # Instantiate an Lab color object with the given values. rgb = RGBColor(rgb[0], rgb[1], rgb[2], rgb_type='sRGB') # Show a string representation. #print rgb # Convert RGB to LAB using a D50 illuminant. lab = rgb.convert_to('lab', target_illuminant='D65') #print lab #print "=== End Example ===\n" # 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) color2 = lab res = (1.E100, '') for c in names: rgb = data[c] rgb = RGBColor(rgb[0], rgb[1], rgb[2], rgb_type='sRGB') color1 = rgb.convert_to('lab', target_illuminant='D65') #print "== Delta E Colors ==" #print " COLOR1: %s" % color1 #print " COLOR2: %s" % color2 #print "== Results ==" #print " CIE2000: %.3f" % color1.delta_e(color2, mode='cie2000') ## Typically used for acceptability. #print " CMC: %.3f (2:1)" % color1.delta_e(color2, mode='cmc', pl=2, pc=1) ## Typically used to more closely model human percetion. #print " CMC: %.3f (1:1)" % color1.delta_e(color2, mode='cmc', pl=1, pc=1) r = color1.delta_e(color2, mode='cmc', pl=2, pc=1) if (r < res[0]): res = (r, c, data[c]) # data[key]['Color'] = res[1] # data[key]['Delta_E'] = res[0] # data[key]['RGBref'] = res[2] result['%s (%s)' % (key, res[1])] = data[key] return result
def get_lab(self): """ returns the current point in L*a*b* """ if not hasattr(self, '_lab'): c = RGBColor() c.set_from_rgb_hex(self.sRGB) self._lab = c.convert_to('lab').get_value_tuple() return self._lab
def extract_bsdf(wd): color = RGBColor() color.set_from_rgb_hex(wd.color) lab = color.convert_to('lab') c = wd.contrast d = wd.d() return lab, (c, d)
def get_lab(self): """ returns the current point in L*a*b* """ if not hasattr(self, '_lab'): c = RGBColor() c.set_from_rgb_hex(self.sRGB) self._lab = c.convert_to('lab').get_value_tuple() return self._lab
def extract_bsdf(wd): color=RGBColor() color.set_from_rgb_hex(wd.color) lab=color.convert_to('lab') c=wd.contrast d=wd.d() return lab,(c,d)
def contrast_text(self): """Returns hex code of a color that contrasts with this one, for overlaying text. Includes the #.""" # get rgb and hsv values rgbcolor = RGBColor() rgbcolor.set_from_rgb_hex(self.color_hex) hsvcolor = rgbcolor.convert_to('hsv') new_v = hsvcolor.hsv_v; if new_v <= .55: new_v = 1.0; elif new_v > .55: new_v = 0.0; new_h = hsvcolor.hsv_h new_s = 0 contrast = HSVColor(hsv_h = new_h, hsv_s = new_s, hsv_v = new_v) contrast_rgb = contrast.convert_to('rgb') return contrast_rgb.get_rgb_hex()
def rgbColorPickerUpdated(self, valuesDict, typeId, devId): try: self.debugLog(u"Starting rgbColorPickerUpdated.") self.debugLog(u"typeId: " + typeId + "\ndevId: " + unicode(devId) + "\nvaluesDict: " + unicode(valuesDict)) # Get the raw 3 byte, space-separated hex string from the color picker. rgbHexList = valuesDict['rgbColor'].split() # Assign the RGB values. red = int(rgbHexList[0], 16) green = int(rgbHexList[1], 16) blue = int(rgbHexList[2], 16) # Convert the RGB values to HSL/HSV for use in the HSB actions. rgb = RGBColor(red, green, blue, rgb_type='wide_gamut_rgb') hsb = rgb.convert_to('hsv') hue = int(round(hsb.hsv_h * 1.0)) saturation = int(round(hsb.hsv_s * 100.0)) brightness = int(round(hsb.hsv_v * 100.0)) # Assign the values to the appropriate valuesDict items. valuesDict['red'] = red valuesDict['green'] = green valuesDict['blue'] = blue valuesDict['hue'] = hue valuesDict['saturation'] = saturation valuesDict['brightness'] = brightness # Can send a live update to the hardware here: # self.sendSetRGBWCommand(valuesDict, typeId, devId) del valuesDict['rgbColor'] return (valuesDict) except Exception: t, v, tb = sys.exc_info() self.handle_exception(t,v,tb)
def handle(self, *args, **options): comparisons = [] comparisons += IntrinsicPointComparison.objects.all() \ .filter(point1_image_darker__isnull=True) \ .values_list('id', 'point1__sRGB', 'point2__sRGB') comparisons += IntrinsicPointComparisonResponse.objects.all() \ .filter(reflectance_eq=False, reflectance_dd__isnull=True) \ .order_by().distinct('comparison') \ .values_list('comparison__id', 'comparison__point1__sRGB', 'comparison__point2__sRGB') comparisons = list(set(comparisons)) for (id, sRGB1, sRGB2) in progress_bar(comparisons): c1 = RGBColor() c1.set_from_rgb_hex(sRGB1) l1 = c1.convert_to('lab').lab_l c2 = RGBColor() c2.set_from_rgb_hex(sRGB2) l2 = c2.convert_to('lab').lab_l if l1 < l2: IntrinsicPointComparison.objects \ .filter(id=id).update(point1_image_darker=True) IntrinsicPointComparisonResponse.objects \ .filter(comparison_id=id, darker="1") \ .update(reflectance_eq=False, reflectance_dd=True) IntrinsicPointComparisonResponse.objects \ .filter(comparison_id=id, darker="2") \ .update(reflectance_eq=False, reflectance_dd=False) else: IntrinsicPointComparison.objects \ .filter(id=id).update(point1_image_darker=False) IntrinsicPointComparisonResponse.objects \ .filter(comparison_id=id, darker="1") \ .update(reflectance_eq=False, reflectance_dd=False) IntrinsicPointComparisonResponse.objects \ .filter(comparison_id=id, darker="2") \ .update(reflectance_eq=False, reflectance_dd=True) IntrinsicPointComparisonResponse.objects \ .filter(comparison_id=id, darker="E") \ .update(reflectance_eq=True, reflectance_dd=None)
def save(self, *args, **kwargs): if (self.color_L is None) or (self.color_a is None) or (self.color_b is None): c = RGBColor() c.set_from_rgb_hex(self.color) c = c.convert_to('lab') self.color_L = c.lab_l self.color_a = c.lab_a self.color_b = c.lab_b super(ShapeBsdfLabel_wd, self).save(*args, **kwargs)
def color(v, echelle, tohex=True): """ Retourne la couleur d'une valeur intermediaire. """ # Utilisation d'un régression linéaire des valeurs HSV (hue, saturation, value) # de 2 couleurs (même méthode que l'algorithme Lab-LCH d'ArcGIS). keys = echelle.keys() keys.sort() if v < min(keys): v = min(keys) if v > max(keys): v = max(keys) if v in keys: rgb = RGBColor(*echelle[v]) if tohex: return rgb.get_rgb_hex() else: return rgb.get_value_tuple() kmin, kmax = None, None vmin, vmax = None, None for i in range(len(keys) - 1): if v > keys[i] and v < keys[i + 1]: kmin, kmax = i, i + 1 vmin, vmax = keys[i], keys[i + 1] break if kmin is None or kmax is None or vmin is None or vmax is None: return None rgb_a = RGBColor(*echelle[vmin]) hsv_a = rgb_a.convert_to('hsv') rgb_b = RGBColor(*echelle[vmax]) hsv_b = rgb_b.convert_to('hsv') xa = keys[kmin] xb = keys[kmax] xi = v hi = eq(xi, xa, xb, hsv_a.hsv_h, hsv_b.hsv_h) si = eq(xi, xa, xb, hsv_a.hsv_s, hsv_b.hsv_s) vi = eq(xi, xa, xb, hsv_a.hsv_v, hsv_b.hsv_v) hsv_i = HSVColor(hi, si, vi) rgb_i = hsv_i.convert_to('rgb') if tohex: return rgb_i.get_rgb_hex() else: return rgb_i.get_value_tuple()
def alter_lch(hex_color, value, component='L', relative=True): rgb_color = RGBColor() rgb_color.set_from_rgb_hex(hex_color) lch_color = rgb_color.convert_to('lchab') lch_lst = list(lch_color.get_value_tuple()) comp_idx = ('L', 'C', 'H').index(component) lch_lst[comp_idx] = lch_lst[comp_idx] + value if relative else value L, C, H = lch_lst lch_res = LCHabColor(L, C, H) return lch_to_hex(lch_res)
def set_rgb(self, rgb): ''' Pass an RGB value to the classifier ''' rgb = RGBColor(*rgb) logger.debug(rgb.get_rgb_hex()) self.lab = rgb.convert_to('lab') logger.debug('Saved lab: {lab} from rgb: {rgb}'.format( lab=self._lab_to_tuple(self.lab), rgb=rgb)) self._update_lab_colors()
def save(self, *args, **kwargs): if (self.color_L is None) or (self.color_a is None) or (self.color_b is None): c = RGBColor() c.set_from_rgb_hex(self.color) c = c.convert_to('lab') self.color_L = c.lab_l self.color_a = c.lab_a self.color_b = c.lab_b super(ShapeBsdfLabel_wd, self).save(*args, **kwargs)
def alter_lch(hex_color, value, component='L', relative=True): rgb_color = RGBColor() rgb_color.set_from_rgb_hex(hex_color) lch_color = rgb_color.convert_to('lchab') lch_lst = list(lch_color.get_value_tuple()) comp_idx = ('L', 'C', 'H').index(component) lch_lst[comp_idx] = lch_lst[comp_idx] + value if relative else value L, C, H = lch_lst lch_res = LCHabColor(L, C, H) return lch_to_hex(lch_res)
def E2(shape): if shape.substance_entropy > 2.0: yield 10000.0 yield 10000.0 yield 10000.0 yield 10000.0 return color = RGBColor() color.set_from_rgb_hex(shape.dominant_rgb0) lab2 = color.convert_to('lab') yield lab.delta_e(lab2) # cie2000 delta e color.set_from_rgb_hex(shape.dominant_rgb1) lab2 = color.convert_to('lab') yield lab.delta_e(lab2) # cie2000 delta e color.set_from_rgb_hex(shape.dominant_rgb2) lab2 = color.convert_to('lab') yield lab.delta_e(lab2) # cie2000 delta e color.set_from_rgb_hex(shape.dominant_rgb3) lab2 = color.convert_to('lab') yield lab.delta_e(lab2) # cie2000 delta e
def E2(shape): if shape.substance_entropy>2.0: yield 10000.0 yield 10000.0 yield 10000.0 yield 10000.0 return color=RGBColor() color.set_from_rgb_hex(shape.dominant_rgb0) lab2=color.convert_to('lab') yield lab.delta_e(lab2) # cie2000 delta e color.set_from_rgb_hex(shape.dominant_rgb1) lab2=color.convert_to('lab') yield lab.delta_e(lab2) # cie2000 delta e color.set_from_rgb_hex(shape.dominant_rgb2) lab2=color.convert_to('lab') yield lab.delta_e(lab2) # cie2000 delta e color.set_from_rgb_hex(shape.dominant_rgb3) lab2=color.convert_to('lab') yield lab.delta_e(lab2) # cie2000 delta e
def set_rgb(self, rgb): ''' Pass an RGB value to the classifier ''' rgb = RGBColor(*rgb) logger.debug(rgb.get_rgb_hex()) self.lab = rgb.convert_to('lab') logger.debug('Saved lab: {lab} from rgb: {rgb}'.format( lab=self._lab_to_tuple(self.lab), rgb=rgb)) self._update_lab_colors()
def example_rgb_to_xyz(): """ The reverse is similar. """ print "=== RGB Example: RGB->XYZ ===" # Instantiate an Lab color object with the given values. rgb = RGBColor(120, 130, 140, rgb_type='sRGB') # Show a string representation. print rgb # Convert RGB to XYZ using a D50 illuminant. xyz = rgb.convert_to('xyz', target_illuminant='D50') print xyz print "=== End Example ===\n"
def example_rgb_to_xyz(): """ The reverse is similar. """ print "=== RGB Example: RGB->XYZ ===" # Instantiate an Lab color object with the given values. rgb = RGBColor(120, 130, 140, rgb_type='sRGB') # Show a string representation. print rgb # Convert RGB to XYZ using a D50 illuminant. xyz = rgb.convert_to('xyz', target_illuminant='D50') print xyz print "=== End Example ===\n"
def refresh_values(self): if not self.color_hex.islower(): self.color_hex = self.color_hex.lower() # get rgb and hsv values rgbcolor = RGBColor() rgbcolor.set_from_rgb_hex(self.color_hex) hsvcolor = rgbcolor.convert_to('hsv') self.R = rgbcolor.rgb_r self.G = rgbcolor.rgb_g self.B = rgbcolor.rgb_b self.H = round(hsvcolor.hsv_h) # need to multiply by 100 to get the percent self.S = round(hsvcolor.hsv_s * 100.0) self.V = round(hsvcolor.hsv_v * 100.0) # make rounded values self.rR = round_rgb_colorvalue(self.R) self.rG = round_rgb_colorvalue(self.G) self.rB = round_rgb_colorvalue(self.B) round_rgb = RGBColor(rgb_r = self.rR, rgb_g = self.rG, rgb_b = self.rB) round_hsv = round_rgb.convert_to('hsv') self.rounded_hex = round_rgb.get_rgb_hex()[1:7] self.rH = round_hsv.hsv_h self.rS = round_hsv.hsv_s self.rV = round_hsv.hsv_v # check to see if this is a round color if self.R == self.rR and self.G == self.rG and self.B == self.rB: self.is_round = True else: self.is_round = False
def set_light(self, light, hex, brightness=0, on=True): rgb = RGBColor() rgb.set_from_rgb_hex(hex) color = rgb.convert_to('hsv') bri = int(color.hsv_v * 254) hue = int(color.hsv_h * 200) sat = int(color.hsv_s * 254) if brightness > 0: bri = brightness params = { "on": on, "bri": bri, "hue": hue, "sat": sat, } params = json.dumps(params) self.send_data(light, params)
def _rgb_to_lab(self, rgb): rgb = RGBColor(*rgb) return rgb.convert_to('lab')
def hex_to_lch(hex_color): rgb_color = RGBColor() rgb_color.set_from_rgb_hex(hex_color) return rgb_color.convert_to('lchab')
def getLAB(self): rgb = self.getRGB() rgbObj = RGBColor( rgb[0], rgb[1], rgb[2], rgb_type='sRGB' ) labObj = rgbObj.convert_to('lab') return labObj
def rgb_to_hsv(rgb): rgbcolor = RGBColor(*rgb) hsvcolor = rgbcolor.convert_to('hsv') return (hsvcolor.hsv_l, hsvcolor.hsv_a, hsvcolor.hsv_b)
def rgb_to_lab(rgb): rgbcolor = RGBColor(*rgb) labcolor = rgbcolor.convert_to('lab') return (labcolor.lab_l, labcolor.lab_a, labcolor.lab_b)
def hex_to_lch(hex_color): rgb_color = RGBColor() rgb_color.set_from_rgb_hex(hex_color) return rgb_color.convert_to('lchab')
def rgb_to_luv(rgb): rgbcolor = RGBColor(*rgb) luvcolor = rgbcolor.convert_to('xyz').convert_to('luv') return (luvcolor.luv_l, luvcolor.luv_u, luvcolor.luv_v)
def _rgb_to_lab(self, rgb): rgb = RGBColor(*rgb) return rgb.convert_to('lab')
def rgb_to_wheel(rgb): rgbcolor = RGBColor(*rgb) hsvcolor = rgbcolor.convert_to('hsv') angle = hsvcolor.hsv_h*2*math.pi/360 return (math.sin(angle) * hsvcolor.hsv_s, math.cos(angle) * hsvcolor.hsv_s)
def color_distance(sample, standard): rgb_samp = RGBColor(sample[0], sample[1], sample[2]) rgb_std = RGBColor(standard[0], standard[1], standard[2]) lab_samp = rgb_samp.convert_to('lab') lab_std = rgb_std.convert_to('lab') return lab_std.delta_e(lab_samp, mode='cmc')