epezent / implot

Immediate Mode Plotting
MIT License
4.65k stars 517 forks source link

Struggling with creating new colormap for heatmap #367

Closed yassinetb closed 2 years ago

yassinetb commented 2 years ago

I am trying to create a new colormap for a heatmap plot and am struggling to get the desired colors. I for some reason don't understand how the ImPlot::AddColormap method functions. Some example code is given in implot_demo, but I struggle to understand how the colors are obtained on that color map as the hex color (4282515870 etc..) equivalent (when I look up hex to RGB) seem to be of different colors than the ones obtained on the implot_demo colormap. Here is the code snippet on implot_demo for convenience (line 489):

static ImPlotColormap Liars = -1;
    if (Liars == -1) {
        static const ImU32 Liars_Data[6] = { 4282515870, 4282609140, 4287357182, 4294630301, 4294945280, 4294921472 };
        Liars = ImPlot::AddColormap("Liars", Liars_Data, 6);
    }

I am now simply trying to have three colors on my colormap: black, red and green - could you help on how to use the function or help with code to implement this simple colormap?

Thank you!

marcizhu commented 2 years ago

These numbers represent the colors in RGBA format, not RGB. The "A" is the alpha channel, aka the opacity of the color. An alpha of 255 would make it 100% opaque and an alpha of 0 would make it totally transparent. If you only have RGB values, you could easily make your colormap using the macro IM_COL32(R, G, B, A), where R, G, B and A are the red, green, blue and alpha channels, respectively. You can set A to 255 and it should work:

// This colormap has 3 colors: the first is pure red, the second is pure green and the third is pure blue
static const ImU32 MyColormap[] = { IM_COL32(255, 0, 0, 255), IM_COL32(0, 255, 0, 255), IM_COL32(0, 0, 255, 255) };

As an additional example, the colormap you asked for (with black, red and green colors, in that order) would be something like this:

static const ImU32 SimpleColormap[3] = { IM_COL32(0, 0, 0, 255), IM_COL32(255, 0, 0, 255), IM_COL32(0, 255, 0, 255) };
Liars = ImPlot::AddColormap("Simple", SimpleColormap, 3);

Hope that helps :)