Source code for picogl.core.rgbcolor

"""
RGB Color

Simple, readable structure using dataclasses.
Default values give you a convenient “white opaque” color (1.0, 1.0, 1.0, 1.0).
RGBA correctly inherits from RGB, reusing its fields.


Type hints and value constraints**: Ensure color components are in the [0.0, 1.0] range and handle possible out-of-range values.
  You could add a post-init check to enforce bounds.
Immutability (optional)**: If colors shouldn’t change after creation, consider making the dataclasses frozen.
Validation utility**: Provide a small helper to clamp values or validate inputs.
String representation**: Customize repr for clearer debugging, especially for RGBA.
Interoperability**: If you plan to convert to/from common formats (tuples, lists, hex), consider adding convenience methods.

"""

from __future__ import annotations

from dataclasses import dataclass
from typing import ClassVar, Tuple

from picogl.core.mixin.vec3 import Vec3Mixin, clamp01
from picogl.core.mixin.vec4 import Vec4Mixin
from typing_extensions import Self


@dataclass(frozen=True)
[docs] class RGBColor(Vec3Mixin): """RGB color"""
[docs] r: float = 1.0
[docs] g: float = 1.0
[docs] b: float = 1.0
[docs] WHITE: ClassVar["Self"]
[docs] BLACK: ClassVar["Self"]
[docs] RED: ClassVar["Self"]
[docs] GREEN: ClassVar["Self"]
[docs] BLUE: ClassVar["Self"]
[docs] BLUE_ISOSURFACE: ClassVar["Self"]
[docs] YELLOW: ClassVar["Self"]
[docs] MAGENTA: ClassVar["Self"]
[docs] CYAN: ClassVar["Self"]
[docs] GREY: ClassVar["Self"]
[docs] def __post_init__(self): # Enforce constraints on init object.__setattr__(self, "r", clamp01(self.r)) object.__setattr__(self, "g", clamp01(self.g)) object.__setattr__(self, "b", clamp01(self.b))
[docs] def to_tuple(self) -> Tuple[float, float, float]: return self.r, self.g, self.b
@property
[docs] def rgb(self) -> Tuple[float, float, float]: return self.r, self.g, self.b
@property
[docs] def alpha(self) -> float: return 1.0
@classmethod
[docs] def from_tuple(cls, rgb: Tuple[float, float, float]) -> RGBColor: return cls(*rgb)
@classmethod
[docs] def from_rgba_tuple(cls, t: Tuple[float, float, float, float]): r, g, b, _a = t return cls(r, g, b)
[docs] def with_alpha(self, alpha: float) -> "RGBAColor": return RGBAColor(self.r, self.g, self.b, alpha)
[docs] def scaled(self, factor: float) -> Self: return type(self)( self.r * factor, self.g * factor, self.b * factor, )
# RGBAColor @dataclass(frozen=True)
[docs] class RGBAColor(RGBColor, Vec4Mixin): """RGBA color"""
[docs] a: float = 1.0
[docs] COMPONENTS: ClassVar[int] = 4
[docs] def __post_init__(self): super().__post_init__() object.__setattr__(self, "a", clamp01(self.a))
[docs] def to_tuple(self) -> Tuple[float, float, float, float]: return self.r, self.g, self.b, self.a
@property
[docs] def tuple(self) -> Tuple[float, float, float, float]: return self.to_tuple()
[docs] def without_alpha(self) -> RGBColor: return RGBColor(self.r, self.g, self.b)
[docs] def scaled(self, factor: float) -> Self: return type(self)( self.r * factor, self.g * factor, self.b * factor, self.a, )
[docs] _COLOR_VALUES = { "WHITE": (1.0, 1.0, 1.0), "BLACK": (0.0, 0.0, 0.0), "RED": (1.0, 0.0, 0.0), "GREEN": (0.0, 1.0, 0.0), "BLUE": (0.0, 0.0, 1.0), "BLUE_ISOSURFACE": (0.0, 0.0, 0.8), "YELLOW": (1.0, 1.0, 0.0), "MAGENTA": (1.0, 0.0, 1.0), "CYAN": (0.0, 1.0, 1.0), "TEAPOT_RED": (0.8, 0.2, 0.2), "GREY": (0.5, 0.5, 0.5), "LIGHT_GREY": (0.8, 0.8, 0.8), "CONNOLLY_SURFACE": (0.2, 0.6, 1.0) }
for name, rgb in _COLOR_VALUES.items(): setattr(RGBColor, name, RGBColor(*rgb)) setattr(RGBAColor, name, RGBAColor(*rgb, 1.0))
[docs] RGB = tuple[float, float, float]
[docs] RGBA = tuple[float, float, float, float]
[docs] class RGBTuple: """Named RGB triples in ``[0, 1]``."""
[docs] CONNOLLY_SURFACE: ClassVar[RGB]
[docs] WHITE: ClassVar[RGB]
[docs] GREY: ClassVar[RGB]
[docs] LIGHT_GREY: ClassVar[RGB]
WHITE: ClassVar[RGB]
[docs] BLACK: ClassVar[RGB]
[docs] RED: ClassVar[RGB]
[docs] TEAPOT_RED: ClassVar[RGB]
[docs] GREEN: ClassVar[RGB]
[docs] BLUE: ClassVar[RGB]
[docs] BLUE_ISOSURFACE: ClassVar[RGB]
[docs] class RGBATuple: """Named RGBA quadruples in ``[0, 1]``."""
[docs] WHITE: ClassVar[RGBA]
[docs] BLACK: ClassVar[RGBA]
[docs] RED: ClassVar[RGBA]
[docs] TEAPOT_RED: ClassVar[RGBA]
[docs] GREEN: ClassVar[RGBA]
[docs] BLUE: ClassVar[RGBA]
for name, rgb in _COLOR_VALUES.items(): setattr(RGBTuple, name, rgb) setattr(RGBATuple, name, (*rgb, 1.0)) @dataclass(frozen=False)
[docs] class Coord: """RGB color"""
[docs] r: float = 1.0
[docs] g: float = 1.0
[docs] b: float = 1.0
[docs] def __post_init__(self): setattr(self, "r", clamp01(self.r)) setattr(self, "g", clamp01(self.g)) setattr(self, "b", clamp01(self.b))
@property
[docs] def tuple(self) -> tuple[float, float, float]: return self.to_tuple()
[docs] def to_tuple(self) -> tuple[float, float, float]: return self.r, self.g, self.b
[docs] def with_alpha(self, alpha: float) -> "RGBAColor": return RGBAColor(self.r, self.g, self.b, alpha)