Skip to content

Commit

Permalink
add ability to pass Color widgets HSV data
Browse files Browse the repository at this point in the history
This allows for lossless data round-trips through the color picker and
color edit systems, and lets you do things like track hue in
applications even when saturation and/or value are zero.

Fixes ocornut#2383
  • Loading branch information
haldean committed Feb 26, 2019
1 parent 0d33a84 commit 1e24dc4
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 27 deletions.
6 changes: 5 additions & 1 deletion imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1117,16 +1117,20 @@ enum ImGuiColorEditFlags_
ImGuiColorEditFlags_Float = 1 << 24, // [DataType] // ColorEdit, ColorPicker, ColorButton: _display_ values formatted as 0.0f..1.0f floats instead of 0..255 integers. No round-trip of value via integers.
ImGuiColorEditFlags_PickerHueBar = 1 << 25, // [PickerMode] // ColorPicker: bar for Hue, rectangle for Sat/Value.
ImGuiColorEditFlags_PickerHueWheel = 1 << 26, // [PickerMode] // ColorPicker: wheel for Hue, triangle for Sat/Value.
ImGuiColorEditFlags_InputRGB = 1 << 27, // [Input] // ColorEdit, ColorPicker: input and output data is in RGB format
ImGuiColorEditFlags_InputHSV = 1 << 28, // [Input] // ColorEdit, ColorPicker: input and output data is in HSV format

// Obsolete names (will be removed)
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
ImGuiColorEditFlags_RGB = ImGuiColorEditFlags_ShowRGB, ImGuiColorEditFlags_HSV = ImGuiColorEditFlags_ShowHSV, ImGuiColorEditFlags_HEX = ImGuiColorEditFlags_ShowHEX,
#endif

// [Internal] Masks
ImGuiColorEditFlags__ShowMask = ImGuiColorEditFlags_ShowRGB|ImGuiColorEditFlags_ShowHSV|ImGuiColorEditFlags_ShowHEX,
ImGuiColorEditFlags__InputMask = ImGuiColorEditFlags_InputRGB|ImGuiColorEditFlags_InputHSV,
ImGuiColorEditFlags__DataTypeMask = ImGuiColorEditFlags_Uint8|ImGuiColorEditFlags_Float,
ImGuiColorEditFlags__PickerMask = ImGuiColorEditFlags_PickerHueWheel|ImGuiColorEditFlags_PickerHueBar,
ImGuiColorEditFlags__OptionsDefault = ImGuiColorEditFlags_Uint8|ImGuiColorEditFlags_ShowRGB|ImGuiColorEditFlags_PickerHueBar // Change application default using SetColorEditOptions()
ImGuiColorEditFlags__OptionsDefault = ImGuiColorEditFlags_Uint8|ImGuiColorEditFlags_ShowRGB|ImGuiColorEditFlags_PickerHueBar|ImGuiColorEditFlags_InputRGB // Change application default using SetColorEditOptions()
};

// Enumeration for GetMouseCursor()
Expand Down
13 changes: 13 additions & 0 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,9 @@ static void ShowDemoWindowWidgets()
ImGui::Checkbox("With HDR", &hdr); ImGui::SameLine(); ShowHelpMarker("Currently all this does is to lift the 0..1 limits on dragging widgets.");
int misc_flags = (hdr ? ImGuiColorEditFlags_HDR : 0) | (drag_and_drop ? 0 : ImGuiColorEditFlags_NoDragDrop) | (alpha_half_preview ? ImGuiColorEditFlags_AlphaPreviewHalf : (alpha_preview ? ImGuiColorEditFlags_AlphaPreview : 0)) | (options_menu ? 0 : ImGuiColorEditFlags_NoOptions);

ImGui::Text("RGB values:");
ImGui::DragFloat4("MyColor##0", (float*)&color.x, 0.01f, 0.0f, 1.0f);

ImGui::Text("Color widget:");
ImGui::SameLine(); ShowHelpMarker("Click on the colored square to open a color picker.\nCTRL+click on individual component to input value.\n");
ImGui::ColorEdit3("MyColor##1", (float*)&color, misc_flags);
Expand Down Expand Up @@ -1164,6 +1167,16 @@ static void ShowDemoWindowWidgets()
if (ImGui::Button("Default: Float + HDR + Hue Wheel"))
ImGui::SetColorEditOptions(ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_PickerHueWheel);

ImGui::Spacing();
ImGui::Text("HSV-formatted colors");
ImGui::SameLine(); ShowHelpMarker("By default, colors are given to ColorEdit and ColorPicker in RGB, but ImGuiColorEditFlags_InputHSV allows you to store colors as HSV and pass them to ColorEdit and ColorPicker as HSV. This comes with the added benefit that you can manipulate hue values with the picker even when saturation or value are zero.");

static ImVec4 color_stored_as_hsv(0.23f, 1.0f, 1.0f, 1.0f);
ImGui::Text("HSV values:");
ImGui::DragFloat4("HSVColor##0", (float*)&color_stored_as_hsv, 0.01f, 0.0f, 1.0f);
ImGui::Text("Color widget with InputHSV:");
ImGui::ColorEdit4("HSVColor##1", (float*)&color_stored_as_hsv, ImGuiColorEditFlags_ShowRGB | ImGuiColorEditFlags_InputHSV);

ImGui::TreePop();
}

Expand Down
Loading

0 comments on commit 1e24dc4

Please # to comment.