<filename>src/data/generate_colormap_data.py
import numpy as np
import matplotlib.pyplot as plt
from collections import OrderedDict

cmaps = OrderedDict()

cmaps['Perceptually Uniform Sequential'] = [
            'viridis', 'plasma', 'inferno', 'magma', 'cividis']

cmaps['Sequential'] = [
            'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
            'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
            'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn', 'gray']

cmaps['Diverging'] = [
            'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu',
            'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']

cmaps['Cyclic'] = ['twilight', 'twilight_shifted', 'hsv']

cmaps['Qualitative'] = ['Pastel1', 'Pastel2', 'Paired', 'Accent',
                        'Dark2', 'Set1', 'Set2', 'Set3',
                        'tab10', 'tab20', 'tab20b', 'tab20c']

cmaps['Miscellaneous'] = [
            'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern',
            'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg',
            'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar']

def f32_to_u08(x):
    i = int(x*255.0)
    if i < 0:
        return 0
    elif i > 255:
        return 255
    else:
        return i

print('// This file is automatically generated.')
print('// Colormap data is obtained from Matplotlib:')
print('// https://matplotlib.org/tutorials/colors/colormaps.html')

print("""
struct vdbColormapData
{
    const char *name;
    int num_colors;
    GLuint gl_texture;
    unsigned char colors[3*1024];
};""")

print('#define NUM_COLORMAPS (sizeof(vdb_colormap_data)/sizeof(vdb_colormap_data[0]))')

print("static vdbColormapData vdb_colormap_data[] = \n{")
for cmap_category, names in cmaps.items():
    for name in names:
        if name == 'gray':
            r = np.arange(256)/255.0
            colors = np.vstack((r,r,r)).T
        else:
            cmap = plt.cm.get_cmap(name)
            if hasattr(cmap, 'colors'):
                colors = cmap.colors
            else:
                colors = cmap(np.linspace(0.0,1.0,256))
                colors = colors[:,:3]
        print('    {')
        print('        \"%s\",' % name)
        print('        %d,' % len(colors))
        print('        0,')
        for r,g,b in colors:
            r8 = f32_to_u08(r)
            g8 = f32_to_u08(g)
            b8 = f32_to_u08(b)
            print('        %d, %d, %d,' % (r8,g8,b8))
        print('    },')
        num_colormaps += 1
print("};")


<filename>src/data/generate_colormap_data.py
import numpy as np
import matplotlib.pyplot as plt
from collections import OrderedDict

cmaps = OrderedDict()

cmaps['Perceptually Uniform Sequential'] = [
            'viridis', 'plasma', 'inferno', 'magma', 'cividis']

cmaps['Sequential'] = [
            'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
            'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
            'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn', 'gray']

cmaps['Diverging'] = [
            'PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu',
            'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']

cmaps['Cyclic'] = ['twilight', 'twilight_shifted', 'hsv']

cmaps['Qualitative'] = ['Pastel1', 'Pastel2', 'Paired', 'Accent',
                        'Dark2', 'Set1', 'Set2', 'Set3',
                        'tab10', 'tab20', 'tab20b', 'tab20c']

cmaps['Miscellaneous'] = [
            'flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern',
            'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg',
            'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar']

def f32_to_u08(x):
    i = int(x*255.0)
    if i < 0:
        return 0
    elif i > 255:
        return 255
    else:
        return i

print('// This file is automatically generated.')
print('// Colormap data is obtained from Matplotlib:')
print('// https://matplotlib.org/tutorials/colors/colormaps.html')

print("""
struct vdbColormapData
{
    const char *name;
    int num_colors;
    GLuint gl_texture;
    unsigned char colors[3*1024];
};""")

print('#define NUM_COLORMAPS (sizeof(vdb_colormap_data)/sizeof(vdb_colormap_data[0]))')

print("static vdbColormapData vdb_colormap_data[] = \n{")
for cmap_category, names in cmaps.items():
    for name in names:
        if name == 'gray':
            r = np.arange(256)/255.0
            colors = np.vstack((r,r,r)).T
        else:
            cmap = plt.cm.get_cmap(name)
            if hasattr(cmap, 'colors'):
                colors = cmap.colors
            else:
                colors = cmap(np.linspace(0.0,1.0,256))
                colors = colors[:,:3]
        print('    {')
        print('        \"%s\",' % name)
        print('        %d,' % len(colors))
        print('        0,')
        for r,g,b in colors:
            r8 = f32_to_u08(r)
            g8 = f32_to_u08(g)
            b8 = f32_to_u08(b)
            print('        %d, %d, %d,' % (r8,g8,b8))
        print('    },')
        num_colormaps += 1
print("};")


struct vdbColormapData {     const char *name;     int num_colors;     GLuint gl_texture;     unsigned char colors[3*1024]; };