Color API Reference¶
Color representation and parsing utilities.
Color Class¶
Constructor¶
| Parameter | Type | Default | Description |
|---|---|---|---|
r |
int | required | Red component (0-255) |
g |
int | required | Green component (0-255) |
b |
int | required | Blue component (0-255) |
a |
int | 255 |
Alpha component (0-255) |
Class Methods¶
from_name()¶
Create a Color from a CSS color name.
Raises ValueError if the name is not recognized.
from_hex()¶
Create a Color from a hex string.
Supported formats:
- #rgb - 3-digit shorthand
- #rrggbb - 6-digit
- #rgba - 4-digit with alpha
- #rrggbbaa - 8-digit with alpha
parse()¶
Parse a color from any supported format.
| Input Type | Description |
|---|---|
str |
Color name or hex code |
(r, g, b) |
RGB tuple |
(r, g, b, a) |
RGBA tuple |
Color |
Returns unchanged |
Color.parse("coral")
Color.parse("#ff6347")
Color.parse((255, 99, 71))
Color.parse((255, 99, 71, 128))
Properties¶
| Property | Type | Description |
|---|---|---|
r |
int | Red component (0-255) |
g |
int | Green component (0-255) |
b |
int | Blue component (0-255) |
a |
int | Alpha component (0-255) |
Instance Methods¶
as_tuple()¶
Get color as an RGBA tuple.
as_rgb_tuple()¶
Get color as an RGB tuple (without alpha).
to_hex()¶
Convert color to hex string.
with_alpha()¶
Create a new Color with modified alpha.
| Parameter | Type | Description |
|---|---|---|
alpha |
int | Alpha value 0-255 |
alpha |
float | Alpha value 0.0-1.0 |
lerp()¶
Linearly interpolate between two colors.
| Parameter | Type | Description |
|---|---|---|
other |
Color | Target color |
t |
float | Interpolation factor (0.0-1.0) |
red = Color.parse("red")
blue = Color.parse("blue")
# t=0.0 returns red, t=1.0 returns blue
purple = red.lerp(blue, 0.5)
Type Aliases¶
ColorLike¶
Any value that can be converted to a Color:
All color parameters in the API accept ColorLike.
Named Colors¶
All 147 CSS named colors are supported:
# Basic colors
"red", "green", "blue", "white", "black", "yellow", "cyan", "magenta"
# Popular web colors
"coral", "dodgerblue", "tomato", "gold", "hotpink", "limegreen"
# Grays
"whitesmoke", "gainsboro", "lightgray", "silver", "gray", "dimgray"
See the Colors Guide for the complete list.
Example¶
from window_art import Color
# Create colors
c1 = Color(255, 99, 71)
c2 = Color.parse("dodgerblue")
c3 = Color.from_hex("#ffd700")
# Manipulate
semi = c1.with_alpha(0.5)
mid = c1.lerp(c2, 0.5)
# Convert
print(c1.to_hex()) # "#ff6347"
print(c1.as_tuple()) # (255, 99, 71, 255)
print(c1.as_rgb_tuple()) # (255, 99, 71)