def parse(style_string): for s in style_string.split(): if s.startswith("#"): ix = s[1:] if len(s) == 7 else "".join(_ * 2 for _ in s[1:]) c = x256.to_rgb(x256.from_hex(ix)) return "\"#{:02x}{:02x}{:02x}\"".format(*c) return "nil"
def load_style(name): style = pygments.styles.get_style_by_name(name) bg = parse(style.background_color) fg = parse(style.styles[Text]) if fg == "nil": brightness = sum(x256.to_rgb(x256.from_hex(bg[2:-1]))) / 3 fg = "\"#000000\"" if brightness >= 128 else "\"#FFFFFF\"" style_dict = dict((str(k), parse(v)) for k, v in style.styles.items()) return bg, fg, translate(style_dict)
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 = 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:]) red, green, blue = [level * (255.0 / 23)] * 3 return hex_rgb(red, green, blue) try: return webcolors.name_to_hex(color) except: pass raise ValueError("No known color for %s" % color)
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 = 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:]) red, green, blue = [level * (255.0 / 23)] * 3 return hex_rgb(red, green, blue) try: return webcolors.name_to_hex(color) except: pass raise ValueError('No known color for %s' % color)
def test_to_rgb(self): color = x256.to_rgb(self.xcolor) self.assertEqual(self.aprox_rgb, color)
def to_rgb(s): return tuple(x256.to_rgb(int(s)))