def test_to_husl_gray_3d(): img = _img() img[..., 1] = img[..., 0] # makes things gray img[..., 2] = img[..., 0] # makes things gray img_float = transform.ensure_rgb_float(img) husl_new = nphusl.to_husl(img) was_wrong = False for row in range(img.shape[0]): for col in range(img.shape[1]): husl_old = husl.rgb_to_husl(*img_float[row, col]) a = husl.husl_to_rgb(*husl_old) b = husl.husl_to_rgb(*husl_new[row, col]) a = np.asarray(a) b = np.asarray(b) i = row * img.shape[1] * 3 + col * 3 _diff_husl(husl_new[row, col], husl_old)
def husl_palette(n_colors=6, h=.01, s=.9, l=.65): """Get a set of evenly spaced colors in HUSL hue space. h, s, and l should be between 0 and 1 Parameters ---------- n_colors : int number of colors in the palette h : float first hue s : float saturation l : float lightness Returns ------- palette : list of tuples color palette """ hues = np.linspace(0, 1, n_colors + 1)[:-1] hues += h hues %= 1 hues *= 359 s *= 99 l *= 99 palette = [husl.husl_to_rgb(h_i, s, l) for h_i in hues] return palette
def husl_palette(n_colors=6, h=.01, s=.9, l=.65): """Get a set of evenly spaced colors in HUSL hue space. h, s, and l should be between 0 and 1 Parameters ---------- n_colors : int number of colors in the palette h : float first hue s : float saturation l : float lightness Returns ------- palette : list of tuples color palette """ hues = np.linspace(h, 1 + h, n_colors, endpoint=False) hues = 359 * (hues % 1) s *= 99 l *= 99 return [rgb_to_hex(husl.husl_to_rgb(h_i, s, l)) for h_i in hues]
def rgb (self) : _vmap = self._vmap result = _vmap.get ("rgb") if result is None : v = self.preferred_value if husl is not None and isinstance (v, HUSL_Value) : h, s, l = v result = RGB_Value \ (* husl.husl_to_rgb (float (h), s * 100., l * 100.)) elif isinstance (v, HSL_Value) : h, s, l = _vmap ["hsl"] c = (1.0 - abs (2.0 * l - 1.0)) * s h6 = h / 60.0 x = c * (1 - abs (h6 % 2 - 1)) m = l - 0.5 * c if h6 < 1 : r, g, b = c, x, 0 elif h6 < 2 : r, g, b = x, c, 0 elif h6 < 3 : r, g, b = 0, c, x elif h6 < 4 : r, g, b = 0, x, c elif h6 < 5 : r, g, b = x, 0, c elif h6 < 6 : r, g, b = c, 0, x else : raise ValueError ("Invalid hue: %s" % h) result = RGB_Value (r + m, g + m, b + m) _vmap ["rgb"] = result return result
def rgb(self): _vmap = self._vmap result = _vmap.get("rgb") if result is None: v = self.preferred_value if husl is not None and isinstance(v, HUSL_Value): h, s, l = v result = RGB_Value \ (* husl.husl_to_rgb (float (h), s * 100., l * 100.)) elif isinstance(v, HSL_Value): h, s, l = _vmap["hsl"] c = (1.0 - abs(2.0 * l - 1.0)) * s h6 = h / 60.0 x = c * (1 - abs(h6 % 2 - 1)) m = l - 0.5 * c if h6 < 1: r, g, b = c, x, 0 elif h6 < 2: r, g, b = x, c, 0 elif h6 < 3: r, g, b = 0, c, x elif h6 < 4: r, g, b = 0, x, c elif h6 < 5: r, g, b = x, 0, c elif h6 < 6: r, g, b = c, 0, x else: raise ValueError("Invalid hue: %s" % h) result = RGB_Value(r + m, g + m, b + m) _vmap["rgb"] = result return result
def make_phase_image(amp, phase, normalize=True, saturate=True, threshold=True): """ Turns a phase matrix into an image to be plotted with imshow. """ nelectrodes,d = amp.shape alpha = copy.deepcopy(amp) if normalize: max_amp = np.percentile(amp, 98) alpha = alpha / max_amp img = np.zeros([nelectrodes, d, 4], dtype='float32') #set the alpha and color for the bins if saturate: alpha[alpha > 1.0] = 1.0 #saturate if threshold: alpha[alpha < 0.05] = 0.0 #nonlinear threshold cnorm = ((180.0 / np.pi) * phase).astype('int') for j in range(nelectrodes): for ti in range(d): #img[j, ti, :3] = husl.husl_to_rgb(cnorm[j, ti], 99.0, 50.0) #use HUSL color space: https://github.com/boronine/pyhusl/tree/v2.1.0 img[j, ti, :3] = husl.husl_to_rgb(cnorm[j, ti], 99.0, 61.0) #use HUSL color space: https://github.com/boronine/pyhusl/tree/v2.1.0 img[:, :, 3] = alpha return img
def color_changed(foo): c = cs.get_current_color() x = husl_to_rgb(c.hue * 360, c.saturation * 100, c.value * 100) print(c.hue, c.saturation, c.value, x) data = cal.get_rgb(c.red / 256.0, c.green / 256.0, c.blue / 256.0) data = cal.get_rgb(x[0] * 255, x[1] * 255, x[2] * 255) data = [data[1], data[0], data[2]] setleds(data * int(sys.argv[2]))
def test_cython_husl_to_rgb(): from nphusl import _nphusl_cython as cy hsl = np.ndarray(dtype=float, shape=(9, 9, 3)) hsl[:] = 200.0, 50.1, 30.4 hsl[:] /= 255.0 rgb = cy.husl_to_rgb(hsl) rgb_std = husl.husl_to_rgb(*(200.0/255, 50.1/255, 30.4/255)) assert _diff(rgb, rgb_std, 0.1)
def colorize_border(path, name): """ Colorize terminal tab by your server name. """ H, S, L = get_color(path, name) color = husl.husl_to_rgb(H, S, L) decorate_terminal(color)
def test_within_rgb_range(self): for H in range(0, 361, 5): for S in range(0, 101, 5): for L in range(0, 101, 5): RGB = husl.husl_to_rgb(H, S, L) for channel in RGB: assert (channel >= -rgb_range_tolerance and channel <= 1 + rgb_range_tolerance), ((H, S, L), RGB) RGB = husl.huslp_to_rgb(H, S, L) for channel in RGB: assert (channel >= -rgb_range_tolerance and channel <= 1 + rgb_range_tolerance), ((H, S, L), RGB)
def register_husl_colormap(nsegs=90): cdict = {'red':list(), 'green':list(), 'blue':list()} r0,g0,b0 = husl.husl_to_rgb(0, 99.0, 50) cdict['red'].append((0., r0, r0)) cdict['blue'].append((0., b0, b0)) cdict['green'].append((0., g0, g0)) inc = 360. / nsegs for k in range(nsegs): d = (k+1)*inc x = (k+1) / float(nsegs) r,g,b = husl.husl_to_rgb(d, 99.0, 50.0) cdict['red'].append((x, r, r)) cdict['green'].append((x, g, g)) cdict['blue'].append((x, b, b)) cm = LinearSegmentedColormap('HUSL', cdict) plt.register_cmap(cmap=cm)
def husl_colormap(saturation, lightness): """ Generate a colormap of linearly varying hue while keeping saturation and lightness fixed. See here for the HUSL color space: http://www.boronine.com/husl/ """ hvals = np.linspace(0, 360, 256, endpoint=False) cmap_vals = np.array( [husl.husl_to_rgb(h, saturation, lightness) for h in hvals]) cmap = mcolors.ListedColormap(cmap_vals) return cmap
def parseColor(color): parsed_color = None if color.startswith('#'): if re.match('^#[0-9a-f]{6}$', color) != None: parsed_color = color elif color.startswith('rgb'): rgb_match = re.search( '^rgb\(([0-9]{1,3}),\s*([0-9]{1,3}),\s*([0-9]{1,3})\)$', color) if rgb_match is not None: try: r = min(int(rgb_match.group(1)), 255) g = min(int(rgb_match.group(2)), 255) b = min(int(rgb_match.group(3)), 255) parsed_color = '#' + ("%x" % r) + ("%x" % g) + ("%x" % b) except ValueError: pass elif color.startswith('hsl'): hsl_match = re.search( '^hsl\(([0-9]{1,3}(\.[0-9]*)?),\s*([0-9]{1,3}(\.[0-9]*)?)\%,\s*([0-9]{1,3}(\.[0-9]*)?)\%\)$', color) if hsl_match is not None: try: h = min(float(hsl_match.group(1)) / 365, 1) s = min(float(hsl_match.group(3)) / 100, 1) l = min(float(hsl_match.group(5)) / 100, 1) (r, g, b) = colorsys.hls_to_rgb(h, l, s) parsed_color = '#' + ("%x" % (r * 255)) + ("%x" % (g * 255)) + ("%x" % (b * 255)) except ValueError: pass elif color.startswith('husl'): husl_match = re.search( '^husl\(([0-9]{1,3}(\.[0-9]*)?),\s*([0-9]{1,3}(\.[0-9]*)?)\%,\s*([0-9]{1,3}(\.[0-9]*)?)\%\)$', color) if husl_match is not None: try: import husl h = min(float(husl_match.group(1)), 360) s = min(float(husl_match.group(3)), 100) l = min(float(husl_match.group(5)), 100) rgb = husl.husl_to_rgb(h, s, l) parsed_color = husl.rgb_to_hex(rgb) except ValueError: pass except ImportError: print( 'HUSL colour definitions need the husl library. Please install it.' ) pass return parsed_color
def parseColor(color): parsed_color = None if color.startswith('#'): if re.match('^#[0-9a-f]{6}$', color) != None: parsed_color = color elif color.startswith('rgb'): rgb_match = re.search('^rgb\(([0-9]{1,3}),\s*([0-9]{1,3}),\s*([0-9]{1,3})\)$', color) if rgb_match is not None: try: r = min(int(rgb_match.group(1)), 255) g = min(int(rgb_match.group(2)), 255) b = min(int(rgb_match.group(3)), 255) parsed_color = '#'+("%x" % r)+("%x" % g)+("%x" % b) except ValueError: pass elif color.startswith('hsl'): hsl_match = re.search('^hsl\(([0-9]{1,3}(\.[0-9]*)?),\s*([0-9]{1,3}(\.[0-9]*)?)\%,\s*([0-9]{1,3}(\.[0-9]*)?)\%\)$', color) if hsl_match is not None: try: h = min(float(hsl_match.group(1)) / 365, 1) s = min(float(hsl_match.group(3)) / 100, 1) l = min(float(hsl_match.group(5)) / 100, 1) (r, g, b) = colorsys.hls_to_rgb(h, l, s) parsed_color = '#'+("%x" % (r * 255))+("%x" % (g * 255))+("%x" % (b * 255)) except ValueError: pass elif color.startswith('husl'): husl_match = re.search('^husl\(([0-9]{1,3}(\.[0-9]*)?),\s*([0-9]{1,3}(\.[0-9]*)?)\%,\s*([0-9]{1,3}(\.[0-9]*)?)\%\)$', color) if husl_match is not None: try: import husl h = min(float(husl_match.group(1)), 360) s = min(float(husl_match.group(3)), 100) l = min(float(husl_match.group(5)), 100) rgb = husl.husl_to_rgb(h, s, l) parsed_color = husl.rgb_to_hex(rgb) except ValueError: pass except ImportError: print('HUSL colour definitions need the husl library. Please install it.') pass return parsed_color
def draw_husl_circle(): """ Draw an awesome circle whose angle is colored using the HUSL color space. The HUSL color space is circular, so it's useful for plotting phase. This figure could serve as a "color circle" as opposed to a color bar. """ #generate a bunch of points on the circle theta = np.arange(0.0, 2*np.pi, 1e-3) plt.figure() radii = np.arange(0.75, 1.0, 1e-2) for t in theta: x = radii*np.cos(t) y = radii*np.sin(t) a = (180.0/np.pi)*t c = husl.husl_to_rgb(a, 99.0, 50.0) plt.plot(x, y, c=c) plt.xlim(-1.25, 1.25) plt.ylim(-1.25, 1.25) plt.show()
def getColor(cls): cls.seed = (cls.seed + golden_ratio_conjugate) % 1 hue = 360 * cls.seed sat = random.uniform(cls.minS, cls.maxS) lum = random.uniform(cls.minL, cls.maxL) return husl_to_rgb(hue, sat, lum)
def husl_to_rgba(hue,saturation,lightness): return argb(255,*[255*x for x in husl.husl_to_rgb(hue,saturation,lightness)])
p1.enableAutoRange('xy', False) p1.setXRange(0.0, 100000.0, padding=0) p1.setYRange(0.4, 1.0, padding=0) p1.setLabels(bottom='Iterations') p1.showGrid(x=True, y=True) inputFiles = sys.argv[1:] def rgb(r,g,b): return r*255.0, g*255.0, b*255.0 # Setup plots plots = {} for idx, file in enumerate(inputFiles): hue = idx * 360.0 / len(inputFiles) color_map = husl.husl_to_rgb(hue, 80.0, 40.0) color_f1 = husl.husl_to_rgb(hue, 40.0, 60.0) text_map = pg.TextItem(anchor=(0,0.5)) text_f1 = pg.TextItem(anchor=(0,0.5)) p1.addItem(text_map) p1.addItem(text_f1) plots[file] = { 'test_f1': p1.plot(pen=rgb(*color_f1), name='%s F1' % file), 'test_map': p1.plot(pen=rgb(*color_map), name='%s mAP' % file), 'test_f1_text': text_f1, 'test_f1_color': rgb(*color_f1), 'test_map_text': text_map, 'test_map_color': rgb(*color_map),