mirror of
https://github.com/Drezil/imgui.git
synced 2025-07-04 12:08:47 +02:00
ColorEdit: Fix multiple issues. (#4014)
* Change g.ColorEditLastColor type to ImU32 and store RGB color value. - Fixes inability to change hue when saturation is 0. (#4014) - Fixes edgecases where lossy color conversion prevent restoration of hue/saturation. - Fixes hue value jitter when modifying color using SV square. * Fix hue resetting to 0 when it is set to 255 by explicitly restoring hue if it is 0 and previous value was 1. * Further reduce hue jitter by restoring hue when color is modified using SV square.
This commit is contained in:
@ -4781,6 +4781,30 @@ bool ImGui::ColorEdit3(const char* label, float col[3], ImGuiColorEditFlags flag
|
||||
return ColorEdit4(label, col, flags | ImGuiColorEditFlags_NoAlpha);
|
||||
}
|
||||
|
||||
// ColorEdit supports RGB and HSV inputs. In case of RGB input resulting color may have undefined hue and/or saturation.
|
||||
// Since widget displays both RGB and HSV values we must preserve hue and saturation to prevent these values resetting.
|
||||
static void ColorEditRestoreHS(const float* col, float* H, float* S, float* V)
|
||||
{
|
||||
// This check is optional. Suppose we have two color widgets side by side, both widgets display different colors, but both colors have hue and/or saturation undefined.
|
||||
// With color check: hue/saturation is preserved in one widget. Editing color in one widget would reset hue/saturation in another one.
|
||||
// Without color check: common hue/saturation would be displayed in all widgets that have hue/saturation undefined.
|
||||
// g.ColorEditLastColor is stored as ImU32 RGB value: this essentially gives us color equality check with reduced precision.
|
||||
// Tiny external color changes would not be detected and this check would still pass. This is OK, since we only restore hue/saturation _only_ if they are undefined,
|
||||
// therefore this change flipping hue/saturation from undefined to a very tiny value would still be represented in color picker.
|
||||
ImGuiContext& g = *GImGui;
|
||||
if (g.ColorEditLastColor != ImGui::ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0)))
|
||||
return;
|
||||
|
||||
// When S == 0, H is undefined.
|
||||
// When H == 1 it wraps around to 0.
|
||||
if (*S == 0.0f || (*H == 0.0f && g.ColorEditLastHue == 1))
|
||||
*H = g.ColorEditLastHue;
|
||||
|
||||
// When V == 0, S is undefined.
|
||||
if (*V == 0.0f)
|
||||
*S = g.ColorEditLastSat;
|
||||
}
|
||||
|
||||
// Edit colors components (each component in 0.0f..1.0f range).
|
||||
// See enum ImGuiColorEditFlags_ for available options. e.g. Only access 3 floats if ImGuiColorEditFlags_NoAlpha flag is set.
|
||||
// With typical options: Left-click on color square to open color picker. Right-click to open option menu. CTRL-Click over input fields to edit them and TAB to go to next item.
|
||||
@ -4836,13 +4860,7 @@ 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 (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0)
|
||||
{
|
||||
if (f[1] == 0)
|
||||
f[0] = g.ColorEditLastHue;
|
||||
if (f[2] == 0)
|
||||
f[1] = g.ColorEditLastSat;
|
||||
}
|
||||
ColorEditRestoreHS(col, &f[0], &f[1], &f[2]);
|
||||
}
|
||||
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]) };
|
||||
|
||||
@ -4977,7 +4995,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
|
||||
g.ColorEditLastHue = f[0];
|
||||
g.ColorEditLastSat = f[1];
|
||||
ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);
|
||||
memcpy(g.ColorEditLastColor, f, sizeof(float) * 3);
|
||||
g.ColorEditLastColor = ColorConvertFloat4ToU32(ImVec4(f[0], f[1], f[2], 0));
|
||||
}
|
||||
if ((flags & ImGuiColorEditFlags_DisplayRGB) && (flags & ImGuiColorEditFlags_InputHSV))
|
||||
ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]);
|
||||
@ -5112,13 +5130,7 @@ 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 (memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0)
|
||||
{
|
||||
if (S == 0)
|
||||
H = g.ColorEditLastHue;
|
||||
if (V == 0)
|
||||
S = g.ColorEditLastSat;
|
||||
}
|
||||
ColorEditRestoreHS(col, &H, &S, &V);
|
||||
}
|
||||
else if (flags & ImGuiColorEditFlags_InputHSV)
|
||||
{
|
||||
@ -5171,6 +5183,10 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
|
||||
{
|
||||
S = ImSaturate((io.MousePos.x - picker_pos.x) / (sv_picker_size - 1));
|
||||
V = 1.0f - ImSaturate((io.MousePos.y - picker_pos.y) / (sv_picker_size - 1));
|
||||
|
||||
// Greatly reduces hue jitter and reset to 0 when hue == 255 and color is rapidly modified using SV square.
|
||||
if (g.ColorEditLastColor == ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0)))
|
||||
H = g.ColorEditLastHue;
|
||||
value_changed = value_changed_sv = true;
|
||||
}
|
||||
if (!(flags & ImGuiColorEditFlags_NoOptions))
|
||||
@ -5247,7 +5263,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.ColorEditLastSat = S;
|
||||
memcpy(g.ColorEditLastColor, col, sizeof(float) * 3);
|
||||
g.ColorEditLastColor = ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0));
|
||||
}
|
||||
else if (flags & ImGuiColorEditFlags_InputHSV)
|
||||
{
|
||||
@ -5301,13 +5317,7 @@ 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 (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.ColorEditLastSat;
|
||||
}
|
||||
ColorEditRestoreHS(col, &H, &S, &V); // Fix local Hue as display below will use it immediately.
|
||||
}
|
||||
else if (flags & ImGuiColorEditFlags_InputHSV)
|
||||
{
|
||||
|
Reference in New Issue
Block a user