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.5.tar.gz (11.0 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.5-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tbcm-0.5.tar.gz
Algorithm Hash digest
SHA256 9bdb39b69f1c9c2a8446f6c08c9fda61465dc7a43472b8eb2241c127feb489bc
MD5 6c7ff5605636c1ade53615b9ace130c8
BLAKE2b-256 9d6500e15047aea43221594640b5ae60f81214c8d6250e4f5abc9c39f5e38f95

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tbcm-0.5-py3-none-any.whl
  • Upload date:
  • Size: 10.1 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1e85d8eff29e33a61db6ce40afedd003a49beec60d29c7771c3aa0decd3b885a
MD5 846f91d8bc54b21ae6c3af38d1106f60
BLAKE2b-256 2597f3119fb04fbafec772a67a28d6e73e28d87340999eb2b1ffc200387ae9d7

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