# Source code for tabletree.src.api.view_util.highlight

    from typing import Literal, Self, overload

    from ...lib.view_command import ConfigAccessor

    class ColorRGB:
        def __init__(self) -> None:
            self.__r: int | None = None
            self.__g: int | None = None
            self.__b: int | None = None

        def __clamp(self, value: int) -> int:
            return min(255, max(0, value))

        @property
        def r(self) -> int:
            """The red value."""
            return 0 if self.__r is None else self.__r

        @r.setter
        def r(self, r_value: int):
            self.__r = self.__clamp(r_value)

        @property
        def g(self) -> int:
            """The green value."""
            return 0 if self.__g is None else self.__g

        @g.setter
        def g(self, g_value: int):
            self.__g = self.__clamp(g_value)

        @property
        def b(self) -> int:
            """The blue value."""
            return 0 if self.__b is None else self.__b

        @b.setter
        def b(self, b_value: int):
            self.__b = self.__clamp(b_value)

    class HighlightColor(ColorRGB):
        HighlightColorPreset = Literal["info", "warn", "error"]
        _HighlightColorPresetDict: dict[HighlightColorPreset, int] = {
            "info": 1,
            "warn": 2,
            "error": 0,
        }
        _HighlightColorPresetToPriority: dict[HighlightColorPreset, int] = {
            "info": 10000,
            "warn": 20000,
            "error": 30000,
        }
        fallback_color: list[int] = [255, 255, 255]
        index: int

        def __init__(self) -> None:
            """Create a color for highlighting."""
            super().__init__()
            self.index = -1
            self.r = self.fallback_color[0]
            self.g = self.fallback_color[1]
            self.b = self.fallback_color[2]

        def set_color_index(self, index: int) -> Self:
            """Set a color with a config index.

            Args:
                index:
                    The color index in the config.
            """
            self.index = index
            return self

        def set_color_rgb(self, r: int, g: int, b: int) -> Self:
            """Set a color with rgb values.

            Args:
                r: `0 ≦ r ≦ 255`
                g: `0 ≦ g ≦ 255`
                b: `0 ≦ b ≦ 255`
            """
            self.r = r
            self.g = g
            self.b = b
            return self

        @property
        def rgb(self) -> list[int]:
            """Obtain the rgb values."""
            return [self.r, self.g, self.b]

        @property
        def config_accessor(self):
            """Obtain a config accessor, or rgb if color index is not set."""
            if self.index == -1:
                return self.rgb
            else:
                return ConfigAccessor(
                    {
                        "$config": f"colorMap[{self.index}]",
                        "default": self.fallback_color,
                    }
                )

        @staticmethod
        def preset_to_config_accessor(
            color: HighlightColorPreset,
        ) -> ConfigAccessor:
            """Obtain a config accessor of a preset color.

            Args:
                color:
                    The preset color.
            """
            return ConfigAccessor(
                {
                    "$config": "colorMap["
                    f"{HighlightColor._HighlightColorPresetDict[color]}"
                    "]",
                    "default": HighlightColor.fallback_color,
                }
            )

    class Highlight:
        @overload
        def __init__(  # noqa: D418
            self,
            key: str,
            color: HighlightColor.HighlightColorPreset,
            /,
        ) -> None:
            """Create a Highlight object.

            Args:
                key: The key for the geometries to be highlighted.
                color: The highlight colors of the geometries.
                The preset priorities are ::

                    "info": 10,000,
                    "warn": 20,000,
                    "error": 30,000.
            """
            ...

        @overload
        def __init__(  # noqa: D418
            self,
            key: str,
            color: HighlightColor,
            /,
            *,
            priority: int | None = None,
        ) -> None:
            """Create a Highlight object.

            Args:
                key: The key for the geometries to be highlighted.
                color: The highlight colors of the geometries.
                priority: The priority.
                    A Highlight with higher priority overrides others.
                    If the same value is set,
                    highlight colors are overridden
                    in the order geometries are highlighted.
                    The value must be less than 1,000,000.
            """
            ...

        def __init__(
            self,
            key: str,
            color: HighlightColor.HighlightColorPreset | HighlightColor,
            /,
            *,
            priority: int | None = None,
        ) -> None:
            """Create a Highlight object.

            Args:
                key: The key for the geometries to be highlighted.
                color: The highlight colors of the geometries.
                priority: The priority.
                    A Highlight with higher priority overrides others.
                    If the same value is set,
                    highlight colors are overridden
                    in the order geometries are highlighted.
                    The value must be less than 1,000,000.
                    The preset priorities are ::

                        "info": 10,000,
                        "warn": 20,000,
                        "error": 30,000.
            """
            self.key = key
            self.color = color
            self.priority = priority