Skip to main content

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

Two color colormap

Generate a palette with 12 colors

cm = ListedColormap([c.to_rgb() for c in glasbey(12)])
cm

Twelve color colormap

Use a seed for reproducibility

cm = ListedColormap([c.to_rgb() for c in glasbey(12, seed=485)])
cm

Twelve color colormap, created with seed 485

cm = ListedColormap([c.to_rgb() for c in glasbey(12, seed=485)])
cm

The same twelve color colormap, created with seed 485

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

Bluish color map

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

Blue color map

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

Possibly a color palette optimized for protanopia

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

glasbeyish-0.2.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

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

glasbeyish-0.2.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

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

Hashes for glasbeyish-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5b5134f8f881aaf16eae022c60b44b66371cbec54587e1b96a61625b95dbdecd
MD5 93fb7cde9cfa8b90aae51d14bf3e3313
BLAKE2b-256 a8b48bececb3151ebfd3a5bb637f2723a422daf01c6fad95e0cd4f40748e5b62

See more details on using hashes here.

Provenance

The following attestation bundles were made for glasbeyish-0.2.0.tar.gz:

Publisher: package.yml on actapia/glasbeyish

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for glasbeyish-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a4ca5bdedbeee0aaa29f60fe9d81fc49b246cddbab4d06db6b91efd7b61021b5
MD5 1e127d468f1df0a81c65db97c90f2081
BLAKE2b-256 b75a654b80f1a75c75115ddb3aa94e2302c2af881c9af7468deb5003375f9f9c

See more details on using hashes here.

Provenance

The following attestation bundles were made for glasbeyish-0.2.0-py3-none-any.whl:

Publisher: package.yml on actapia/glasbeyish

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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