Преглед изворни кода

ColorPicker / ColorEdit: restore Hue when zeroing Saturation. (#2722, #2770) - changelog, fixed uninitialized variables, tweaks, renaming.

omar пре 6 година
родитељ
комит
38d22bc47d
3 измењених фајлова са 16 додато и 10 уклоњено
  1. 3 1
      docs/CHANGELOG.txt
  2. 3 1
      imgui_internal.h
  3. 10 8
      imgui_widgets.cpp

+ 3 - 1
docs/CHANGELOG.txt

@@ -35,10 +35,12 @@ HOW TO UPDATE?
 
 
 Other Changes:
 Other Changes:
 - Nav, Scrolling: Added support for Home/End key. (#787)
 - Nav, Scrolling: Added support for Home/End key. (#787)
+- ColorEdit: Disable Hue edit when Saturation==0 instead of letting Hue values jump around.
+- ColorEdit, ColorPicker: In HSV display of a RGB stored value, attempt to locally preserve Hue
+  when Saturation==0, which reduces accidentally lossy interactions. (#2722, 2770) [@rokups]
 - ColorPicker: Made rendering aware of global style alpha of the picker can be faded out. (#2711)
 - ColorPicker: Made rendering aware of global style alpha of the picker can be faded out. (#2711)
   Note that some elements won't accurately fade down with the same intensity, and the color wheel
   Note that some elements won't accurately fade down with the same intensity, and the color wheel
   when enabled will have small overlap glitches with (style.Alpha < 1.0).
   when enabled will have small overlap glitches with (style.Alpha < 1.0).
-- ColorEdit: Disable Hue edit when Saturation==0 instead of letting Hue values jump around.
 - TabBar: fixed ScrollToBar request creating bouncing loop when tab is larger than available space.
 - TabBar: fixed ScrollToBar request creating bouncing loop when tab is larger than available space.
 - TabBar: fixed single-tab not shrinking their width down.
 - TabBar: fixed single-tab not shrinking their width down.
 - TabBar: feed desired width (sum of unclipped tabs width) into layout system to allow for auto-resize. (#2768)
 - TabBar: feed desired width (sum of unclipped tabs width) into layout system to allow for auto-resize. (#2768)

+ 3 - 1
imgui_internal.h

@@ -1013,7 +1013,7 @@ struct ImGuiContext
     ImGuiID                 TempInputTextId;                    // Temporary text input when CTRL+clicking on a slider, etc.
     ImGuiID                 TempInputTextId;                    // Temporary text input when CTRL+clicking on a slider, etc.
     ImGuiColorEditFlags     ColorEditOptions;                   // Store user options for color edit widgets
     ImGuiColorEditFlags     ColorEditOptions;                   // Store user options for color edit widgets
     float                   ColorEditLastHue;
     float                   ColorEditLastHue;
-    float                   ColorEditLastActiveColor[3];
+    float                   ColorEditLastColor[3];
     ImVec4                  ColorPickerRef;
     ImVec4                  ColorPickerRef;
     bool                    DragCurrentAccumDirty;
     bool                    DragCurrentAccumDirty;
     float                   DragCurrentAccum;                   // Accumulator for dragging modification. Always high-precision, not rounded by end-user precision settings
     float                   DragCurrentAccum;                   // Accumulator for dragging modification. Always high-precision, not rounded by end-user precision settings
@@ -1158,6 +1158,8 @@ struct ImGuiContext
         LastValidMousePos = ImVec2(0.0f, 0.0f);
         LastValidMousePos = ImVec2(0.0f, 0.0f);
         TempInputTextId = 0;
         TempInputTextId = 0;
         ColorEditOptions = ImGuiColorEditFlags__OptionsDefault;
         ColorEditOptions = ImGuiColorEditFlags__OptionsDefault;
+        ColorEditLastHue = 0.0f;
+        ColorEditLastColor[0] = ColorEditLastColor[1] = ColorEditLastColor[2] = FLT_MAX;
         DragCurrentAccumDirty = false;
         DragCurrentAccumDirty = false;
         DragCurrentAccum = 0.0f;
         DragCurrentAccum = 0.0f;
         DragSpeedDefaultRatio = 1.0f / 100.0f;
         DragSpeedDefaultRatio = 1.0f / 100.0f;

+ 10 - 8
imgui_widgets.cpp

@@ -4201,9 +4201,9 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
         ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);
         ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);
     else if ((flags & ImGuiColorEditFlags_InputRGB) && (flags & ImGuiColorEditFlags_DisplayHSV))
     else if ((flags & ImGuiColorEditFlags_InputRGB) && (flags & ImGuiColorEditFlags_DisplayHSV))
     {
     {
-        ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]);
         // Hue is lost when converting from greyscale rgb (saturation=0). Restore it.
         // Hue is lost when converting from greyscale rgb (saturation=0). Restore it.
-        if (f[1] == 0 && memcmp(g.ColorEditLastActiveColor, col, sizeof(float) * 3) == 0)
+        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;
             f[0] = g.ColorEditLastHue;
     }
     }
     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]) };
     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]) };
@@ -4335,7 +4335,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
         {
         {
             g.ColorEditLastHue = f[0];
             g.ColorEditLastHue = f[0];
             ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);
             ColorConvertHSVtoRGB(f[0], f[1], f[2], f[0], f[1], f[2]);
-            memcpy(g.ColorEditLastActiveColor, f, sizeof(float) * 3);
+            memcpy(g.ColorEditLastColor, f, sizeof(float) * 3);
         }
         }
         if ((flags & ImGuiColorEditFlags_DisplayRGB) && (flags & ImGuiColorEditFlags_InputHSV))
         if ((flags & ImGuiColorEditFlags_DisplayRGB) && (flags & ImGuiColorEditFlags_InputHSV))
             ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]);
             ColorConvertRGBtoHSV(f[0], f[1], f[2], f[0], f[1], f[2]);
@@ -4514,13 +4514,15 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
     float R = col[0], G = col[1], B = col[2];
     float R = col[0], G = col[1], B = col[2];
     if (flags & ImGuiColorEditFlags_InputRGB)
     if (flags & ImGuiColorEditFlags_InputRGB)
     {
     {
-        ColorConvertRGBtoHSV(R, G, B, H, S, V);
         // Hue is lost when converting from greyscale rgb (saturation=0). Restore it.
         // Hue is lost when converting from greyscale rgb (saturation=0). Restore it.
-        if (S == 0 && memcmp(g.ColorEditLastActiveColor, col, sizeof(float) * 3) == 0)
+        ColorConvertRGBtoHSV(R, G, B, H, S, V);
+        if (S == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0)
             H = g.ColorEditLastHue;
             H = g.ColorEditLastHue;
     }
     }
     else if (flags & ImGuiColorEditFlags_InputHSV)
     else if (flags & ImGuiColorEditFlags_InputHSV)
+    {
         ColorConvertHSVtoRGB(H, S, V, R, G, B);
         ColorConvertHSVtoRGB(H, S, V, R, G, B);
+    }
 
 
     bool value_changed = false, value_changed_h = false, value_changed_sv = false;
     bool value_changed = false, value_changed_h = false, value_changed_sv = false;
 
 
@@ -4643,7 +4645,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]);
             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.ColorEditLastHue = H;
-            memcpy(g.ColorEditLastActiveColor, col, sizeof(float) * 3);
+            memcpy(g.ColorEditLastColor, col, sizeof(float) * 3);
         }
         }
         else if (flags & ImGuiColorEditFlags_InputHSV)
         else if (flags & ImGuiColorEditFlags_InputHSV)
         {
         {
@@ -4696,9 +4698,9 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
             R = col[0];
             R = col[0];
             G = col[1];
             G = col[1];
             B = col[2];
             B = col[2];
-            float preserve_hue = H;
             ColorConvertRGBtoHSV(R, G, B, H, S, V);
             ColorConvertRGBtoHSV(R, G, B, H, S, V);
-            H = preserve_hue;                           // Avoids picker losing hue value for 1 frame glitch.
+            if (S == 0 && memcmp(g.ColorEditLastColor, col, sizeof(float) * 3) == 0) // Fix local Hue as display below will use it immediately.
+                H = g.ColorEditLastHue;
         }
         }
         else if (flags & ImGuiColorEditFlags_InputHSV)
         else if (flags & ImGuiColorEditFlags_InputHSV)
         {
         {