1
0
Fork 0
mirror of https://github.com/gwm17/implot.git synced 2024-11-26 12:18:52 -05:00

make ColormapScale reversable

This commit is contained in:
Evan Pezent 2022-06-20 07:48:51 -05:00
parent 0f4d4dccc3
commit 6c00109636
3 changed files with 13 additions and 8 deletions

View File

@ -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) if (frame_size.y < gp.Style.PlotMinSize.y && size.y < 0.0f)
frame_size.y = gp.Style.PlotMinSize.y; 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(); gp.CTicker.Reset();
Locator_Default(gp.CTicker, range, frame_size.y, true, Formatter_Default, (void*)format); 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); ImGui::RenderFrame(bb_frame.Min, bb_frame.Max, GetStyleColorU32(ImPlotCol_FrameBg), true, G.Style.FrameRounding);
const bool opposite = ImHasFlag(flags, ImPlotColormapScaleFlags_Opposite); 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; float bb_grad_shift = opposite ? pad : 0;
ImRect bb_grad(bb_frame.Min + gp.Style.PlotPadding + ImVec2(bb_grad_shift, 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, 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); ImGui::PushClipRect(bb_frame.Min, bb_frame.Max, true);
const ImU32 col_text = ImGui::GetColorU32(ImGuiCol_Text); const ImU32 col_text = ImGui::GetColorU32(ImGuiCol_Text);
const bool invert = ImHasFlag(flags, ImPlotColormapScaleFlags_Invert); const bool invert_scale = inverted ? (reversed ? false : true) : (reversed ? true : false);
const float y_min = invert ? bb_grad.Max.y : bb_grad.Min.y; const float y_min = invert_scale ? bb_grad.Max.y : bb_grad.Min.y;
const float y_max = invert ? bb_grad.Min.y : bb_grad.Max.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) { for (int i = 0; i < gp.CTicker.TickCount(); ++i) {
const double y_pos_plt = gp.CTicker.Ticks[i].PlotPos; 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); const float y_pos = ImRemap((float)y_pos_plt, (float)range.Max, (float)range.Min, y_min, y_max);

View File

@ -212,7 +212,7 @@ enum ImPlotColormapScaleFlags_ {
ImPlotColormapScaleFlags_None = 0, // default ImPlotColormapScaleFlags_None = 0, // default
ImPlotColormapScaleFlags_NoLabel = 1 << 0, // the colormap axis label will not be displayed 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_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 // 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. // Sample a color from the current colormap given t between 0 and 1.
IMPLOT_API ImVec4 SampleColormap(float t, ImPlotColormap cmap = IMPLOT_AUTO); 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); 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]. // 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); IMPLOT_API bool ColormapSlider(const char* label, float* t, ImVec4* out = NULL, const char* format = "", ImPlotColormap cmap = IMPLOT_AUTO);

View File

@ -2076,7 +2076,9 @@ void Demo_ColormapWidgets() {
ImPlot::ColormapIcon(cmap); ImGui::SameLine(); ImGui::Text("Icon"); ImPlot::ColormapIcon(cmap); ImGui::SameLine(); ImGui::Text("Icon");
static ImPlotColormapScaleFlags flags = 0; 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_NoLabel);
CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_Opposite); CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_Opposite);
CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_Invert); CHECKBOX_FLAG(flags, ImPlotColormapScaleFlags_Invert);