From 6c00109636a78cd317de2b2a02744deec523b181 Mon Sep 17 00:00:00 2001 From: Evan Pezent Date: Mon, 20 Jun 2022 07:48:51 -0500 Subject: [PATCH] make ColormapScale reversable --- implot.cpp | 13 ++++++++----- implot.h | 4 ++-- implot_demo.cpp | 4 +++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/implot.cpp b/implot.cpp index 47ffd02..5f372f6 100644 --- a/implot.cpp +++ b/implot.cpp @@ -4472,7 +4472,7 @@ void ColormapScale(const char* label, double scale_min, double scale_max, const if (frame_size.y < gp.Style.PlotMinSize.y && size.y < 0.0f) frame_size.y = gp.Style.PlotMinSize.y; - ImPlotRange range(scale_min,scale_max); + ImPlotRange range(ImMin(scale_min,scale_max), ImMax(scale_min,scale_max)); gp.CTicker.Reset(); Locator_Default(gp.CTicker, range, frame_size.y, true, Formatter_Default, (void*)format); @@ -4497,6 +4497,9 @@ void ColormapScale(const char* label, double scale_min, double scale_max, const ImGui::RenderFrame(bb_frame.Min, bb_frame.Max, GetStyleColorU32(ImPlotCol_FrameBg), true, G.Style.FrameRounding); const bool opposite = ImHasFlag(flags, ImPlotColormapScaleFlags_Opposite); + const bool inverted = ImHasFlag(flags, ImPlotColormapScaleFlags_Invert); + const bool reversed = scale_min > scale_max; + float bb_grad_shift = opposite ? pad : 0; ImRect bb_grad(bb_frame.Min + gp.Style.PlotPadding + ImVec2(bb_grad_shift, 0), bb_frame.Min + ImVec2(bar_w + gp.Style.PlotPadding.x + bb_grad_shift, @@ -4505,11 +4508,11 @@ void ColormapScale(const char* label, double scale_min, double scale_max, const ImGui::PushClipRect(bb_frame.Min, bb_frame.Max, true); const ImU32 col_text = ImGui::GetColorU32(ImGuiCol_Text); - const bool invert = ImHasFlag(flags, ImPlotColormapScaleFlags_Invert); - const float y_min = invert ? bb_grad.Max.y : bb_grad.Min.y; - const float y_max = invert ? bb_grad.Min.y : bb_grad.Max.y; + const bool invert_scale = inverted ? (reversed ? false : true) : (reversed ? true : false); + const float y_min = invert_scale ? bb_grad.Max.y : bb_grad.Min.y; + const float y_max = invert_scale ? bb_grad.Min.y : bb_grad.Max.y; - RenderColorBar(gp.ColormapData.GetKeys(cmap), gp.ColormapData.GetKeyCount(cmap), DrawList, bb_grad, true, !invert, !gp.ColormapData.IsQual(cmap)); + RenderColorBar(gp.ColormapData.GetKeys(cmap), gp.ColormapData.GetKeyCount(cmap), DrawList, bb_grad, true, !inverted, !gp.ColormapData.IsQual(cmap)); for (int i = 0; i < gp.CTicker.TickCount(); ++i) { const double y_pos_plt = gp.CTicker.Ticks[i].PlotPos; const float y_pos = ImRemap((float)y_pos_plt, (float)range.Max, (float)range.Min, y_min, y_max); diff --git a/implot.h b/implot.h index ba8e154..2c07664 100644 --- a/implot.h +++ b/implot.h @@ -212,7 +212,7 @@ enum ImPlotColormapScaleFlags_ { ImPlotColormapScaleFlags_None = 0, // default ImPlotColormapScaleFlags_NoLabel = 1 << 0, // the colormap axis label will not be displayed ImPlotColormapScaleFlags_Opposite = 1 << 1, // render the colormap label and tick labels on the opposite side - ImPlotColormapScaleFlags_Invert = 1 << 2, // invert the colormap axis scale + ImPlotColormapScaleFlags_Invert = 1 << 2, // invert the colormap bar and axis scale (this only affects rendering; if you only want to reverse the scale mapping, make scale_min > scale_max) }; // Flags for ANY PlotX function @@ -1164,7 +1164,7 @@ IMPLOT_API ImVec4 GetColormapColor(int idx, ImPlotColormap cmap = IMPLOT_AUTO); // Sample a color from the current colormap given t between 0 and 1. IMPLOT_API ImVec4 SampleColormap(float t, ImPlotColormap cmap = IMPLOT_AUTO); -// Shows a vertical color scale with linear spaced ticks using the specified color map. Use double hashes to hide label (e.g. "##NoLabel"). +// Shows a vertical color scale with linear spaced ticks using the specified color map. Use double hashes to hide label (e.g. "##NoLabel"). If scale_min > scale_max, the scale to color mapping will be reversed. IMPLOT_API void ColormapScale(const char* label, double scale_min, double scale_max, const ImVec2& size = ImVec2(0,0), const char* format = "%g", ImPlotColormapScaleFlags flags = 0, ImPlotColormap cmap = IMPLOT_AUTO); // Shows a horizontal slider with a colormap gradient background. Optionally returns the color sampled at t in [0 1]. IMPLOT_API bool ColormapSlider(const char* label, float* t, ImVec4* out = NULL, const char* format = "", ImPlotColormap cmap = IMPLOT_AUTO); diff --git a/implot_demo.cpp b/implot_demo.cpp index 22545a7..f3c52d3 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -2076,7 +2076,9 @@ void Demo_ColormapWidgets() { ImPlot::ColormapIcon(cmap); ImGui::SameLine(); ImGui::Text("Icon"); static ImPlotColormapScaleFlags flags = 0; - ImPlot::ColormapScale("Scale",0,100,ImVec2(0,0),"%g dB",flags,cmap); + static float scale[2] = {0, 100}; + ImPlot::ColormapScale("Scale",scale[0],scale[1],ImVec2(0,0),"%g dB",flags,cmap); + ImGui::InputFloat2("Scale",scale); CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_NoLabel); CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_Opposite); CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_Invert);