def fromrgb(rgb): """ http://www.rapidtables.com/convert/color/rgb-to-hsv.htm """ R = rgb[0] / 255 # norm to [0,1] G = rgb[1] / 255 # norm to [0,1] B = rgb[2] / 255 # norm to [0,1] Imax = max_index(R, G, B) Cmax = max(R, G, B) Cmin = min(R, G, B) delta = Cmax - Cmin if delta == 0: H = 0 else: H = { 0: 60 * ((G - B) / delta % 6), # Max R 1: 60 * ((B - R) / delta + 2), # Max G 2: 60 * ((R - G) / delta + 4), # Max B }[Imax] V = Cmax S = {True: 0, False: delta / Cmax if Cmax != 0 else 1}[Cmax == 0] return [round(H), round(S * 100), round(V * 100)]
def fromrgb(rgb): """ http://www.rapidtables.com/convert/color/rgb-to-hsl.htm """ R = rgb[0] / 255 # norm to [0,1] G = rgb[1] / 255 # norm to [0,1] B = rgb[2] / 255 # norm to [0,1] Imax = max_index(R, G, B) Cmax = max(R, G, B) Cmin = min(R, G, B) delta = Cmax - Cmin if delta == 0: H = 0 else: H = { 0: 60 * ((G - B) / delta % 6), # Max R 1: 60 * ((B - R) / delta + 2), # Max G 2: 60 * ((R - G) / delta + 4), # Max B }[Imax] L = (Cmax + Cmin) / 2 S = {True: 0, False: delta / (1 - abs(2 * L - 1)) if L != 1 and L != 0 else delta}[delta == 0] return [round(H), round(S * 100), round(L * 100)]
def circle(self, force=False): # To calculate the circle based on the vectorial space of RGB. # TODO: Add reference to paper about this if self._circle is not None and not force: return self._circle base = '' # copy of the rgb rgb = self() # create a dict to contain all the colors in the circle circle = { k : [ 0 for i in range(3) ] for k in [ a + str(b) for b in (1,2) for a in _rgb_str ] + list(_cmy_str) } # normalize the rgb values from 0 - 255, to 0 - 1 percents = [ i/255 for i in rgb ] # The first thing, is to verify, which type of color is this # get the index of the dominant color dominant = vector.max_index(rgb) # REDS if dominant == 0: # Meaning this color is primarily red # YELLOW # If the green component is within the range 10% of the red, # from the left is a yellow if _in_range( percents[1], percents[0]-0.1, percents[0] ): base = 'Y' # MAGENTA # If the blue component is within the range 10% of the red, # from the left is a magenta elif _in_range( percents[2], percents[0]-0.1, percents[0] ): base = 'M' # RED else: base = 'R' # GREENS elif dominant == 1: # Meaning this color is primarily green # YELLOW # If the red component is within the range 10% of the green, # from the left is a yellow if _in_range( percents[0], percents[1]-0.1, percents[1] ): base = 'Y' # CYAN # If the blue component is within the range 10% of the green, # from the left is a cyan elif _in_range( percents[2], percents[1]-0.1, percents[1] ): base = 'C' # GREEN else: base = 'G' # BLUES elif dominant == 2: # Meaning this color is primarily blue # MAGENTA # If th red component is within the range 10% of the blue, # from the left is a magenta if _in_range( percents[0], percents[2]-0.1, percents[2] ): base = 'M' # CYAN # If the green component is within the range 10% of the blue, # from the left is a cyan elif _in_range( percents[1], percents[2]-0.1, percents[2] ): base = 'C' else: base = 'B' self._identity = base if base in _rgb_str: if base == 'R': val = _add_color( 'R', circle, rgb ) green = _rotate_color('R', 'G', rgb) _add_color( 'G', circle, green ) blue = _rotate_color('R', 'B', rgb) _add_color( 'B', circle, blue ) elif base == 'G': val = _add_color( 'G', circle, rgb ) red = _rotate_color('G', 'R', rgb) _add_color( 'R', circle, red ) blue = _rotate_color('G', 'B', rgb) _add_color( 'B', circle, blue ) elif base =='B': val = _add_color( 'B', circle, rgb ) green = _rotate_color('B', 'G', rgb) _add_color( 'G', circle, green ) red = _rotate_color('B', 'R', rgb) _add_color( 'R', circle, red ) self._identity += str(val) yellow = _get_color('Y', circle, val) _add_color( 'Y', circle, yellow ) cyan = _get_color('C', circle, val) _add_color( 'C', circle, cyan ) magenta = _get_color('M', circle, val) _add_color( 'M', circle, magenta ) elif base in _cmy_str: if base == 'C': _add_color( 'C', circle, rgb ) yellow = _rotate_color('C', 'Y', rgb) _add_color( 'Y', circle, yellow ) magenta = _rotate_color('C', 'M', rgb) _add_color( 'M', circle, magenta ) elif base == 'Y': _add_color( 'Y', circle, rgb ) cyan = _rotate_color('Y', 'C', rgb) _add_color( 'C', circle, cyan ) magenta = _rotate_color('Y', 'M', rgb) _add_color( 'M', circle, magenta ) elif base == 'M': _add_color( 'M', circle, rgb ) cyan = _rotate_color('M', 'C', rgb) _add_color( 'C', circle, cyan ) yellow = _rotate_color('M', 'Y', rgb) _add_color( 'Y', circle, yellow ) red = _get_color('R', circle) _add_color( 'R', circle, red ) green = _get_color('G', circle) _add_color( 'G', circle, green ) blue = _get_color('B', circle) _add_color( 'B', circle, blue ) result = [] result.append(circle['R1']) result.append(circle['Y']) result.append(circle['G1']) if circle['G1'] != circle['G2']: result.append(circle['G2']) result.append(circle['C']) result.append(circle['B2']) if circle['B1'] != circle['B2']: result.append(circle['B1']) result.append(circle['M']) if circle['R1'] != circle['R2']: result.append(circle['R2']) self._circle = result return result