termstr: Coding style

- Add rgb color properties to char styles.
This commit is contained in:
Andrew Hamilton 2022-04-30 13:35:55 +10:00
parent 21a9ec2826
commit 960cc57559
2 changed files with 13 additions and 11 deletions

View file

@ -165,14 +165,10 @@ def highlight_str(line, highlight_color, transparency):
@functools.lru_cache(maxsize=500) @functools.lru_cache(maxsize=500)
def blend_style(style): def blend_style(style):
fg_color = (style.fg_color if type(style.fg_color) == tuple
else termstr.XTERM_COLORS[style.fg_color])
bg_color = (style.bg_color if type(style.bg_color) == tuple
else termstr.XTERM_COLORS[style.bg_color])
return termstr.CharStyle( return termstr.CharStyle(
termstr.blend_color(fg_color, highlight_color, transparency), termstr.blend_color(style.fg_rgb_color, highlight_color, transparency),
termstr.blend_color(bg_color, highlight_color, transparency), is_bold=style.is_bold, termstr.blend_color(style.bg_rgb_color, highlight_color, transparency),
is_italic=style.is_italic, is_underlined=style.is_underlined) is_bold=style.is_bold, is_italic=style.is_italic, is_underlined=style.is_underlined)
return termstr.TermStr(line).transform_style(blend_style) return termstr.TermStr(line).transform_style(blend_style)

View file

@ -147,14 +147,20 @@ class CharStyle:
return "".join([ESC, NORMAL, fg_termcode, bg_termcode, bold_code, return "".join([ESC, NORMAL, fg_termcode, bg_termcode, bold_code,
italic_code, underline_code]) italic_code, underline_code])
@functools.cached_property
def fg_rgb_color(self):
return self.fg_color if type(self.fg_color) == tuple else XTERM_COLORS[self.fg_color]
@functools.cached_property
def bg_rgb_color(self):
return self.bg_color if type(self.bg_color) == tuple else XTERM_COLORS[self.bg_color]
def as_html(self): def as_html(self):
bold_code = "font-weight:bold; " if self.is_bold else "" bold_code = "font-weight:bold; " if self.is_bold else ""
italic_code = "font-style:italic; " if self.is_italic else "" italic_code = "font-style:italic; " if self.is_italic else ""
underline_code = "text-decoration:underline; " if self.is_underlined else "" underline_code = "text-decoration:underline; " if self.is_underlined else ""
fg_color = self.fg_color if type(self.fg_color) == tuple else XTERM_COLORS[self.fg_color] return (f"<style>.S{id(self)} {{font-size:80%%; color:rgb{self.fg_rgb_color!r};"
bg_color = self.bg_color if type(self.bg_color) == tuple else XTERM_COLORS[self.bg_color] f" background-color:rgb{self.bg_rgb_color!r}; "
return (f"<style>.S{id(self)} {{font-size:80%%; color:rgb{fg_color!r};"
f" background-color:rgb{bg_color!r}; "
f"{bold_code}{italic_code}{underline_code}}}</style>") f"{bold_code}{italic_code}{underline_code}}}</style>")