Skip to content

Commit

Permalink
ColorEdit4: improve components width computation to better distribute…
Browse files Browse the repository at this point in the history
… the error (ocornut#7120)
  • Loading branch information
Nahor committed Dec 13, 2023
1 parent 9d5f628 commit 50cdfc5
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5201,10 +5201,9 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
if ((flags & (ImGuiColorEditFlags_DisplayRGB | ImGuiColorEditFlags_DisplayHSV)) != 0 && (flags & ImGuiColorEditFlags_NoInputs) == 0)
{
// RGB/HSV 0..255 Sliders
const float w_item_one = ImMax(1.0f, IM_TRUNC((w_inputs - (style.ItemInnerSpacing.x) * (components - 1)) / (float)components));
const float w_item_last = ImMax(1.0f, IM_TRUNC(w_inputs - (w_item_one + style.ItemInnerSpacing.x) * (components - 1)));
const float w_items = w_inputs - style.ItemInnerSpacing.x * (components - 1);

const bool hide_prefix = (w_item_one <= CalcTextSize((flags & ImGuiColorEditFlags_Float) ? "M:0.000" : "M:000").x);
const bool hide_prefix = (IM_TRUNC(w_items / components) <= CalcTextSize((flags & ImGuiColorEditFlags_Float) ? "M:0.000" : "M:000").x);
static const char* ids[4] = { "##X", "##Y", "##Z", "##W" };
static const char* fmt_table_int[3][4] =
{
Expand All @@ -5220,11 +5219,14 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
};
const int fmt_idx = hide_prefix ? 0 : (flags & ImGuiColorEditFlags_DisplayHSV) ? 2 : 1;

float prev_split = 0.0f;
for (int n = 0; n < components; n++)
{
if (n > 0)
SameLine(0, style.ItemInnerSpacing.x);
SetNextItemWidth((n + 1 < components) ? w_item_one : w_item_last);
float next_split = IM_TRUNC(w_items * (n + 1) / components);
SetNextItemWidth(next_split - prev_split);
prev_split = next_split;

// FIXME: When ImGuiColorEditFlags_HDR flag is passed HS values snap in weird ways when SV values go below 0.
if (flags & ImGuiColorEditFlags_Float)
Expand Down

0 comments on commit 50cdfc5

Please sign in to comment.