Skip to main content

Utilities for working with colors and Plotly-friendly palettes.

Project description

san-diego

Small Python utilities for working with colors, image palettes, and Plotly-friendly color output. Install with pip install san-diego and import with import sd.

Notebook Workflow

There is a starter notebook at notebooks/color_playground.ipynb.

Install notebook tooling:

uv sync --group dev

Launch JupyterLab:

uv run --group dev jupyter lab

Features

  • Extract dominant colors from an image.
  • Convert between RGB, HEX, and HSL.
  • Lighten or darken a color by a percentage.
  • Build Plotly colorscales and color sequences.
  • Save, load, and reuse named color schemes with consistent roles.
  • Use Color and ColorScale classes for structured color handling.

Example

from sd import (
    Color,
    ColorScheme,
    ColorSequence,
    ColorScale,
    build_plotly_colorscale,
    build_plotly_colorscale_from_scheme,
    color_swatches,
    get_color_scheme,
    get_color_sequence,
    extract_dominant_colors,
    extract_dominant_hex_colors,
    list_color_scheme_roles,
    save_color_scheme,
)

brand = Color.from_hex("#1f77b4")

brand.to_rgb()
# (31, 119, 180)

brand.to_hsl()
# (204.6, 0.706161, 0.413725)

brand.lighten(20).to_hex()
# '#4c92c3'

brand.as_plotly_rgb()
# 'rgb(31, 119, 180)'

scale = ColorScale.from_colors(["#0d3b66", "#faf0ca", "#f95738"])
scale.to_plotly()
# [[0.0, '#0d3b66'], [0.5, '#faf0ca'], [1.0, '#f95738']]

build_plotly_colorscale(["#000000", "#ffffff"])
# [[0.0, '#000000'], [1.0, '#ffffff']]

extract_dominant_colors("example.png", count=3)
# [Color(...), Color(...), Color(...)]

extract_dominant_hex_colors("example.png", count=3)
# ['#...', '#...', '#...']

color_swatches(["#1f77b4", "rgb(255, 127, 14)", (44, 160, 44)])
# In Jupyter, renders labeled color swatches.

scheme = get_color_scheme("ocean")
list_color_scheme_roles()
# ['primary', 'secondary', 'accent', 'dark', 'light', 'blue', 'purple', 'red', 'orange', 'yellow', 'green']

scheme.to_hex_map()
# {
#   'primary': '#3a506b',
#   'secondary': '#5bc0be',
#   'accent': '#c5f3f0',
#   'dark': '#0b132b',
#   'light': '#e9fffe',
#   'blue': '#3a6ea5',
#   'purple': '#6a4c93',
#   'red': '#c44536',
#   'orange': '#d97d54',
#   'yellow': '#d8c36a',
#   'green': '#4c956c',
# }

sequence = get_color_sequence("blue")
sequence.to_hex_list()
# ['#ffffff', '#d5e2f0', '#aac5e1', '#80a8d2', '#558bc3', '#3a6ea5', '#2e5884', '#234263', '#172c42', '#0c1621', '#000000']

ColorSequence.from_color("brand-blue", "#336699").to_plotly_sequence()
# ['#ffffff', '#d1e0f0', '#a3c2e0', '#75a3d1', '#4785c2', '#336699', '#29527a', '#1f3d5c', '#14293d', '#0a141f', '#000000']

save_color_scheme(
    "brand",
    {
        "primary": "#112233",
        "secondary": "#445566",
        "accent": "#778899",
        "dark": "#0a0f14",
        "light": "#f5f7fa",
        "blue": "#4f74c8",
        "purple": "#7f5ab6",
        "red": "#bf4b4b",
        "orange": "#d88752",
        "yellow": "#d8c15e",
        "green": "#4d8b68",
    },
    "brand.json",
)

ColorScheme.from_reference("brand.json").to_plotly_colorscale()
# [[0.0, '#112233'], [0.1, '#445566'], [0.2, '#778899'], [0.3, '#0a0f14'], [0.4, '#f5f7fa'], [0.5, '#4f74c8'], [0.6, '#7f5ab6'], [0.7, '#bf4b4b'], [0.8, '#d88752'], [0.9, '#d8c15e'], [1.0, '#4d8b68']]

build_plotly_colorscale_from_scheme("sunset")
# [[0.0, '#e9724c'], [0.1, '#ffc857'], [0.2, '#c5283d'], [0.3, '#1f2041'], [0.4, '#fff1d6'], [0.5, '#4d6cfa'], [0.6, '#7b5ea7'], [0.7, '#c5283d'], [0.8, '#e9724c'], [0.9, '#ffc857'], [1.0, '#7aa67a']]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

san_diego-0.2.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

san_diego-0.2.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file san_diego-0.2.0.tar.gz.

File metadata

  • Download URL: san_diego-0.2.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for san_diego-0.2.0.tar.gz
Algorithm Hash digest
SHA256 278674837f7165768389655e36919ab10fe1fe1c026d319ba438d59418959dab
MD5 bf670b69fe6965b03fc3cf47143e6c3b
BLAKE2b-256 aa7829802f8e0acecf4be43e1bf77930b53e9d071e7b934a00cdabf968a71c3b

See more details on using hashes here.

File details

Details for the file san_diego-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: san_diego-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.13.13

File hashes

Hashes for san_diego-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0726273e86bd708672066b8a7d324b70eb98bc095757ae5e8f140cf09a60abdf
MD5 ce5c5546e02f49bc7563c57e50a32202
BLAKE2b-256 11e4c2a29d2f94654ff23e475ffe4e6a7fb33800aac7eb21fd70e176d1404988

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page