MCPcopy Create free account
hub / github.com/RenderKit/embree / ColorEditRestoreHS

Function ColorEditRestoreHS

tutorials/common/imgui/imgui_widgets.cpp:4908–4928  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

4906// ColorEdit supports RGB and HSV inputs. In case of RGB input resulting color may have undefined hue and/or saturation.
4907// Since widget displays both RGB and HSV values we must preserve hue and saturation to prevent these values resetting.
4908static void ColorEditRestoreHS(const float* col, float* H, float* S, float* V)
4909{
4910 // 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.
4911 // With color check: hue/saturation is preserved in one widget. Editing color in one widget would reset hue/saturation in another one.
4912 // Without color check: common hue/saturation would be displayed in all widgets that have hue/saturation undefined.
4913 // g.ColorEditLastColor is stored as ImU32 RGB value: this essentially gives us color equality check with reduced precision.
4914 // 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,
4915 // therefore this change flipping hue/saturation from undefined to a very tiny value would still be represented in color picker.
4916 ImGuiContext& g = *GImGui;
4917 if (g.ColorEditLastColor != ImGui::ColorConvertFloat4ToU32(ImVec4(col[0], col[1], col[2], 0)))
4918 return;
4919
4920 // When S == 0, H is undefined.
4921 // When H == 1 it wraps around to 0.
4922 if (*S == 0.0f || (*H == 0.0f && g.ColorEditLastHue == 1))
4923 *H = g.ColorEditLastHue;
4924
4925 // When V == 0, S is undefined.
4926 if (*V == 0.0f)
4927 *S = g.ColorEditLastSat;
4928}
4929
4930// Edit colors components (each component in 0.0f..1.0f range).
4931// See enum ImGuiColorEditFlags_ for available options. e.g. Only access 3 floats if ImGuiColorEditFlags_NoAlpha flag is set.

Callers 2

ColorEdit4Method · 0.85
ColorPicker4Method · 0.85

Calls 1

ImVec4Class · 0.85

Tested by

no test coverage detected