Skip to content

Commit 05a6a9f

Browse files
authored
Merge pull request #106 from andrew-s28/inverted-cmaps
Adds functionality for inverting colormap lightness
2 parents 64773a9 + 12cd24e commit 05a6a9f

25 files changed

+11303
-3
lines changed

cmocean/cm.py

+20
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,25 @@ def _register_cmap(cmap, *, name):
6060
reg_map_r = colors.ListedColormap(rgb_with_alpha[::-1,:], N=rgb.shape[0])
6161
_register_cmap(reg_map_r, name=f'cmo.{cmapname}_r')
6262

63+
# Load inverted cmaps
64+
rgb_i = np.loadtxt(os.path.join(datadir, 'inverted', cmapname + '_i-rgb.txt'))
65+
cmap_d[cmapname + '_i'] = tools.cmap(rgb_i, N=256)
66+
cmap_d[cmapname + '_i'].name = cmapname + '_i'
67+
cmap_d[cmapname + '_r_i'] = tools.cmap(rgb_i[::-1, :], N=256)
68+
cmap_d[cmapname + '_r_i'].name = cmapname + '_r_i'
69+
rgb_with_alpha = np.zeros((rgb_i.shape[0],4))
70+
rgb_with_alpha[:,:3] = rgb_i
71+
rgb_with_alpha[:,3] = 1. #set alpha channel to 1
72+
73+
# Register inverted cmaps
74+
reg_map_i = colors.ListedColormap(rgb_with_alpha, N=rgb_i.shape[0])
75+
_register_cmap(reg_map_i, name=f'cmo.{cmapname}_i')
76+
reg_map_r_i = colors.ListedColormap(rgb_with_alpha[::-1,:], N=rgb_i.shape[0])
77+
_register_cmap(reg_map_r_i, name=f'cmo.{cmapname}_r_i')
78+
79+
# order shouldn't matter
80+
cmap_d[cmapname + '_i_r'] = cmap_d[cmapname + '_r_i']
81+
_register_cmap(reg_map_r_i, name=f'cmo.{cmapname}_i_r')
82+
6383
# make colormaps available to call
6484
locals().update(cmap_d)

cmocean/plots.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def plot_lightness(saveplot=False):
2525
fig = plt.figure(figsize=(16, 5))
2626
ax = fig.add_subplot(111)
2727
fig.subplots_adjust(left=0.03, right=0.97)
28-
ax.set_xlim(-0.1, len(cm.cmap_d)/2. + 0.1)
28+
ax.set_xlim(-0.1, len(cm.cmap_d)/5. + 0.1)
2929
ax.set_ylim(0, 100)
3030
ax.set_xlabel('Lightness for each colormap', fontsize=14)
3131

@@ -75,12 +75,12 @@ def plot_gallery(saveplot=False):
7575
gradient = np.vstack((gradient, gradient))
7676
x = np.linspace(0.0, 1.0, 256)
7777

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

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

83-
if '_r' in cmapname: # skip reversed versions for plot
83+
if '_r' in cmapname or '_i' in cmapname: # skip reversed versions for plot
8484
continue
8585

8686
cmap = cm.cmap_d[cmapname] # get the colormap instance

cmocean/rgb/inverted/algae_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/amp_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/balance_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/curl_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/deep_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/delta_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/dense_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/diff_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/gray_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/haline_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/ice_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/matter_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/oxy_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/phase_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/rain_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/solar_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/speed_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/tarn_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/tempo_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/thermal_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/topo_i-rgb.txt

+512
Large diffs are not rendered by default.

cmocean/rgb/inverted/turbid_i-rgb.txt

+512
Large diffs are not rendered by default.

docs/source/index.rst

+16
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ Reversed versions of all colormaps are available by appending "_r" to the colorm
133133
cmocean.plots.test(cmocean.cm.gray_r, ax=ax)
134134
fig.tight_layout()
135135

136+
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:
137+
138+
.. plot::
139+
:include-source:
140+
141+
import cmocean
142+
import matplotlib.pyplot as plt
143+
144+
fig = plt.figure(figsize=(8, 3))
145+
ax = fig.add_subplot(1, 2, 1)
146+
cmocean.plots.test(cmocean.cm.balance, ax=ax)
147+
ax = fig.add_subplot(1, 2, 2)
148+
cmocean.plots.test(cmocean.cm.balance_i, ax=ax)
149+
fig.tight_layout()
150+
151+
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``.
136152

137153
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:
138154

0 commit comments

Comments
 (0)