Skip to content

Commit

Permalink
ColorEdit: Preserve last saturation value when V=0. Disable Hue editi…
Browse files Browse the repository at this point in the history
…ng lock.

This workaround is no longer necessary because preserving hue value prevents it from resetting when it is edited in said condition.
  • Loading branch information
rokups authored and ocornut committed Jan 7, 2020
1 parent 5e2329b commit 32c33c6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ struct ImGuiContext
ImGuiID TempInputTextId; // Temporary text input when CTRL+clicking on a slider, etc.
ImGuiColorEditFlags ColorEditOptions; // Store user options for color edit widgets
float ColorEditLastHue; // Backup of last Hue associated to LastColor[3], so we can restore Hue in lossy RGB<>HSV round trips
float ColorEditLastSaturation; // Backup of last Saturation associated to LastColor[3], so we can restore Saturation in lossy RGB<>HSV round trips
float ColorEditLastColor[3];
ImVec4 ColorPickerRef; // Initial/reference color at the time of opening the color picker.
bool DragCurrentAccumDirty;
Expand Down
36 changes: 26 additions & 10 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4222,8 +4222,13 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
{
// Hue is lost when converting from greyscale rgb (saturation=0). Restore it.
ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]);
if (f[1] == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0)
f[0] = g.ColorEditLastHue;
if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0)
{
if (f[1] == 0)
f[0] = g.ColorEditLastHue;
if (f[2] == 0)
f[1] = g.ColorEditLastSaturation;
}
}
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]) };

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

// Disable Hue edit when Saturation is zero
const bool disable_hue_edit = (n == 0 && (flags & ImGuiColorEditFlags_DisplayHSV) && i[1] == 0);
// FIXME: When ImGuiColorEditFlags_HDR flag is passed HS values snap in weird ways when SV values go below 0.
if (flags & ImGuiColorEditFlags_Float)
{
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]);
value_changed |= DragFloat(ids[n], &f[n], 1.0f/255.0f, 0.0f, hdr ? 0.0f : 1.0f, fmt_table_float[fmt_idx][n]);
value_changed_as_float |= value_changed;
}
else
{
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]);
value_changed |= DragInt(ids[n], &i[n], 1.0f, 0, hdr ? 0 : 255, fmt_table_int[fmt_idx][n]);
}
if (!(flags & ImGuiColorEditFlags_NoOptions))
OpenPopupOnItemClick("context");
Expand Down Expand Up @@ -4354,6 +4358,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
if ((flags & ImGuiColorEditFlags_DisplayHSV) && (flags & ImGuiColorEditFlags_InputRGB))
{
g.ColorEditLastHue = f[0];
g.ColorEditLastSaturation = f[1];
ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);
memcpy(g.ColorEditLastColor, f, sizeof(float) * 3);
}
Expand Down Expand Up @@ -4536,8 +4541,13 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
{
// Hue is lost when converting from greyscale rgb (saturation=0). Restore it.
ColorConvertRGBtoHSV(R, G, B, H, S, V);
if (S == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0)
H = g.ColorEditLastHue;
if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0)
{
if (S == 0)
H = g.ColorEditLastHue;
if (V == 0)
S = g.ColorEditLastSaturation;
}
}
else if (flags & ImGuiColorEditFlags_InputHSV)
{
Expand Down Expand Up @@ -4665,6 +4675,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
{
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]);
g.ColorEditLastHue = H;
g.ColorEditLastSaturation = S;
memcpy(g.ColorEditLastColor, col, sizeof(float) * 3);
}
else if (flags & ImGuiColorEditFlags_InputHSV)
Expand Down Expand Up @@ -4719,8 +4730,13 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
G = col[1];
B = col[2];
ColorConvertRGBtoHSV(R, G, B, H, S, V);
if (S == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) // Fix local Hue as display below will use it immediately.
H = g.ColorEditLastHue;
if (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) // Fix local Hue as display below will use it immediately.
{
if (S == 0)
H = g.ColorEditLastHue;
if (V == 0)
S = g.ColorEditLastSaturation;
}
}
else if (flags & ImGuiColorEditFlags_InputHSV)
{
Expand Down

0 comments on commit 32c33c6

Please sign in to comment.