Coding style.

- Move blend_color into termstr.
- Add invert method to termstr.
This commit is contained in:
Andrew Hamilton 2021-12-08 15:41:36 +10:00
parent 1ee92346b5
commit 2bb586c862
2 changed files with 19 additions and 12 deletions

View file

@ -26,6 +26,15 @@ def rgb_color(rgb, is_foreground):
return f"[{'38' if is_foreground else '48'};2;" + "%i;%i;%im" % rgb
def blend_color(a_color, b_color, transparency):
a_r, a_g, a_b = a_color
b_r, b_g, b_b = b_color
complement = 1 - transparency
return (int(a_r * transparency + b_r * complement),
int(a_g * transparency + b_g * complement),
int(a_b * transparency + b_b * complement))
class Color:
# https://en.wikipedia.org/wiki/Natural_Color_System
@ -38,6 +47,7 @@ class Color:
lime = (0, 255, 0)
yellow = (255, 211, 0)
grey_30 = (30, 30, 30)
grey_40 = (40, 40, 40)
grey_50 = (50, 50, 50)
grey_80 = (80, 80, 80)
grey_100 = (100, 100, 100)
@ -378,6 +388,12 @@ class TermStr(collections.UserString):
is_italic=style.is_italic, is_underlined=style.is_underlined)
return self.transform_style(set_bgcolor)
def invert(self):
def invert_(style):
return CharStyle(style.bg_color, style.fg_color, is_bold=style.is_bold,
is_italic=style.is_italic, is_underlined=style.is_underlined)
return self.transform_style(invert_)
def as_html(self):
result = []
styles = set()