def parse_color(color): r"""Turns a color into an (r, g, b) tuple >>> parse_color('white') (255, 255, 255) >>> parse_color('#ff0000') (255, 0, 0) >>> parse_color('#f00') (255, 0, 0) >>> parse_color((255, 0, 0)) (255, 0, 0) >>> from fabulous import grapefruit >>> parse_color(grapefruit.Color((0.0, 1.0, 0.0))) (0, 255, 0) """ if isinstance(color, basestring): color = grapefruit.Color.NewFromHtml(color) if isinstance(color, int): (r, g, b) = xterm256.xterm_to_rgb(color) elif hasattr(color, 'rgb'): (r, g, b) = [int(c * 255.0) for c in color.rgb] else: (r, g, b) = color assert isinstance(r, int) and 0 <= r <= 255 assert isinstance(g, int) and 0 <= g <= 255 assert isinstance(b, int) and 0 <= b <= 255 return (r, g, b)
def parse_color(color): r"""Turns a color into an (r, g, b) tuple >>> parse_color('white') (255, 255, 255) >>> parse_color('#ff0000') (255, 0, 0) >>> parse_color('#f00') (255, 0, 0) >>> parse_color((255, 0, 0)) (255, 0, 0) >>> import grapefruit >>> parse_color(grapefruit.Color((0.0, 1.0, 0.0))) (0, 255, 0) """ if isinstance(color, basestring): color = grapefruit.Color.NewFromHtml(color) if isinstance(color, int): (r, g, b) = xterm256.xterm_to_rgb(color) elif hasattr(color, 'rgb'): (r, g, b) = [int(c * 255.0) for c in color.rgb] else: (r, g, b) = color assert isinstance(r, int) and 0 <= r <= 255 assert isinstance(g, int) and 0 <= g <= 255 assert isinstance(b, int) and 0 <= b <= 255 return (r, g, b)
def f(xc): s = highlight256(xc, "color %03d" % (xc)) rgb = xterm256.xterm_to_rgb(xc) rgbs = ' (%3d, %3d, %3d)' % rgb if rgb[0] == rgb[1] == rgb[2]: s += bold(rgbs) else: s += rgbs s += ' (%08d, %08d, %08d)' % tuple([int(bin(n)[2:]) for n in rgb]) return s
def get_color(color): if color.startswith('bold'): color = color[5:] if color.startswith('bright'): color = color[7:] if color.startswith('underline'): color = color[10:] if color.startswith('color'): number = int(color[3:]) r, g, b = xterm_to_rgb(number) return hex_rgb(r, g, b) if color.startswith('rgb'): r_, g_, b_ = color[3:] r, g, b = int(r_) * 40 + 55, int(g_) * 40 + 55, int(b_) * 40 + 55 return hex_rgb(r, g, b) if color.startswith('gray'): level = int(color[4:]) r, g, b = [255 - (level * (256.0 / 23))] * 3 return hex_rgb(r, g, b) try: return webcolors.name_to_hex(color) except: pass raise ValueError('No known color for %s' % color)