Skip to content

Color

arabic_animations.core.color.Color dataclass

Color class supporting multiple formats and alpha channel Values are stored internally as RGB (0-1 range)

Source code in arabic_animations/core/color.py
@dataclass
class Color:
    """
    Color class supporting multiple formats and alpha channel
    Values are stored internally as RGB (0-1 range)
    """
    r: float = 0.0
    g: float = 0.0
    b: float = 0.0
    a: float = 1.0

    @classmethod
    def from_rgb(cls, r: float, g: float, b: float, a: float = 1.0) -> 'Color':
        """Create from RGB values (0-1 range)"""
        return cls(r, g, b, a)

    @classmethod
    def from_rgb255(cls, r: int, g: int, b: int, a: float = 1.0) -> 'Color':
        """Create from RGB values (0-255 range)"""
        return cls(r/255, g/255, b/255, a)

    @classmethod
    def from_hex(cls, hex_color: str) -> 'Color':
        """Create from hex string (#RRGGBB or #RRGGBBAA)"""
        hex_color = hex_color.lstrip('#')
        if len(hex_color) == 6:
            r, g, b = tuple(int(hex_color[i:i+2], 16)/255 for i in (0, 2, 4))
            return cls(r, g, b)
        elif len(hex_color) == 8:
            r, g, b, a = tuple(int(hex_color[i:i+2], 16)/255 for i in (0, 2, 4, 6))
            return cls(r, g, b, a)
        raise ValueError("Invalid hex color format")

    @classmethod
    def from_hsl(cls, h: float, s: float, l: float, a: float = 1.0) -> 'Color':
        """Create from HSL values (H: 0-360, S: 0-1, L: 0-1)"""
        r, g, b = colorsys.hls_to_rgb(h/360, l, s)
        return cls(r, g, b, a)

    @classmethod
    def from_hsv(cls, h: float, s: float, v: float, a: float = 1.0) -> 'Color':
        """Create from HSV values (H: 0-360, S: 0-1, V: 0-1)"""
        r, g, b = colorsys.hsv_to_rgb(h/360, s, v)
        return cls(r, g, b, a)

    def to_rgb(self) -> Tuple[float, float, float, float]:
        """Get RGB values (0-1 range)"""
        return (self.r, self.g, self.b, self.a)

    def to_rgb255(self) -> Tuple[int, int, int, float]:
        """Get RGB values (0-255 range)"""
        return (int(self.r * 255), int(self.g * 255), int(self.b * 255), self.a)

    def to_hex(self) -> str:
        """Get hex string"""
        if self.a == 1:
            return f"#{int(self.r*255):02x}{int(self.g*255):02x}{int(self.b*255):02x}"
        return f"#{int(self.r*255):02x}{int(self.g*255):02x}{int(self.b*255):02x}{int(self.a*255):02x}"

    def to_hsl(self) -> Tuple[float, float, float, float]:
        """Get HSL values"""
        h, l, s = colorsys.rgb_to_hls(self.r, self.g, self.b)
        return (h*360, s, l, self.a)

    def to_hsv(self) -> Tuple[float, float, float, float]:
        """Get HSV values"""
        h, s, v = colorsys.rgb_to_hsv(self.r, self.g, self.b)
        return (h*360, s, v, self.a)

    def with_alpha(self, alpha: float) -> 'Color':
        """Return new color with modified alpha"""
        return Color(self.r, self.g, self.b, alpha)

    def lighten(self, amount: float) -> 'Color':
        """Return lightened color"""
        h, s, l, a = self.to_hsl()
        return Color.from_hsl(h, s, min(1, l + amount), a)

    def darken(self, amount: float) -> 'Color':
        """Return darkened color"""
        h, s, l, a = self.to_hsl()
        return Color.from_hsl(h, s, max(0, l - amount), a)

darken(amount)

Return darkened color

Source code in arabic_animations/core/color.py
def darken(self, amount: float) -> 'Color':
    """Return darkened color"""
    h, s, l, a = self.to_hsl()
    return Color.from_hsl(h, s, max(0, l - amount), a)

from_hex(hex_color) classmethod

Create from hex string (#RRGGBB or #RRGGBBAA)

Source code in arabic_animations/core/color.py
@classmethod
def from_hex(cls, hex_color: str) -> 'Color':
    """Create from hex string (#RRGGBB or #RRGGBBAA)"""
    hex_color = hex_color.lstrip('#')
    if len(hex_color) == 6:
        r, g, b = tuple(int(hex_color[i:i+2], 16)/255 for i in (0, 2, 4))
        return cls(r, g, b)
    elif len(hex_color) == 8:
        r, g, b, a = tuple(int(hex_color[i:i+2], 16)/255 for i in (0, 2, 4, 6))
        return cls(r, g, b, a)
    raise ValueError("Invalid hex color format")

from_hsl(h, s, l, a=1.0) classmethod

Create from HSL values (H: 0-360, S: 0-1, L: 0-1)

Source code in arabic_animations/core/color.py
@classmethod
def from_hsl(cls, h: float, s: float, l: float, a: float = 1.0) -> 'Color':
    """Create from HSL values (H: 0-360, S: 0-1, L: 0-1)"""
    r, g, b = colorsys.hls_to_rgb(h/360, l, s)
    return cls(r, g, b, a)

from_hsv(h, s, v, a=1.0) classmethod

Create from HSV values (H: 0-360, S: 0-1, V: 0-1)

Source code in arabic_animations/core/color.py
@classmethod
def from_hsv(cls, h: float, s: float, v: float, a: float = 1.0) -> 'Color':
    """Create from HSV values (H: 0-360, S: 0-1, V: 0-1)"""
    r, g, b = colorsys.hsv_to_rgb(h/360, s, v)
    return cls(r, g, b, a)

from_rgb(r, g, b, a=1.0) classmethod

Create from RGB values (0-1 range)

Source code in arabic_animations/core/color.py
@classmethod
def from_rgb(cls, r: float, g: float, b: float, a: float = 1.0) -> 'Color':
    """Create from RGB values (0-1 range)"""
    return cls(r, g, b, a)

from_rgb255(r, g, b, a=1.0) classmethod

Create from RGB values (0-255 range)

Source code in arabic_animations/core/color.py
@classmethod
def from_rgb255(cls, r: int, g: int, b: int, a: float = 1.0) -> 'Color':
    """Create from RGB values (0-255 range)"""
    return cls(r/255, g/255, b/255, a)

lighten(amount)

Return lightened color

Source code in arabic_animations/core/color.py
def lighten(self, amount: float) -> 'Color':
    """Return lightened color"""
    h, s, l, a = self.to_hsl()
    return Color.from_hsl(h, s, min(1, l + amount), a)

to_hex()

Get hex string

Source code in arabic_animations/core/color.py
def to_hex(self) -> str:
    """Get hex string"""
    if self.a == 1:
        return f"#{int(self.r*255):02x}{int(self.g*255):02x}{int(self.b*255):02x}"
    return f"#{int(self.r*255):02x}{int(self.g*255):02x}{int(self.b*255):02x}{int(self.a*255):02x}"

to_hsl()

Get HSL values

Source code in arabic_animations/core/color.py
def to_hsl(self) -> Tuple[float, float, float, float]:
    """Get HSL values"""
    h, l, s = colorsys.rgb_to_hls(self.r, self.g, self.b)
    return (h*360, s, l, self.a)

to_hsv()

Get HSV values

Source code in arabic_animations/core/color.py
def to_hsv(self) -> Tuple[float, float, float, float]:
    """Get HSV values"""
    h, s, v = colorsys.rgb_to_hsv(self.r, self.g, self.b)
    return (h*360, s, v, self.a)

to_rgb()

Get RGB values (0-1 range)

Source code in arabic_animations/core/color.py
def to_rgb(self) -> Tuple[float, float, float, float]:
    """Get RGB values (0-1 range)"""
    return (self.r, self.g, self.b, self.a)

to_rgb255()

Get RGB values (0-255 range)

Source code in arabic_animations/core/color.py
def to_rgb255(self) -> Tuple[int, int, int, float]:
    """Get RGB values (0-255 range)"""
    return (int(self.r * 255), int(self.g * 255), int(self.b * 255), self.a)

with_alpha(alpha)

Return new color with modified alpha

Source code in arabic_animations/core/color.py
def with_alpha(self, alpha: float) -> 'Color':
    """Return new color with modified alpha"""
    return Color(self.r, self.g, self.b, alpha)

arabic_animations.core.color.Style dataclass

Text styling options

Source code in arabic_animations/core/color.py
@dataclass
class Style:
    """Text styling options"""
    stroke_color: Color = field(default_factory=lambda: Colors.BLACK)
    fill_color: Optional[Color] = None
    stroke_width: float = 2.0
    background_color: Color = field(default_factory=lambda: Colors.PAPER_WHITE)
    shadow_color: Optional[Color] = None
    shadow_offset: Tuple[float, float] = (0, 0)
    shadow_blur: float = 0.0
    glow_color: Optional[Color] = None
    glow_radius: float = 0.0
    gradient: Optional[Tuple[Color, Color]] = None
    gradient_direction: Optional[Tuple[float, float]] = None  # (x, y) vector

Usage Examples

Creating Colors

from arabic_animations.core.color import Color, Colors

# From RGB (0-1)
red = Color.from_rgb(1.0, 0.0, 0.0)

# From RGB (0-255)
blue = Color.from_rgb255(0, 0, 255)

# From hex
gold = Color.from_hex("#FFD700")

# From HSL
green = Color.from_hsl(120, 1.0, 0.5)

Using Predefined Colors

# Basic colors
black = Colors.BLACK
white = Colors.WHITE

# UI colors
primary = Colors.PRIMARY
warning = Colors.WARNING

# Paper colors
background = Colors.PAPER_CREAM

Creating Styles

from arabic_animations.core.color import Style

# Basic style
style = Style(
    stroke_color=Colors.BLACK,
    fill_color=Colors.BLUE
)

# Complex style
style = Style(
    stroke_color=Colors.PRIMARY,
    gradient=(Colors.PRIMARY, Colors.SECONDARY),
    glow_color=Colors.WHITE.with_alpha(0.6),
    glow_radius=3.0
)