A library for generating color palettes with distinct colors
Project description
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
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 glasbeyish-0.2.0.tar.gz.
File metadata
- Download URL: glasbeyish-0.2.0.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b5134f8f881aaf16eae022c60b44b66371cbec54587e1b96a61625b95dbdecd
|
|
| MD5 |
93fb7cde9cfa8b90aae51d14bf3e3313
|
|
| BLAKE2b-256 |
a8b48bececb3151ebfd3a5bb637f2723a422daf01c6fad95e0cd4f40748e5b62
|
Provenance
The following attestation bundles were made for glasbeyish-0.2.0.tar.gz:
Publisher:
package.yml on actapia/glasbeyish
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
glasbeyish-0.2.0.tar.gz -
Subject digest:
5b5134f8f881aaf16eae022c60b44b66371cbec54587e1b96a61625b95dbdecd - Sigstore transparency entry: 196454233
- Sigstore integration time:
-
Permalink:
actapia/glasbeyish@3848be698f5311b716ebc3733c811ff29fbb6fec -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/actapia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
package.yml@3848be698f5311b716ebc3733c811ff29fbb6fec -
Trigger Event:
release
-
Statement type:
File details
Details for the file glasbeyish-0.2.0-py3-none-any.whl.
File metadata
- Download URL: glasbeyish-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4ca5bdedbeee0aaa29f60fe9d81fc49b246cddbab4d06db6b91efd7b61021b5
|
|
| MD5 |
1e127d468f1df0a81c65db97c90f2081
|
|
| BLAKE2b-256 |
b75a654b80f1a75c75115ddb3aa94e2302c2af881c9af7468deb5003375f9f9c
|
Provenance
The following attestation bundles were made for glasbeyish-0.2.0-py3-none-any.whl:
Publisher:
package.yml on actapia/glasbeyish
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
glasbeyish-0.2.0-py3-none-any.whl -
Subject digest:
a4ca5bdedbeee0aaa29f60fe9d81fc49b246cddbab4d06db6b91efd7b61021b5 - Sigstore transparency entry: 196454235
- Sigstore integration time:
-
Permalink:
actapia/glasbeyish@3848be698f5311b716ebc3733c811ff29fbb6fec -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/actapia
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
package.yml@3848be698f5311b716ebc3733c811ff29fbb6fec -
Trigger Event:
release
-
Statement type: