Coloring
momapy.coloring
Color definitions and utilities for momapy.
This module provides the Color class for representing colors with RGBA components, along with a comprehensive set of predefined named colors. Colors can be created from various formats (RGB, RGBA, HEX, HEXA) and converted between formats.
Examples:
from momapy.coloring import Color, red, blue
# Using predefined colors
color = red
# Creating from hex
color = Color.from_hex("#FF5733")
# Creating from RGB
color = Color.from_rgb(255, 87, 51)
# With alpha transparency
color = Color.from_rgba(255, 87, 51, 0.5)
# Convert to different formats
color.to_hex()
color.to_rgba()
Classes:
| Name | Description |
|---|---|
Color |
Represents an RGBA color. |
Functions:
| Name | Description |
|---|---|
get_color |
Return the named color with the given name. |
has_color |
Check if a named color exists. |
list_colors |
Return the names of all available named colors. |
print_colors |
Print all available named colors with their names. |
Attributes:
| Name | Type | Description |
|---|---|---|
aliceblue |
The color aliceblue | aliceblue |
|
antiquewhite |
The color antiquewhite | antiquewhite |
|
aqua |
The color aqua | aqua |
|
aquamarine |
The color aquamarine | aquamarine |
|
azure |
The color azure | azure |
|
beige |
The color beige | beige |
|
bisque |
The color bisque | bisque |
|
black |
The color black | black |
|
blanchedalmond |
The color blanchedalmond | blanchedalmond |
|
blue |
The color blue | blue |
|
blueviolet |
The color blueviolet | blueviolet |
|
brown |
The color brown | brown |
|
burlywood |
The color burlywood | burlywood |
|
cadetblue |
The color cadetblue | cadetblue |
|
chartreuse |
The color chartreuse | chartreuse |
|
chocolate |
The color chocolate | chocolate |
|
coral |
The color coral | coral |
|
cornflowerblue |
The color cornflowerblue | cornflowerblue |
|
cornsilk |
The color cornsilk | cornsilk |
|
crimson |
The color crimson | crimson |
|
cyan |
The color cyan | cyan |
|
darkblue |
The color darkblue | darkblue |
|
darkcyan |
The color darkcyan | darkcyan |
|
darkgoldenrod |
The color darkgoldenrod | darkgoldenrod |
|
darkgray |
The color darkgray | darkgray |
|
darkgreen |
The color darkgreen | darkgreen |
|
darkgrey |
The color darkgrey | darkgrey |
|
darkkhaki |
The color darkkhaki | darkkhaki |
|
darkmagenta |
The color darkmagenta | darkmagenta |
|
darkolivegreen |
The color darkolivegreen | darkolivegreen |
|
darkorange |
The color darkorange | darkorange |
|
darkorchid |
The color darkorchid | darkorchid |
|
darkred |
The color darkred | darkred |
|
darksalmon |
The color darksalmon | darksalmon |
|
darkseagreen |
The color darkseagreen | darkseagreen |
|
darkslateblue |
The color darkslateblue | darkslateblue |
|
darkslategray |
The color darkslategray | darkslategray |
|
darkturquoise |
The color darkturquoise | darkturquoise |
|
darkviolet |
The color darkviolet | darkviolet |
|
deeppink |
The color deeppink | deeppink |
|
deepskyblue |
The color deepskyblue | deepskyblue |
|
dimgray |
The color dimgray | dimgray |
|
dimgrey |
The color dimgrey | dimgrey |
|
dodgerblue |
The color dodgerblue | dodgerblue |
|
firebrick |
The color firebrick | firebrick |
|
floralwhite |
The color floralwhite | floralwhite |
|
forestgreen |
The color forestgreen | forestgreen |
|
fuchsia |
The color fuchsia | fuchsia |
|
gainsboro |
The color gainsboro | gainsboro |
|
ghostwhite |
The color ghostwhite | ghostwhite |
|
gold |
The color gold | gold |
|
goldenrod |
The color goldenrod | goldenrod |
|
gray |
The color gray | gray |
|
green |
The color green | green |
|
greenyellow |
The color greenyellow | greenyellow |
|
grey |
The color grey | grey |
|
honeydew |
The color honeydew | honeydew |
|
hotpink |
The color hotpink | hotpink |
|
indianred |
The color indianred | indianred |
|
indigo |
The color indigo | indigo |
|
ivory |
The color ivory | ivory |
|
khaki |
The color khaki | khaki |
|
lavender |
The color lavender | lavender |
|
lavenderblush |
The color lavenderblush | lavenderblush |
|
lawngreen |
The color lawngreen | lawngreen |
|
lemonchiffon |
The color lemonchiffon | lemonchiffon |
|
lightblue |
The color lightblue | lightblue |
|
lightcoral |
The color lightcoral | lightcoral |
|
lightcyan |
The color lightcyan | lightcyan |
|
lightgoldenrodyellow |
The color lightgoldenrodyellow | lightgoldenrodyellow |
|
lightgray |
The color lightgray | lightgray |
|
lightgreen |
The color lightgreen | lightgreen |
|
lightgrey |
The color lightgrey | lightgrey |
|
lightpink |
The color lightpink | lightpink |
|
lightsalmon |
The color lightsalmon | lightsalmon |
|
lightseagreen |
The color lightseagreen | lightseagreen |
|
lightskyblue |
The color lightskyblue | lightskyblue |
|
lightslategray |
The color lightslategray | lightslategray |
|
lightsteelblue |
The color lightsteelblue | lightsteelblue |
|
lightyellow |
The color lightyellow | lightyellow |
|
lime |
The color lime | lime |
|
limegreen |
The color limegreen | limegreen |
|
linen |
The color linen | linen |
|
magenta |
The color magenta | magenta |
|
maroon |
The color maroon | maroon |
|
mediumaquamarine |
The color mediumaquamarine | mediumaquamarine |
|
mediumblue |
The color mediumblue | mediumblue |
|
mediumorchid |
The color mediumorchid | mediumorchid |
|
mediumpurple |
The color mediumpurple | mediumpurple |
|
mediumseagreen |
The color mediumseagreen | mediumseagreen |
|
mediumslateblue |
The color mediumslateblue | mediumslateblue |
|
mediumspringgreen |
The color mediumspringgreen | mediumspringgreen |
|
mediumturquoise |
The color mediumturquoise | mediumturquoise |
|
mediumvioletred |
The color mediumvioletred | mediumvioletred |
|
midnightblue |
The color midnightblue | midnightblue |
|
mintcream |
The color mintcream | mintcream |
|
mistyrose |
The color mistyrose | mistyrose |
|
moccasin |
The color moccasin | moccasin |
|
navajowhite |
The color navajowhite | navajowhite |
|
navy |
The color navy | navy |
|
oldlace |
The color oldlace | oldlace |
|
olive |
The color olive | olive |
|
olivedrab |
The color olivedrab | olivedrab |
|
orange |
The color orange | orange |
|
orangered |
The color orangered | orangered |
|
orchid |
The color orchid | orchid |
|
palegoldenrod |
The color palegoldenrod | palegoldenrod |
|
palegreen |
The color palegreen | palegreen |
|
paleturquoise |
The color paleturquoise | paleturquoise |
|
palevioletred |
The color palevioletred | palevioletred |
|
papayawhip |
The color papayawhip | papayawhip |
|
peachpuff |
The color peachpuff | peachpuff |
|
peru |
The color peru | peru |
|
pink |
The color pink | pink |
|
plum |
The color plum | plum |
|
powderblue |
The color powderblue | powderblue |
|
purple |
The color purple | purple |
|
red |
The color red | red |
|
rosybrown |
The color rosybrown | rosybrown |
|
royalblue |
The color royalblue | royalblue |
|
saddlebrown |
The color saddlebrown | saddlebrown |
|
salmon |
The color salmon | salmon |
|
sandybrown |
The color sandybrown | sandybrown |
|
seagreen |
The color seagreen | seagreen |
|
seashell |
The color seashell | seashell |
|
sienna |
The color sienna | sienna |
|
silver |
The color silver | silver |
|
skyblue |
The color skyblue | skyblue |
|
slateblue |
The color slateblue | slateblue |
|
slategray |
The color slategray | slategray |
|
snow |
The color snow | snow |
|
springgreen |
The color springgreen | springgreen |
|
steelblue |
The color steelblue | steelblue |
|
tan |
The color tan | tan |
|
teal |
The color teal | teal |
|
thistle |
The color thistle | thistle |
|
tomato |
The color tomato | tomato |
|
turquoise |
The color turquoise | turquoise |
|
violet |
The color violet | violet |
|
wheat |
The color wheat | wheat |
|
white |
The color white | white |
|
whitesmoke |
The color whitesmoke | whitesmoke |
|
yellow |
The color yellow | yellow |
|
yellowgreen |
The color yellowgreen | yellowgreen |
Color
dataclass
Bases: object
Represents an RGBA color.
Examples:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
red
|
int
|
The red component of the color |
required |
green
|
int
|
The green component of the color |
required |
blue
|
int
|
The blue component of the color |
required |
alpha
|
float
|
The alpha component of the color |
1.0
|
Methods:
| Name | Description |
|---|---|
__or__ |
Return a new color with the specified alpha value. |
from_hex |
Create a color from a hexadecimal string. |
from_hexa |
Create a color from a hexadecimal string with alpha. |
from_rgb |
Create a color from RGB components. |
from_rgba |
Create a color from RGBA components. |
to_hex |
Return the color as hexadecimal string. |
to_hexa |
Return the color as hexadecimal string with alpha. |
to_rgb |
Return the color as RGB tuple. |
to_rgba |
Return the color as RGBA tuple. |
with_alpha |
Return a copy of the color with the specified alpha. |
__or__
Return a new color with the specified alpha value.
The | operator takes alpha on the same 0.0-1.0 scale used by the
constructor, from_rgba, and with_alpha, e.g. red | 0.5 yields
an alpha of 0.5.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Alpha value (0.0-1.0). |
required |
Returns:
| Type | Description |
|---|---|
Self
|
A new Color with the specified alpha. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If alpha is not between 0.0 and 1.0. |
Source code in src/momapy/coloring.py
from_hex
classmethod
Create a color from a hexadecimal string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_str
|
str
|
Hex color string (e.g., "#FF5733" or "FF5733"). |
required |
Returns:
| Type | Description |
|---|---|
Self
|
A new Color instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the hex string is invalid. |
Examples:
Source code in src/momapy/coloring.py
from_hexa
classmethod
Create a color from a hexadecimal string with alpha.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_str
|
str
|
Hex color string with alpha (e.g., "#FF5733FF"). |
required |
Returns:
| Type | Description |
|---|---|
Self
|
A new Color instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the hex string is invalid. |
Examples:
Source code in src/momapy/coloring.py
from_rgb
classmethod
Create a color from RGB components.
The rgb_range argument mirrors :meth:to_rgb, so
from_rgb(*color.to_rgb(rgb_range), rgb_range) round-trips.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
red
|
float
|
Red component, expressed in |
required |
green
|
float
|
Green component, expressed in |
required |
blue
|
float
|
Blue component, expressed in |
required |
rgb_range
|
tuple[float, float]
|
Range that the RGB components are expressed in, defaults to (0, 255). |
(0, 255)
|
Returns:
| Type | Description |
|---|---|
Self
|
A new Color instance with alpha=1.0, normalized to 0-255 RGB. |
Examples:
Source code in src/momapy/coloring.py
from_rgba
classmethod
from_rgba(red: float, green: float, blue: float, alpha: float, rgb_range: tuple[float, float] = (0, 255), alpha_range: tuple[float, float] = (0.0, 1.0)) -> Self
Create a color from RGBA components.
The rgb_range and alpha_range arguments mirror
:meth:to_rgba, so from_rgba(*color.to_rgba(rgb_range, alpha_range),
rgb_range, alpha_range) round-trips.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
red
|
float
|
Red component, expressed in |
required |
green
|
float
|
Green component, expressed in |
required |
blue
|
float
|
Blue component, expressed in |
required |
alpha
|
float
|
Alpha component, expressed in |
required |
rgb_range
|
tuple[float, float]
|
Range that the RGB components are expressed in, defaults to (0, 255). |
(0, 255)
|
alpha_range
|
tuple[float, float]
|
Range that |
(0.0, 1.0)
|
Returns:
| Type | Description |
|---|---|
Self
|
A new Color instance, normalized to 0-255 RGB / 0.0-1.0 alpha. |
Examples:
Source code in src/momapy/coloring.py
to_hex
Return the color as hexadecimal string.
Returns:
| Type | Description |
|---|---|
str
|
Hex color string (e.g., "#ff5733"). |
Examples:
Source code in src/momapy/coloring.py
to_hexa
Return the color as hexadecimal string with alpha.
The alpha byte is rounded (not truncated), so the
from_hexa / to_hexa round-trip is exact for every 8-bit
alpha value.
Returns:
| Type | Description |
|---|---|
str
|
Hex color string with alpha (e.g., "#ff5733ff"). |
Examples:
Source code in src/momapy/coloring.py
to_rgb
to_rgb(rgb_range: tuple[float, float] = (0, 255), round_to_int: bool = False) -> tuple[int | float, int | float, int | float]
Return the color as RGB tuple.
The components are sampled onto rgb_range and returned as floats by
default. Pass round_to_int=True to round them to integers (e.g. for
an 8-bit (0, 255) range).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rgb_range
|
tuple[float, float]
|
Range for RGB components, defaults to (0, 255). |
(0, 255)
|
round_to_int
|
bool
|
If True, round the components to integers. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[int | float, int | float, int | float]
|
Tuple of (red, green, blue). |
Examples:
Source code in src/momapy/coloring.py
to_rgba
to_rgba(rgb_range: tuple[float, float] = (0, 255), alpha_range: tuple[float, float] = (0.0, 1.0), rgba_range: tuple[float, float] | None = None, round_to_int: bool = False) -> tuple[int | float, int | float, int | float, float]
Return the color as RGBA tuple.
The RGB components are sampled onto rgb_range and returned as floats
by default. Pass round_to_int=True to round them to integers (e.g.
for an 8-bit (0, 255) range).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rgb_range
|
tuple[float, float]
|
Range for RGB components, defaults to (0, 255). |
(0, 255)
|
alpha_range
|
tuple[float, float]
|
Range for alpha component, defaults to (0.0, 1.0). |
(0.0, 1.0)
|
rgba_range
|
tuple[float, float] | None
|
Override range for all components. |
None
|
round_to_int
|
bool
|
If True, round the RGB components to integers. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[int | float, int | float, int | float, float]
|
Tuple of (red, green, blue, alpha). |
Examples:
color = Color(255, 128, 0)
color.to_rgba()
color.to_rgba(rgb_range=(0.0, 1.0))
color.to_rgba(round_to_int=True)
Source code in src/momapy/coloring.py
with_alpha
Return a copy of the color with the specified alpha.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Alpha value, expressed in |
required |
alpha_range
|
tuple[float, float]
|
Range that |
(0, 1)
|
Returns:
| Type | Description |
|---|---|
Self
|
A new Color with the specified alpha, normalized to 0.0-1.0. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/momapy/coloring.py
aliceblue
module-attribute
The color aliceblue | aliceblue
antiquewhite
module-attribute
The color antiquewhite | antiquewhite
aquamarine
module-attribute
The color aquamarine | aquamarine
blanchedalmond
module-attribute
The color blanchedalmond | blanchedalmond
blueviolet
module-attribute
The color blueviolet | blueviolet
burlywood
module-attribute
The color burlywood | burlywood
chartreuse
module-attribute
The color chartreuse | chartreuse
cornflowerblue
module-attribute
The color cornflowerblue | cornflowerblue
darkgoldenrod
module-attribute
The color darkgoldenrod | darkgoldenrod
darkkhaki
module-attribute
The color darkkhaki | darkkhaki
darkmagenta
module-attribute
The color darkmagenta | darkmagenta
darkolivegreen
module-attribute
The color darkolivegreen | darkolivegreen
darkorange
module-attribute
The color darkorange | darkorange
darkorchid
module-attribute
The color darkorchid | darkorchid
darksalmon
module-attribute
The color darksalmon | darksalmon
darkseagreen
module-attribute
The color darkseagreen | darkseagreen
darkslateblue
module-attribute
The color darkslateblue | darkslateblue
darkslategray
module-attribute
The color darkslategray | darkslategray
darkturquoise
module-attribute
The color darkturquoise | darkturquoise
darkviolet
module-attribute
The color darkviolet | darkviolet
deepskyblue
module-attribute
The color deepskyblue | deepskyblue
dodgerblue
module-attribute
The color dodgerblue | dodgerblue
floralwhite
module-attribute
The color floralwhite | floralwhite
forestgreen
module-attribute
The color forestgreen | forestgreen
gainsboro
module-attribute
The color gainsboro | gainsboro
get_color
get_color(color_name: str) -> Color
Return the named color with the given name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_name
|
str
|
Name of the color to look up. |
required |
Returns:
| Type | Description |
|---|---|
Color
|
The corresponding Color. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no color with that name exists. |
Examples:
Source code in src/momapy/coloring.py
ghostwhite
module-attribute
The color ghostwhite | ghostwhite
greenyellow
module-attribute
The color greenyellow | greenyellow
has_color
Check if a named color exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_name
|
str
|
Name of the color to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the color exists, False otherwise. |
Examples:
Source code in src/momapy/coloring.py
lavenderblush
module-attribute
The color lavenderblush | lavenderblush
lemonchiffon
module-attribute
The color lemonchiffon | lemonchiffon
lightblue
module-attribute
The color lightblue | lightblue
lightcoral
module-attribute
The color lightcoral | lightcoral
lightcyan
module-attribute
The color lightcyan | lightcyan
lightgoldenrodyellow
module-attribute
The color lightgoldenrodyellow | lightgoldenrodyellow
lightgray
module-attribute
The color lightgray | lightgray
lightgreen
module-attribute
The color lightgreen | lightgreen
lightgrey
module-attribute
The color lightgrey | lightgrey
lightpink
module-attribute
The color lightpink | lightpink
lightsalmon
module-attribute
The color lightsalmon | lightsalmon
lightseagreen
module-attribute
The color lightseagreen | lightseagreen
lightskyblue
module-attribute
The color lightskyblue | lightskyblue
lightslategray
module-attribute
The color lightslategray | lightslategray
lightsteelblue
module-attribute
The color lightsteelblue | lightsteelblue
lightyellow
module-attribute
The color lightyellow | lightyellow
list_colors
Return the names of all available named colors.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of color names for all predefined colors. |
Examples:
Source code in src/momapy/coloring.py
mediumaquamarine
module-attribute
The color mediumaquamarine | mediumaquamarine
mediumblue
module-attribute
The color mediumblue | mediumblue
mediumorchid
module-attribute
The color mediumorchid | mediumorchid
mediumpurple
module-attribute
The color mediumpurple | mediumpurple
mediumseagreen
module-attribute
The color mediumseagreen | mediumseagreen
mediumslateblue
module-attribute
The color mediumslateblue | mediumslateblue
mediumspringgreen
module-attribute
The color mediumspringgreen | mediumspringgreen
mediumturquoise
module-attribute
The color mediumturquoise | mediumturquoise
mediumvioletred
module-attribute
The color mediumvioletred | mediumvioletred
midnightblue
module-attribute
The color midnightblue | midnightblue
mintcream
module-attribute
The color mintcream | mintcream
mistyrose
module-attribute
The color mistyrose | mistyrose
navajowhite
module-attribute
The color navajowhite | navajowhite
palegoldenrod
module-attribute
The color palegoldenrod | palegoldenrod
palegreen
module-attribute
The color palegreen | palegreen
paleturquoise
module-attribute
The color paleturquoise | paleturquoise
palevioletred
module-attribute
The color palevioletred | palevioletred
papayawhip
module-attribute
The color papayawhip | papayawhip
peachpuff
module-attribute
The color peachpuff | peachpuff
powderblue
module-attribute
The color powderblue | powderblue
print_colors
Print all available named colors with their names.
Displays color names in their actual color in the terminal.
Examples:
Source code in src/momapy/coloring.py
rosybrown
module-attribute
The color rosybrown | rosybrown
saddlebrown
module-attribute
The color saddlebrown | saddlebrown
sandybrown
module-attribute
The color sandybrown | sandybrown
slategray
module-attribute
The color slategray | slategray
springgreen
module-attribute
The color springgreen | springgreen
whitesmoke
module-attribute
The color whitesmoke | whitesmoke