glasbeyish
This is a library that implements a method described in the paper "Colour Displays for Categorical Images" by Glasbey et al. for finding optimal distinct color palettes of a specified size.
Unlike the glasbey library, this library implements the simulated annealing method described in the paper rather, than the iterative method. The simulated annealing method can often give better color palettes at the expense of longer run times.
Examples
These examples can also be found in the examples.ipynb Jupyter notebook.
import numpy as np
from nice_colorsys import rgb
from glasbeyish import glasbey
from matplotlib.colors import ListedColormap
Generate a palette with 2 colors
cm = ListedColormap([c.to_rgb() for c in glasbey(2)])
cm
Generate a palette with 12 colors
cm = ListedColormap([c.to_rgb() for c in glasbey(12)])
cm
Use a seed for reproducibility
cm = ListedColormap([c.to_rgb() for c in glasbey(12, seed=485)])
cm
cm = ListedColormap([c.to_rgb() for c in glasbey(12, seed=485)])
cm
Limit the colors that can be generated
Here, we'll try to make a bluish color palette by discarding colors too far from blue.
def color_dist(a, b):
return np.linalg.norm(np.array(a.to_cieluv()) - np.array(b.to_cieluv()))
cm = ListedColormap([c.to_rgb() for c in glasbey(12, seed=485, limit=lambda x: color_dist(x, rgb(0, 0, 1)) < 100)])
cm
Use a custom random color generator
In this example, we'll generate random colors with only blue components in the
RGB color space. glasbey expects the generated colors to be in the CIELUV
color space and won't automatically convert the colors from RGB to CIELUV for
us, so we have to do that ourselves.
rng = np.random.default_rng()
def random_color(rng):
return rgb(0, 0, rng.random()).to_cieluv()
cm = ListedColormap([c.to_rgb().safe() for c in glasbey(12, seed=485, random_color=random_color)])
cm
Use a custom color distance
As an example, this code shows using a custom color distance to generate a palette for people with protanopia, a form of color blindness.
Disclaimer: This has not been tested with color blind users and might not even correctly assess color differences as seen by protanopes—it's for demonstration purposes only!
hpe = np.array([
[0.4002, 0.7076, -0.0808],
[-0.2263, 1.1653, 0.0457],
[0, 0, 0.9182]
])
hpe_inv = np.linalg.inv(hpe)
lms = colorspace("lms", ["long", "medium", "short"])
register_space(lms, *derived_rgb_functions(ciexyz, lms, lambda x: hpe_inv @ x, lambda x: hpe @ x))
# Protanopia matrix from https://ixora.io/projects/colorblindness/color-blindness-simulation-research/
protan_matrix = np.array([
[0, 1.05118294, -0.05116099],
[0, 1, 0 ],
[0, 0, 1 ]
])
def protan(x):
return np.array(lms(*(protan_matrix @ cieluv(*x).to_lms())).to_cieluv())
def protan_dist(a, b):
a = protan(a)
b = protan(b)
return np.linalg.norm(b - a)
cm = ListedColormap([c.to_rgb().safe() for c in glasbey(8, seed=485, color_dist=protan_dist)])
cm
Metadata
Release files for glasbeyish 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| glasbeyish-0.2.0.tar.gz | 12.4 kB | With provenance | Details for glasbeyish-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| glasbeyish-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for glasbeyish-0.2.0-py3-none-any.whl |
Total release size: 18.0 kB
Release files / glasbeyish-0.2.0.tar.gz
| Download URL | glasbeyish-0.2.0.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5b5134f8f881aaf16eae022c60b44b66371cbec54587e1b96a61625b95dbdecd
|
|
BLAKE2b-256 checksum How to use checksums |
a8b48bececb3151ebfd3a5bb637f2723a422daf01c6fad95e0cd4f40748e5b62
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 14, 2025.
Transparency logRelease files / glasbeyish-0.2.0-py3-none-any.whl
| Download URL | glasbeyish-0.2.0-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a4ca5bdedbeee0aaa29f60fe9d81fc49b246cddbab4d06db6b91efd7b61021b5
|
|
BLAKE2b-256 checksum How to use checksums |
b75a654b80f1a75c75115ddb3aa94e2302c2af881c9af7468deb5003375f9f9c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 14, 2025.
Transparency logSecurity for glasbeyish 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for glasbeyish-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 14, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.