Skip to content

Commit

Permalink
Merge pull request #106 from andrew-s28/inverted-cmaps
Browse files Browse the repository at this point in the history
Adds functionality for inverting colormap lightness
  • Loading branch information
kthyng authored Apr 5, 2024
2 parents 64773a9 + 12cd24e commit 05a6a9f
Show file tree
Hide file tree
Showing 25 changed files with 11,303 additions and 3 deletions.
20 changes: 20 additions & 0 deletions cmocean/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,25 @@ def _register_cmap(cmap, *, name):
reg_map_r = colors.ListedColormap(rgb_with_alpha[::-1,:], N=rgb.shape[0])
_register_cmap(reg_map_r, name=f'cmo.{cmapname}_r')

# Load inverted cmaps
rgb_i = np.loadtxt(os.path.join(datadir, 'inverted', cmapname + '_i-rgb.txt'))
cmap_d[cmapname + '_i'] = tools.cmap(rgb_i, N=256)
cmap_d[cmapname + '_i'].name = cmapname + '_i'
cmap_d[cmapname + '_r_i'] = tools.cmap(rgb_i[::-1, :], N=256)
cmap_d[cmapname + '_r_i'].name = cmapname + '_r_i'
rgb_with_alpha = np.zeros((rgb_i.shape[0],4))
rgb_with_alpha[:,:3] = rgb_i
rgb_with_alpha[:,3] = 1. #set alpha channel to 1

# Register inverted cmaps
reg_map_i = colors.ListedColormap(rgb_with_alpha, N=rgb_i.shape[0])
_register_cmap(reg_map_i, name=f'cmo.{cmapname}_i')
reg_map_r_i = colors.ListedColormap(rgb_with_alpha[::-1,:], N=rgb_i.shape[0])
_register_cmap(reg_map_r_i, name=f'cmo.{cmapname}_r_i')

# order shouldn't matter
cmap_d[cmapname + '_i_r'] = cmap_d[cmapname + '_r_i']
_register_cmap(reg_map_r_i, name=f'cmo.{cmapname}_i_r')

# make colormaps available to call
locals().update(cmap_d)
6 changes: 3 additions & 3 deletions cmocean/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def plot_lightness(saveplot=False):
fig = plt.figure(figsize=(16, 5))
ax = fig.add_subplot(111)
fig.subplots_adjust(left=0.03, right=0.97)
ax.set_xlim(-0.1, len(cm.cmap_d)/2. + 0.1)
ax.set_xlim(-0.1, len(cm.cmap_d)/5. + 0.1)
ax.set_ylim(0, 100)
ax.set_xlabel('Lightness for each colormap', fontsize=14)

Expand Down Expand Up @@ -75,12 +75,12 @@ def plot_gallery(saveplot=False):
gradient = np.vstack((gradient, gradient))
x = np.linspace(0.0, 1.0, 256)

fig, axes = plt.subplots(nrows=int(len(cm.cmap_d)/2), ncols=1, figsize=(6, 12))
fig, axes = plt.subplots(nrows=int(len(cm.cmap_d)/5), ncols=1, figsize=(6, 12))
fig.subplots_adjust(top=0.99, bottom=0.01, left=0.2, right=0.99, wspace=0.05)

for ax, cmapname in zip(axes, cm.cmapnames):

if '_r' in cmapname: # skip reversed versions for plot
if '_r' in cmapname or '_i' in cmapname: # skip reversed versions for plot
continue

cmap = cm.cmap_d[cmapname] # get the colormap instance
Expand Down
512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/algae_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/amp_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/balance_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/curl_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/deep_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/delta_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/dense_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/diff_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/gray_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/haline_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/ice_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/matter_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/oxy_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/phase_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/rain_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/solar_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/speed_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/tarn_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/tempo_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/thermal_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/topo_i-rgb.txt

Large diffs are not rendered by default.

512 changes: 512 additions & 0 deletions cmocean/rgb/inverted/turbid_i-rgb.txt

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ Reversed versions of all colormaps are available by appending "_r" to the colorm
cmocean.plots.test(cmocean.cm.gray_r, ax=ax)
fig.tight_layout()

Inverted lightness versions of all colormaps are available by appending "_i" to the colormap name. This is particularly useful for diverging colormaps used on light backgrounds:

.. plot::
:include-source:

import cmocean
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(8, 3))
ax = fig.add_subplot(1, 2, 1)
cmocean.plots.test(cmocean.cm.balance, ax=ax)
ax = fig.add_subplot(1, 2, 2)
cmocean.plots.test(cmocean.cm.balance_i, ax=ax)
fig.tight_layout()

Reversed and inverted lightness versions of all colormaps are available by appending "_r_i" or "_i_r" to the colormap name, e.g., ``cmocean.cm.balance_i_r``.

You can lighten a colormap using an alpha value below 1 with the `cmocean.tools.lighten()` function so that you can overlay contours and other lines that are more easily visible:

Expand Down

0 comments on commit 05a6a9f

Please sign in to comment.