Skip to main content

Ten Bit Color Maps

Project description

Ten Bit Color Maps

Features

  • 1024 colors in each sequential colormap
  • Perceptually uniform - each color is same perceptual distance from the previous color (before rounding)
  • Raw color data rounded so they can be used in a byte tensor (see pytorch example below)
  • All maps start at black

Installation

pip install tbcm

Usage

With matplotlib

from tbcm import tb_inferno, tb_oleron

then to use tb_inferno, do:

import matplotlib.pyplot as plt
import numpy as np
x,y = np.meshgrid(np.linspace(-1,1,15),np.linspace(-1,1,15))
z = np.cos(x*np.pi)*np.sin(y*np.pi)

fig = plt.figure(figsize=(9,4))
ax1 = fig.add_subplot(121, projection='3d')
ax1.plot_surface(x,y,z,rstride=1,cstride=1,cmap=tb_inferno)
ax2 = fig.add_subplot(122)
cf = ax2.contourf(x,y,z,51,vmin=-1,vmax=1,cmap=tb_inferno)
cbar = fig.colorbar(cf)

result:

matplotlib tb_inferno example

and the same with tb_oleron is:

fig = plt.figure(figsize=(9,4))
ax1 = fig.add_subplot(121, projection='3d')
ax1.plot_surface(x,y,z,rstride=1,cstride=1,cmap=tb_oleron)
ax2 = fig.add_subplot(122)
cf = ax2.contourf(x,y,z,51,vmin=-1,vmax=1,cmap=tb_oleron)
cbar = fig.colorbar(cf)

result:

matplotlib tb_inferno example

With pytorch

First import the color data as lists and convert to byte tensors:

from tbcm import tb_inferno_data, tb_oleron_data

import torch
import numpy as np
from PIL import Image

tb_oleron = torch.tensor(tb_oleron_data, dtype=torch.uint8)
tb_inferno = torch.tensor(tb_inferno_data, dtype=torch.uint8)

Then define this mapping function to convert data to a range appropriate for this colormap:

def map_values(data, cm_range=(0.126, 0.99), invalid=None):
    if invalid is None:
        hist, bin_edges = torch.histogram(data.view(-1), bins=int(1e4))
    else:
        hist, bin_edges = torch.histogram(data[~invalid], bins=int(1e4))
    bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
    cdf = hist.cumsum(dim=0)
    cdf = cdf / cdf[-1]
    out = torch.from_numpy(np.interp(data.numpy(), bin_centers.numpy(), cdf.numpy()))
    scale = cm_range[1] - cm_range[0]
    if invalid is None:
        out = out - out.min()
        out = out * scale / out.max()
    else:
        out = out - out[~invalid].min()
        out = out * scale / out[~invalid].max()
    out = out + cm_range[0]
    return out

Finally use the tb_oleron colormap like:

z = torch.cos(torch.linspace(-1, 1, 256).view(1, -1) * torch.pi) * torch.sin(
    torch.linspace(-1, 1, 256).view(-1, 1) * torch.pi
)

h, w = z.shape
idx = map_values(z).view(-1).mul(1023).round().clamp(0, 1023).long()
img = tb_oleron[idx].view(h, w, 3)
Image.fromarray(img.numpy())

Result:

pytorch tb_inferno example

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

tbcm-0.4.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

tbcm-0.4-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file tbcm-0.4.tar.gz.

File metadata

  • Download URL: tbcm-0.4.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.1

File hashes

Hashes for tbcm-0.4.tar.gz
Algorithm Hash digest
SHA256 0b2b791d4eeaeeb2cded853d20b3778ceabf81b78228f7cf82ad26749bfa7929
MD5 02c92ef27827333769ed45364cc63501
BLAKE2b-256 acbc1e638c5a0f0bc4751e03c9ee4e97f8a4bfffe179ac60034260e031389bb2

See more details on using hashes here.

File details

Details for the file tbcm-0.4-py3-none-any.whl.

File metadata

  • Download URL: tbcm-0.4-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.1

File hashes

Hashes for tbcm-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6a85a4a65970059e07b6e181208ee8c6717bd1c50ff8cea4ded9af4a3446fe35
MD5 a0272d7d073379cc2d1832209b2ae83c
BLAKE2b-256 ce04631e293507750266cc3fbeb9d17f065b1989b979e454c2aa185891c4d9ec

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