Python package for https://github.com/simple-color-palette/simple-color-palette
Project description
simple-color-palette-py
v0.1.0
A Python package for the Simple Color Palette format — a minimal JSON-based file format for defining color palettes (MIT license)
Latest Changes
- Initial release
Install
pip install simple-color-palette
API Reference
Dataclasses
simplecolorpalette provides Python dataclasses for defining a color Palette, its constituent Colors, and the RGB Components of those colors. Each dataclass field can be given either positionally or by keyword.
Components
Stores linear sRGB color channel data. Values outside the range of 0.0 to 1.0 are allowed for the red, green, and blue components. Opacity values outside the 0.0 to 1.0 range will be clamped.
| Field | Type | Required | Description |
|---|---|---|---|
red |
float |
Yes | Red channel (linear sRGB) |
green |
float |
Yes | Green channel (linear sRGB) |
blue |
float |
Yes | Blue channel (linear sRGB) |
opacity |
float |
No | Optional opacity/alpha channel. Defaults to 1.0 if omitted |
cyan_components = Components(red=0.0, green=1.0, blue=1.0)
magenta_components = Components(red=1.0, green=0.0, blue=1.0)
yellow_components = Components(red=1.0, green=1.0, blue=0.0)
Color
Represents a single color swatch in a palette; defined by its red, green, blue, and (optionally) alpha components.
| Field | Type | Required | Description |
|---|---|---|---|
name |
str |
No | Optional color name (e.g., 'Cyan'). |
components |
Components |
Yes | Linear sRGB RGB or RGBA components |
cyan = Color(name='Cyan', components=cyan_components)
magenta = Color(name='Magenta', components=magenta_components)
yellow = Color(name='Yellow', components=yellow_components)
Palette
Represents a named collection of colors.
| Field | Type | Required | Description |
|---|---|---|---|
name |
str |
No | Palette display name (e.g., 'CMY') |
colors |
list[Color] |
Yes | List of color entries (must contain at least one Color) |
palette = Palette(name='CMY', colors=[cyan, magenta, yellow])
Functions
load(path: pathlib.Path | str) -> Palette
Loads a *.color-palette JSON file into a Palette object
palette = load('/path/to/palette.color-palette')
print(palette.name)
print(palette.colors[0].components)
save(palette: Palette, path: pathlib.Path | str) -> None
Serializes a Palette object and writes it to disk in spec-compliant *.color-palette JSON format
[!NOTE] If the
Paletteobject passed tosavedoes not have a definedname, the file name will be used (without the*.color-paletteextension)
save(palette, 'my_palette.color-palette')
Output format
{
"name": "CMY",
"colors": [
{ "name": "Cyan", "components": [0.0, 1.0, 1.0] },
{ "name": "Magenta", "components": [1.0, 0.0, 1.0] },
{ "name": "Yellow", "components": [1.0, 1.0, 0.0] }
]
}
from_hex(hex_string: str) -> Components
Creates a Components object from a hex color code string, e.g. "#DEADBEEF"
| Parameter | Type | Description |
|---|---|---|
hex_string |
str |
3, 4, 6, or 8-digit RGB hex color code (the leading '#' is optional) |
hex_color = '#ff3344'
components = from_hex(hex_color)
print(components)
# >>> Components(red=1.0, green=0.2, blue=0.26667, opacity=1.0)
hex_color_with_opacity = '#4466ffcc'
components_with_opacity = from_hex(hex_color_with_opacity)
print(components_with_opacity)
# >>> Components(red=0.26667, green=0.4, blue=1.0, opacity=0.8)
Usage Example
import simplecolorpalette as scp
# define some colors - scp.Color dataclass objects
red_color = scp.Color(
name='Red',
components=scp.Components(red=1.0, green=0.0, blue=0.0,)
)
yellow_color = scp.Color(
name='Yellow',
components=scp.Components(red=1.0, green=1.0, blue=0.0)
)
green_color = scp.Color(
name='Green',
components=scp.Components(red=0.0, green=1.0, blue=0.0,)
)
# define the color palette - scp.Palette object
palette = scp.Palette(
name='Traffic Lights',
colors=[red_color, yellow_color, green_color],
)
# introspecting color components
print(red_color.components)
# >>> Components(red=1.0, green=0.0, blue=0.0, opacity=1.0)
# modifying color components
red_color.components.red = 0.9
# saving to JSON (as a *.color-palette file)
palette_file_path = 'path/to/palette.color-palette'
scp.save(palette, palette_file_path)
# loading palette data from a *.color-palette file
palette_data = scp.load('path/to/palette.color-palette')
print(palette_data)
# >>> Palette(colors=[Color(components=Components(red=0.9, green=0.0, blue=0.0, opacity=1.0), name='Red'), Color(components=Components(red=1.0, green=1.0, blue=0.0, opacity=1.0), name='Yellow'), Color(components=Components(red=0.0, green=1.0, blue=0.0, opacity=1.0), name='Green')], name='Traffic Lights')
[!NOTE]
- The
simplecolorpalette.saveandsimplecolorpalette.loadfunctions will accept file paths with either a*.color-paletteor*.jsonfile extension, other file extensions will raise aValueError.- Output files saved with
save()will use the*.color-paletteextension.
If you want to access Palette data in dict format, use dataclasses.asdict:
from dataclasses import asdict
... # other stuff omitted for brevity
print(asdict(palette_data))
# >>> {'colors': [{'components': {'red': 0.9, 'green': 0.0, 'blue': 0.0, 'opacity': 1.0}, 'name': 'Red'}, {'components': {'red': 1.0, 'green': 1.0, 'blue': 0.0, 'opacity': 1.0}, 'name': 'Yellow'}, {'components': {'red': 0.0, 'green': 1.0, 'blue': 0.0, 'opacity': 1.0}, 'name': 'Green'}], 'name': 'Traffic Lights'}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple_color_palette-0.1.0.tar.gz.
File metadata
- Download URL: simple_color_palette-0.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0af68054cba25bff70f05ecb8d58be29f92c2e5e51cb5798d2ff5daa070fa6e
|
|
| MD5 |
9c83ea73e1bad6ab566fd89d5e4656fe
|
|
| BLAKE2b-256 |
4febe48fbbc90ae4ecb7c2af925ec80d3660846f94c70a49fc101f892a364ddf
|
File details
Details for the file simple_color_palette-0.1.0-py3-none-any.whl.
File metadata
- Download URL: simple_color_palette-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfc6e4d9856358d63167493972ac96a96cce871a06727b43ff21115597747b4b
|
|
| MD5 |
3df210b5469ba6b552ac05b09001d2c1
|
|
| BLAKE2b-256 |
8adde6891ab9d9b5ed39e71bae616de24fa97ecdec3d465b7b79a87708dbca8a
|