replace color with Textual color

This commit is contained in:
Will McGugan
2022-03-29 20:42:47 +01:00
parent bea92f3f4d
commit aa16dbab12
9 changed files with 30 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
#uber1 { #uber1 {
layout: vertical; layout: vertical;
text: on dark_green;
text-background: dark_green;
overflow: hidden auto; overflow: hidden auto;
border: heavy white; border: heavy white;
} }

View File

@@ -7,6 +7,7 @@ import rich.repr
from rich.segment import Segment, SegmentLines from rich.segment import Segment, SegmentLines
from rich.style import Style, StyleType from rich.style import Style, StyleType
from .color import Color
from .css.types import EdgeStyle, EdgeType from .css.types import EdgeStyle, EdgeType
@@ -135,14 +136,18 @@ class Border:
from_color = Style.from_color from_color = Style.from_color
self._styles = ( self._styles = (
from_color(top_color), from_color(top_color.rich_color),
from_color(right_color), from_color(right_color.rich_color),
from_color(bottom_color), from_color(bottom_color.rich_color),
from_color(left_color), from_color(left_color.rich_color),
) )
self.inner_style = from_color(bgcolor=inner_color) self.inner_style = from_color(bgcolor=inner_color)
self.outer_style = from_color(bgcolor=outer_color) self.outer_style = from_color(bgcolor=outer_color)
def __rich_repr__(self) -> rich.repr.Result:
yield self.renderable
yield self.edge_styles
def _crop_renderable(self, lines: list[list[Segment]], width: int) -> None: def _crop_renderable(self, lines: list[list[Segment]], width: int) -> None:
"""Crops a renderable in place. """Crops a renderable in place.
@@ -256,10 +261,11 @@ class Border:
if __name__ == "__main__": if __name__ == "__main__":
from rich import print from rich import print
from rich.color import Color
from rich.text import Text from rich.text import Text
from rich.padding import Padding from rich.padding import Padding
from .color import Color
inner = Color.parse("#303F9F") inner = Color.parse("#303F9F")
outer = Color.parse("#212121") outer = Color.parse("#212121")

View File

@@ -532,6 +532,11 @@ class Color(NamedTuple):
b: int b: int
a: float = 1.0 a: float = 1.0
@classmethod
def from_rich_color(cls, rich_color: RichColor) -> Color:
r, g, b = rich_color.get_truecolor()
return cls(r, g, b)
@property @property
def rich_color(self) -> RichColor: def rich_color(self) -> RichColor:
r, g, b, _a = self r, g, b, _a = self
@@ -574,8 +579,7 @@ class Color(NamedTuple):
""" """
if color_text in ANSI_COLOR_NAMES: if color_text in ANSI_COLOR_NAMES:
color_number = ANSI_COLOR_NAMES[color_text] color_number = ANSI_COLOR_NAMES[color_text]
r, g, b = ANSI_COLORS[color_number] return cls(*ANSI_COLORS[color_number])
return cls(r, g, b)
color_match = RE_COLOR.match(color_text) color_match = RE_COLOR.match(color_text)
if color_match is None: if color_match is None:
raise ColorParseError(f"failed to parse {color_text!r} as a color") raise ColorParseError(f"failed to parse {color_text!r} as a color")

View File

@@ -319,8 +319,8 @@ class StyleProperty:
has_rule = obj.has_rule has_rule = obj.has_rule
style = Style.from_color( style = Style.from_color(
obj.text_color if has_rule("text_color") else None, obj.text_color.rich_color if has_rule("text_color") else None,
obj.text_background if has_rule("text_background") else None, obj.text_background.rich_color if has_rule("text_background") else None,
) )
if has_rule("text_style"): if has_rule("text_style"):
style += obj.text_style style += obj.text_style

View File

@@ -3,7 +3,6 @@ from __future__ import annotations
from typing import cast, Iterable, NoReturn from typing import cast, Iterable, NoReturn
import rich.repr import rich.repr
from rich.color import Color
from rich.style import Style from rich.style import Style
from ._error_tools import friendly_list from ._error_tools import friendly_list
@@ -22,6 +21,7 @@ from .styles import DockGroup, Styles
from .tokenize import Token from .tokenize import Token
from .transition import Transition from .transition import Transition
from .types import BoxSizing, Edge, Display, Overflow, Visibility from .types import BoxSizing, Edge, Display, Overflow, Visibility
from ..color import Color
from .._duration import _duration_as_seconds from .._duration import _duration_as_seconds
from .._easing import EASING from .._easing import EASING
from ..geometry import Spacing, SpacingDimensions, clamp from ..geometry import Spacing, SpacingDimensions, clamp
@@ -315,7 +315,7 @@ class StylesBuilder:
def _parse_border(self, name: str, tokens: list[Token]) -> tuple[str, Color]: def _parse_border(self, name: str, tokens: list[Token]) -> tuple[str, Color]:
border_type = "solid" border_type = "solid"
border_color = Color.default() border_color = Color(0, 255, 0)
for token in tokens: for token in tokens:
token_name, value, _, _, _, _ = token token_name, value, _, _, _, _ = token

View File

@@ -566,7 +566,7 @@ class Styles(StylesBase):
if top == right and right == bottom and bottom == left: if top == right and right == bottom and bottom == left:
border_type, border_color = rules[f"{name}_top"] border_type, border_color = rules[f"{name}_top"]
yield name, f"{border_type} {border_color.name}" yield name, f"{border_type} {border_color.hex}"
return return
# Check for edges # Check for edges

View File

@@ -10,8 +10,8 @@ COMMENT_START = r"\/\*"
SCALAR = r"\-?\d+\.?\d*(?:fr|%|w|h|vw|vh)" SCALAR = r"\-?\d+\.?\d*(?:fr|%|w|h|vw|vh)"
DURATION = r"\d+\.?\d*(?:ms|s)" DURATION = r"\d+\.?\d*(?:ms|s)"
NUMBER = r"\-?\d+\.?\d*" NUMBER = r"\-?\d+\.?\d*"
COLOR = r"\#[0-9a-fA-F]{6}|color\([0-9]{1,3}\)|rgb\(\d{1,3}\,\s?\d{1,3}\,\s?\d{1,3}\)" # COLOR = r"\#[0-9a-fA-F]{6}|color\([0-9]{1,3}\)|rgb\(\d{1,3}\,\s?\d{1,3}\,\s?\d{1,3}\)"
# COLOR = r"\#([0-9a-fA-F]{8})|\#([0-9a-fA-F]{6})|rgb\((\-?\d+\.?\d*,\-?\d+\.?\d*,\-?\d+\.?\d*)\)|rgba\((\-?\d+\.?\d*,\-?\d+\.?\d*,\-?\d+\.?\d*,\-?\d+\.?\d*)\)" COLOR = r"\#[0-9a-fA-F]{8}|\#[0-9a-fA-F]{6}|rgb\(\-?\d+\.?\d*,\-?\d+\.?\d*,\-?\d+\.?\d*\)|rgba\(\-?\d+\.?\d*,\-?\d+\.?\d*,\-?\d+\.?\d*,\-?\d+\.?\d*\)"
KEY_VALUE = r"[a-zA-Z_-][a-zA-Z0-9_-]*=[0-9a-zA-Z_\-\/]+" KEY_VALUE = r"[a-zA-Z_-][a-zA-Z0-9_-]*=[0-9a-zA-Z_\-\/]+"
TOKEN = "[a-zA-Z_-]+" TOKEN = "[a-zA-Z_-]+"
STRING = r"\".*?\"" STRING = r"\".*?\""
@@ -158,8 +158,8 @@ tokenize_declarations = DeclarationTokenizerState()
if __name__ == "__main__": if __name__ == "__main__":
css = """#something { css = """#something {
text: on red;
offset-x: 10; color: rgb(10,12,23)
} }
""" """
# transition: offset 500 in_out_cubic; # transition: offset 500 in_out_cubic;

View File

@@ -3,8 +3,7 @@ from __future__ import annotations
import sys import sys
from typing import Tuple from typing import Tuple
from ..color import Color
from rich.color import Color
if sys.version_info >= (3, 8): if sys.version_info >= (3, 8):
from typing import Literal from typing import Literal

View File

@@ -403,7 +403,7 @@ class Widget(DOMNode):
outer_color=parent_text_style.bgcolor, outer_color=parent_text_style.bgcolor,
) )
if styles.opacity: if styles.opacity != 1.0:
renderable = Opacity(renderable, opacity=styles.opacity) renderable = Opacity(renderable, opacity=styles.opacity)
return renderable return renderable