Skip to content

Commit 32c33c6

Browse files
rokupsocornut
authored andcommitted
ColorEdit: Preserve last saturation value when V=0. Disable Hue editing lock.
This workaround is no longer necessary because preserving hue value prevents it from resetting when it is edited in said condition.
1 parent 5e2329b commit 32c33c6

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

imgui_internal.h

+1
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,7 @@ struct ImGuiContext
11211121
ImGuiID TempInputTextId; // Temporary text input when CTRL+clicking on a slider, etc.
11221122
ImGuiColorEditFlags ColorEditOptions; // Store user options for color edit widgets
11231123
float ColorEditLastHue; // Backup of last Hue associated to LastColor[3], so we can restore Hue in lossy RGB<>HSV round trips
1124+
float ColorEditLastSaturation; // Backup of last Saturation associated to LastColor[3], so we can restore Saturation in lossy RGB<>HSV round trips
11241125
float ColorEditLastColor[3];
11251126
ImVec4 ColorPickerRef; // Initial/reference color at the time of opening the color picker.
11261127
bool DragCurrentAccumDirty;

imgui_widgets.cpp

+26-10
Original file line numberDiff line numberDiff line change
@@ -4222,8 +4222,13 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
42224222
{
42234223
// Hue is lost when converting from greyscale rgb (saturation=0). Restore it.
42244224
ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]);
4225-
if (f[1] == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0)
4226-
f[0] = g.ColorEditLastHue;
4225+
if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0)
4226+
{
4227+
if (f[1] == 0)
4228+
f[0] = g.ColorEditLastHue;
4229+
if (f[2] == 0)
4230+
f[1] = g.ColorEditLastSaturation;
4231+
}
42274232
}
42284233
int i[4] = { IM_F32_TO_INT8_UNBOUND(f[0]), IM_F32_TO_INT8_UNBOUND(f[1]), IM_F32_TO_INT8_UNBOUND(f[2]), IM_F32_TO_INT8_UNBOUND(f[3]) };
42294234

@@ -4262,16 +4267,15 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
42624267
SameLine(0, style.ItemInnerSpacing.x);
42634268
SetNextItemWidth((n + 1 < components) ? w_item_one : w_item_last);
42644269

4265-
// Disable Hue edit when Saturation is zero
4266-
const bool disable_hue_edit = (n == 0 && (flags & ImGuiColorEditFlags_DisplayHSV) && i[1] == 0);
4270+
// FIXME: When ImGuiColorEditFlags_HDR flag is passed HS values snap in weird ways when SV values go below 0.
42674271
if (flags & ImGuiColorEditFlags_Float)
42684272
{
4269-
value_changed |= DragFloat(ids[n], &f[n], 1.0f/255.0f, disable_hue_edit ? +FLT_MAX : 0.0f, disable_hue_edit ? -FLT_MAX : hdr ? 0.0f : 1.0f, fmt_table_float[fmt_idx][n]);
4273+
value_changed |= DragFloat(ids[n], &f[n], 1.0f/255.0f, 0.0f, hdr ? 0.0f : 1.0f, fmt_table_float[fmt_idx][n]);
42704274
value_changed_as_float |= value_changed;
42714275
}
42724276
else
42734277
{
4274-
value_changed |= DragInt(ids[n], &i[n], 1.0f, disable_hue_edit ? INT_MAX : 0, disable_hue_edit ? INT_MIN : hdr ? 0 : 255, fmt_table_int[fmt_idx][n]);
4278+
value_changed |= DragInt(ids[n], &i[n], 1.0f, 0, hdr ? 0 : 255, fmt_table_int[fmt_idx][n]);
42754279
}
42764280
if (!(flags & ImGuiColorEditFlags_NoOptions))
42774281
OpenPopupOnItemClick("context");
@@ -4354,6 +4358,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
43544358
if ((flags & ImGuiColorEditFlags_DisplayHSV) && (flags & ImGuiColorEditFlags_InputRGB))
43554359
{
43564360
g.ColorEditLastHue = f[0];
4361+
g.ColorEditLastSaturation = f[1];
43574362
ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);
43584363
memcpy(g.ColorEditLastColor, f, sizeof(float) * 3);
43594364
}
@@ -4536,8 +4541,13 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
45364541
{
45374542
// Hue is lost when converting from greyscale rgb (saturation=0). Restore it.
45384543
ColorConvertRGBtoHSV(R, G, B, H, S, V);
4539-
if (S == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0)
4540-
H = g.ColorEditLastHue;
4544+
if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0)
4545+
{
4546+
if (S == 0)
4547+
H = g.ColorEditLastHue;
4548+
if (V == 0)
4549+
S = g.ColorEditLastSaturation;
4550+
}
45414551
}
45424552
else if (flags & ImGuiColorEditFlags_InputHSV)
45434553
{
@@ -4665,6 +4675,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
46654675
{
46664676
ColorConvertHSVtoRGB(H >= 1.0f ? H - 10 * 1e-6f : H, S > 0.0f ? S : 10*1e-6f, V > 0.0f ? V : 1e-6f, col[0], col[1], col[2]);
46674677
g.ColorEditLastHue = H;
4678+
g.ColorEditLastSaturation = S;
46684679
memcpy(g.ColorEditLastColor, col, sizeof(float) * 3);
46694680
}
46704681
else if (flags & ImGuiColorEditFlags_InputHSV)
@@ -4719,8 +4730,13 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
47194730
G = col[1];
47204731
B = col[2];
47214732
ColorConvertRGBtoHSV(R, G, B, H, S, V);
4722-
if (S == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) // Fix local Hue as display below will use it immediately.
4723-
H = g.ColorEditLastHue;
4733+
if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) // Fix local Hue as display below will use it immediately.
4734+
{
4735+
if (S == 0)
4736+
H = g.ColorEditLastHue;
4737+
if (V == 0)
4738+
S = g.ColorEditLastSaturation;
4739+
}
47244740
}
47254741
else if (flags & ImGuiColorEditFlags_InputHSV)
47264742
{

0 commit comments

Comments
 (0)