annotools.color¶
Color parsing for overlay parameters and a stable text-to-color hash, so the same label or mask ID keeps its color across runs without a lookup table.
color ¶
Color parsing, stable text-to-color hashing, and helpers.
parse_color ¶
Parse a CSS/PIL color name or #RRGGBB into an RGB tuple.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
Color name ( |
required |
name
|
str
|
Prefix used in the error message so callers can point at the offending field. |
'color'
|
Returns:
| Type | Description |
|---|---|
RGB
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Naming |
Examples:
>>> from annotools import parse_color
>>> parse_color("red"), parse_color("#0000ff")
((255, 0, 0), (0, 0, 255))
Source code in src/annotools/color.py
color_from_text ¶
Map any text to a stable, saturated color.
The hue comes from the first two bytes of sha256(text); saturation is 0.75 and lightness 0.5,
which keeps every color readable on both light and dark backgrounds. Identical text always yields
the same color and a one-character change yields an unrelated one, so labels and mask IDs get
consistent colors across runs without a lookup table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Any string (a class name, a mask ID rendered as text). |
required |
Returns:
| Type | Description |
|---|---|
RGB
|
|
Examples:
>>> from annotools import color_from_text, to_hex
>>> color_from_text("cat") == color_from_text("cat")
True
>>> to_hex(color_from_text("cat")).startswith("#")
True
References
- Spec:
.agents/knowledge/spec/color-from-text.md(annotools repository).
Source code in src/annotools/color.py
to_hex ¶
Format an RGB tuple as lowercase #rrggbb.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color
|
RGB
|
|
required |
Returns:
| Type | Description |
|---|---|
str
|
The seven-character hex string. |
Examples: