epezent / implot

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

Question: Increasing the number of colors in a ColorMap. #382

Closed kvern-code closed 2 years ago

kvern-code commented 2 years ago

Hello, I have this plot where I am plotting 13 individual data sets.

image

I am using the ImPlotColormap_Deep colormap which only has 10 colors according to the ImPlotColormap_ enum. Is there a way to increase the amount of colors in a color map so that none of the lines are the same color? I only need a few more colors (like 3).

Loving the library, it is amazing. Thank you for the assistance.

CosminPerRam commented 2 years ago

Right now I don't think you can simply add a color to a colormap, but you can create your own ImPlotColormap with how many colors you would like, then add and use it, take a look at AddColormap (from implot.h) here.

Tip: if using a colormap is not mandatory, you can individually set the colors of each line/data set in a plot using

//begin plot
ImPlot::PushStyleColor(ImPlotCol_Fill, <ImVec4 color>);
ImPlot::PlotBars("##MyPlotBars", ...); //or any other plot type, just make sure you get the right color style that you need
ImPlot::PopStyleColor();
//end plot
kvern-code commented 2 years ago

Okay great. I can get some colors and set them manually no problem. I had thought there may be a function to increase the number of colors in the map because the ImPlotColormap_ enum details how many colors each one has, so maybe that could be changed but it appears not.

I tried my hand at creating a colormap using the AddColormap function but I couldn't make sense of what to do. How is an entire colormap generated by supplying a single color? When I tried it my lines were just random colors (with random duplicates of color despite have size > 13).

CosminPerRam commented 2 years ago

The second parameter of AddColormap is a const ImVec4* (or an const ImU32*), which memory-wise can be said to be an array, so, the second parameter is an array of ImVec4 (or ImU32).

When I tried it my lines were just random colors (with random duplicates of color despite have size > 13).

As it is an array, you supplied a single value and you told it that it has a size of 13 (as the third parameter), when it tries to access your array (which is a 1-element array, a single value) it goes out of bounds and picks whatever is at that memory address, so, pretty much random colors!

kvern-code commented 2 years ago

Ahh brilliant. Great explanation. I have implemented the AddColormap function successfully. Thank you for your help!