Skip to content

colormap-refactor#442

Merged
swharden merged 4 commits intoScottPlot:masterfrom
bclehmann:colormap-refactor
Jun 9, 2020
Merged

colormap-refactor#442
swharden merged 4 commits intoScottPlot:masterfrom
bclehmann:colormap-refactor

Conversation

@bclehmann
Copy link
Member

Purpose:
@swharden's suggestions here: #420 (comment)

New functionality (code):

(byte r, byte g, byte b) = colormapObject.Lookup(0.3); // From intensity
(byte r, byte g, byte b) = colormapObject.Lookup(77); // From byte

New functionality (image):
N/A

@swharden
Copy link
Member

swharden commented Jun 8, 2020

I spent all weekend working on colormaps in a development branch of my spectrogram library. You may find this page interesting: https://github.com/swharden/Spectrogram/tree/1.2#colormaps

Once I get that finalized, I'll loop back around to ScottPlot and work on this colormap module. I think some of the things I'm learning in Spectrogram's colormaps will translate well here. I thought I'd drop a couple notes for reference:

class structure

I like having a Colormap class which takes an IColormap as input, but also has static Colormap properties like Viridis, Jet, etc. This lets you instantiate a colormap with

// a standard colormap
Colormap cmap1 = Colormap.Viridis;

// a custom colormap
IColormap customCmap = new CustomCmap(); // custom implementation
Colormap cmap2 = Colormap(customCmap);

The biggest advantage here is intellisense gives you a list of colormaps as you start typing Colormap., while avoiding enum types entirely.

See https://github.com/swharden/Spectrogram/blob/1.2/src/Spectrogram/Colormap.cs

storing values

Storing RGB as an int seems simplest, where the lowest 3 bytes of the int correspond to the RGB values. See: https://github.com/swharden/Spectrogram/blob/1.2/src/Spectrogram/Colormaps/Viridis.cs

matplotlib colormaps

For reference, mant of matplotlib's colormaps are here, including magma, inferno, plasma, viridis, cividis, twilight, and turbo

https://github.com/matplotlib/matplotlib/blob/4f42c6825fe311f18812f99701701cb484da7670/lib/matplotlib/_cm_listed.py

Note that in-between colors can be calculated if desired

@bclehmann
Copy link
Member Author

I like your structure more, enums seem clunky for this one. As for storing them I don't have any preference between byte arrays or ints, bytes are more clear in source code, but System.Drawing uses ARGB ints, so you can save converting. If you want to have users make custom colourmaps then it may be more intuitive to use byte arrays, but it isn't difficult to convert between.

@bclehmann
Copy link
Member Author

Also, calculating in-between colours seems unnecessary, as colourmaps are defined for 256 steps, which means adding more between those steps will be rounded away (on an sRGB monitor) unless the colourmap has very uneven steps, in which case it will be indistinguishable to the human eye.

@swharden
Copy link
Member

swharden commented Jun 8, 2020

As for storing them

Actually, storage implementation (storing byte arrays, integers, or a formula) is handled in the colormap class itself (e.g., Viridis.cs) so developers can choose any storage mechanism they like.

The reasons I found int[] useful is because I can copy/paste it between languages (like Python) without worrying about syntax. It takes less memory than floating point options, and is also more concise on the page.

Also, calculating in-between colours seems unnecessary

Good point! A corner case may be some TIFs store pixel intensity as 32-bit values or even floating-point numbers, but I'll ignore that and agree we can keep things simple by assuming we intend to produce 8-bit RGB images.

@swharden swharden merged commit 1594989 into ScottPlot:master Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants