def convert(dict, illuminant='D65'): """ Converts the set of XYZ colours into other spaces: Lab, LCH, sRGB """ illuminant = colour.ILLUMINANTS['CIE 1931 2 Degree Standard Observer'][ illuminant] dict['Lab'] = colour.XYZ_to_Lab(dict['XYZ'] / 100., illuminant) # beware: expects XYZ in [0,1] dict['LCH'] = colour.Lab_to_LCHab(dict['Lab']) dict['RGB'] = colour.XYZ_to_sRGB( dict['XYZ'] / 100., illuminant) # beware: expects XYZ in [0,1]
def set_convertor(name, ill='D65'): """ Binds the conversion functions LCH2RGB() and RGB2LCH() to the choosen colour package """ global LCH2RGB, RGB2LCH, convertor, illuminant if name not in ['custom', 'colorspacious', 'colourscience']: print("Unknown conversion module") return convertor = name illuminant = ill if name == 'custom': LCH2RGB = lambda L, C, H: XYZ2RGB(Lab2XYZ(LCH2Lab((L, C, H)))) RGB2LCH = lambda R, G, B: Lab2LCH(XYZ2Lab(RGB2XYZ((R, G, B)))) if name == 'colorspacious': from colorspacious import cspace_convert func_LCH2RGB = lambda L, C, H: cspace_convert([L, C, H], { "name": "CIELCh", "XYZ100_w": ill }, "sRGB1") func_RGB2LCH = lambda R, G, B: cspace_convert([R, G, B], "sRGB1", { "name": "CIELCh", "XYZ100_w": ill }) if name == 'colourscience': import colour as cs cs_ill = cs.ILLUMINANTS['CIE 1931 2 Degree Standard Observer'][ill] func_LCH2RGB = lambda L, C, H: cs.XYZ_to_sRGB( cs.Lab_to_XYZ(cs.LCHab_to_Lab([L, C, H]), illuminant=cs_ill)) func_RGB2LCH = lambda R, G, B: cs.Lab_to_LCHab( cs.XYZ_to_Lab(cs.sRGB_to_XYZ([R, G, B]), illuminant=cs_ill)) if name == 'colorspacious' or name == 'colourscience': def LCH2RGB(L, C, H): if hasattr(L, '__iter__'): RGB = np.array(list(map(func_LCH2RGB, L, C, H))) R = RGB[:, 0] G = RGB[:, 1] B = RGB[:, 2] else: R, G, B = func_LCH2RGB(L, C, H) return R, G, B def RGB2LCH(R, G, B): if hasattr(R, '__iter__'): LCH = np.array(list(map(func_RGB2LCH, R, G, B))) L = LCH[:, 0] C = LCH[:, 1] H = LCH[:, 2] else: L, C, H = func_RGB2LCH(R, G, B) return L, C, H print("convertor = '%s' (illuminant = '%s')" % (name, illuminant))
def get_main_color(R,G,B): sRGB = [R/255,G/255,B/255] RGB = np.array(sRGB) XYZ = colour.sRGB_to_XYZ(sRGB) Lab = colour.XYZ_to_Lab(XYZ) LCHab = colour.Lab_to_LCHab(Lab) L = int(LCHab[0]);C = int(LCHab[1]);ab = int(LCHab[2]) print([L,C,ab]) if C<40:# 1 if L<10: return 'black' elif L>90: return 'white' else: return 'gray' elif ab<45:# 2 if L<30: return 'brown' elif L>70: return 'pink' else: return 'red' elif ab<75:# 3 if L<30: return 'brown' else: return 'orange' elif ab<105:# 4 return 'yellow' elif ab<210:# 5 return 'green' elif ab<315:# 6 return 'blue' elif ab>330:# 7 if L>50: return 'pink' else: return 'purple' else: return 'purple'
print(colour.XYZ_to_Lab(XYZ)) print('\n') Lab = (100.00000000, 28.97832184, 30.96902832) message_box(('Converting to "CIE XYZ" tristimulus values from given "CIE Lab" ' 'colourspace values:\n' '\n\t{0}'.format(Lab))) print(colour.Lab_to_XYZ(Lab)) print('\n') message_box(('Converting to "CIE LCHab" colourspace from given "CIE Lab" ' 'colourspace values:\n' '\n\t{0}'.format(Lab))) print(colour.Lab_to_LCHab(Lab)) print('\n') LCHab = (100.00000000, 42.41254357, 46.90195532) message_box(('Converting to "CIE Lab" colourspace from given "CIE LCHab" ' 'colourspace values:\n' '\n\t{0}'.format(LCHab))) print(colour.LCHab_to_Lab(LCHab)) print('\n') XYZ = (114.17634600, 100.00000000, 49.81520600) message_box(('Converting to "Hunter L,a,b" colour scale from given "CIE XYZ" ' 'tristimulus values:\n' '\n\t{0}'.format(XYZ)))
print(img) # 列印圖像的numpy數組,3緯數組 #%% # 網址1:https://colour.readthedocs.io/en/v0.3.13/generated/colour.sRGB_to_XYZ.html # 網址2:https://colour.readthedocs.io/en/v0.3.7/colour.html # 網址3:https://colour.readthedocs.io/en/v0.3.13/generated/colour.Lab_to_LCHab.html?highlight=lch#colour.Lab_to_LCHab # sRGB_to_XYZ #import colormath #from colormath.color_objects import LabColor, XYZColor #from colormath.color_conversions import convert_color import colour import numpy as np sRGB = [200 / 255, 150 / 255, 100 / 255] RGB = np.array(sRGB) XYZ = colour.sRGB_to_XYZ(sRGB) Lab = colour.XYZ_to_Lab(XYZ) LCHab = colour.Lab_to_LCHab(Lab) L = int(LCHab[0]) C = int(LCHab[1]) ab = int(LCHab[2]) #%% LCHab = ['41', '59', '27'] LCHab = np.array(LCHab) L = int(LCHab[0]) C = int(LCHab[1]) ab = int(LCHab[2]) #%% r = 255 g = 89 b = 0 sRGB = [r / 255, g / 255, b / 255] RGB = np.array(sRGB)
def compute(name, hue, lightness, saturation, output="scss"): """ Outputs different conversations of the given CIECAM02 input. Uses CIECAM with the same "signature" as HLC """ xyz = colour.CIECAM02_to_XYZ( lightness, saturation, hue, WHITEPOINT_D65_2, adapting_luminance, background_luminance, surround = surround, discount_illuminant = discount_illuminant ) xyz_trans = [ value / 100 for value in xyz ] #lab_65_10 = colour.XYZ_to_Lab(xyz_trans, illuminant=ILLUMINANT_D65_10) #lch_65_10 = colour.Lab_to_LCHab(lab_65_10) #hlc_65_10 = [lch_65_10[2], lch_65_10[0], lch_65_10[1]] lab_65_2 = colour.XYZ_to_Lab(xyz_trans, illuminant=ILLUMINANT_D65_2) lch_65_2 = colour.Lab_to_LCHab(lab_65_2) hlc_65_2 = [lch_65_2[2], lch_65_2[0], lch_65_2[1]] #lab_50_10 = colour.XYZ_to_Lab(xyz_trans, illuminant=ILLUMINANT_D50_10) #lch_50_10 = colour.Lab_to_LCHab(lab_50_10) #hlc_50_10 = [lch_50_10[2], lch_50_10[0], lch_50_10[1]] lab_50_2 = colour.XYZ_to_Lab(xyz_trans, illuminant=ILLUMINANT_D50_2) lch_50_2 = colour.Lab_to_LCHab(lab_50_2) hlc_50_2 = [lch_50_2[2], lch_50_2[0], lch_50_2[1]] srgb = colour.XYZ_to_sRGB(xyz_trans) validRGB = True for entry in srgb: if entry > 1: validRGB = False srgb_trans = [ value * 255 for value in srgb ] if validRGB else [0,0,0] hex_trans = rgb_to_hex(srgb) if validRGB else "EXCEEDS" if output == "text": print("NAME :", name) print("CAM-65 :", floatlist([hue, lightness, saturation])) print("XYZ :", floatlist(xyz)) print("LAB-50/2° :", floatlist(lab_50_2)) print("HLC-50/2° :", floatlist(hlc_50_2)) print("LAB-65/2° :", floatlist(lab_65_2)) print("HLC-65/2° :", floatlist(hlc_65_2)) print("sRGB :", floatlist(srgb_trans)) print("HEX :", hex_trans) print("") elif output == "scss": print("$%s: %s;" % (name, hex_trans)) elif output == "print": print("%s = %s" % (name, intlist(lab_50_2))) elif output == "affinity": print("%s = %s" % (name, intlist(lab_65_2)))