ColorPicker: Added ImGuiColorEditFlags_AlphaBar option (#346)

This commit is contained in:
omar 2017-07-22 19:24:39 +08:00
parent 4355b2e422
commit 790d0eb5be
3 changed files with 9 additions and 10 deletions

View File

@ -9249,18 +9249,14 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
BeginGroup(); BeginGroup();
// Setup // Setup
bool alpha = (flags & ImGuiColorEditFlags_NoAlpha) == 0; bool alpha_bar = (flags & ImGuiColorEditFlags_AlphaBar) && !(flags & ImGuiColorEditFlags_NoAlpha);
ImVec2 picker_pos = window->DC.CursorPos; ImVec2 picker_pos = window->DC.CursorPos;
float bars_width = ColorSquareSize(); // Arbitrary smallish width of Hue/Alpha picking bars float bars_width = ColorSquareSize(); // Arbitrary smallish width of Hue/Alpha picking bars
float bars_line_extrude = ImMin(2.0f, style.ItemInnerSpacing.x * 0.5f); float bars_line_extrude = ImMin(2.0f, style.ItemInnerSpacing.x * 0.5f);
float sv_picker_size = ImMax(bars_width * 1, CalcItemWidth() - (alpha ? 2 : 1) * (bars_width + style.ItemInnerSpacing.x)); // Saturation/Value picking box float sv_picker_size = ImMax(bars_width * 1, CalcItemWidth() - (alpha_bar ? 2 : 1) * (bars_width + style.ItemInnerSpacing.x)); // Saturation/Value picking box
float bar0_pos_x = picker_pos.x + sv_picker_size + style.ItemInnerSpacing.x; float bar0_pos_x = picker_pos.x + sv_picker_size + style.ItemInnerSpacing.x;
float bar1_pos_x = bar0_pos_x + bars_width + style.ItemInnerSpacing.x; float bar1_pos_x = bar0_pos_x + bars_width + style.ItemInnerSpacing.x;
// Recreate our own tooltip over's ColorButton() one because we want to display correct alpha here
//if (IsItemHovered())
// ColorTooltip(col, flags);
float H,S,V; float H,S,V;
ColorConvertRGBtoHSV(col[0], col[1], col[2], H, S, V); ColorConvertRGBtoHSV(col[0], col[1], col[2], H, S, V);
@ -9284,7 +9280,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
} }
// Alpha bar logic // Alpha bar logic
if (alpha) if (alpha_bar)
{ {
SetCursorScreenPos(ImVec2(bar1_pos_x, picker_pos.y)); SetCursorScreenPos(ImVec2(bar1_pos_x, picker_pos.y));
InvisibleButton("alpha", ImVec2(bars_width, sv_picker_size)); InvisibleButton("alpha", ImVec2(bars_width, sv_picker_size));
@ -9314,7 +9310,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
{ {
if ((flags & ImGuiColorEditFlags_ModeMask_) == 0) if ((flags & ImGuiColorEditFlags_ModeMask_) == 0)
flags |= ImGuiColorEditFlags_RGB | ImGuiColorEditFlags_HSV | ImGuiColorEditFlags_HEX; flags |= ImGuiColorEditFlags_RGB | ImGuiColorEditFlags_HSV | ImGuiColorEditFlags_HEX;
PushItemWidth((alpha ? bar1_pos_x : bar0_pos_x) + bars_width - picker_pos.x); PushItemWidth((alpha_bar ? bar1_pos_x : bar0_pos_x) + bars_width - picker_pos.x);
ImGuiColorEditFlags sub_flags = (flags & (ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoColorSquare)) | ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoTooltip; ImGuiColorEditFlags sub_flags = (flags & (ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoColorSquare)) | ImGuiColorEditFlags_NoPicker | ImGuiColorEditFlags_NoOptions | ImGuiColorEditFlags_NoTooltip;
if (flags & ImGuiColorEditFlags_RGB) if (flags & ImGuiColorEditFlags_RGB)
value_changed |= ColorEdit4("##rgb", col, sub_flags | ImGuiColorEditFlags_RGB); value_changed |= ColorEdit4("##rgb", col, sub_flags | ImGuiColorEditFlags_RGB);
@ -9354,7 +9350,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
draw_list->AddLine(ImVec2(bar0_pos_x - bars_line_extrude, bar0_line_y), ImVec2(bar0_pos_x + bars_width + bars_line_extrude, bar0_line_y), IM_COL32_WHITE); draw_list->AddLine(ImVec2(bar0_pos_x - bars_line_extrude, bar0_line_y), ImVec2(bar0_pos_x + bars_width + bars_line_extrude, bar0_line_y), IM_COL32_WHITE);
// Render alpha bar // Render alpha bar
if (alpha) if (alpha_bar)
{ {
float alpha = ImSaturate(col[3]); float alpha = ImSaturate(col[3]);
float bar1_line_y = (float)(int)(picker_pos.y + (1.0f-alpha) * sv_picker_size + 0.5f); float bar1_line_y = (float)(int)(picker_pos.y + (1.0f-alpha) * sv_picker_size + 0.5f);

View File

@ -673,6 +673,7 @@ enum ImGuiColorEditFlags_
ImGuiColorEditFlags_NoInputs = 1 << 7, // ColorEdit, ColorPicker: Disable inputs sliders/text widgets, show only the colored square. ImGuiColorEditFlags_NoInputs = 1 << 7, // ColorEdit, ColorPicker: Disable inputs sliders/text widgets, show only the colored square.
ImGuiColorEditFlags_NoTooltip = 1 << 8, // ColorEdit, ColorButton: Disable tooltip when hovering the colored square. ImGuiColorEditFlags_NoTooltip = 1 << 8, // ColorEdit, ColorButton: Disable tooltip when hovering the colored square.
ImGuiColorEditFlags_NoLabel = 1 << 9, // ColorEdit: Disable display of inline text label (however the label is still used in tooltip and picker) ImGuiColorEditFlags_NoLabel = 1 << 9, // ColorEdit: Disable display of inline text label (however the label is still used in tooltip and picker)
ImGuiColorEditFlags_AlphaBar = 1 << 10, // ColorPicker: Show vertical alpha bar/gradient
ImGuiColorEditFlags_ModeMask_ = ImGuiColorEditFlags_RGB|ImGuiColorEditFlags_HSV|ImGuiColorEditFlags_HEX ImGuiColorEditFlags_ModeMask_ = ImGuiColorEditFlags_RGB|ImGuiColorEditFlags_HSV|ImGuiColorEditFlags_HEX
}; };

View File

@ -674,15 +674,17 @@ void ImGui::ShowTestWindow(bool* p_open)
ImGui::Text("Color picker:"); ImGui::Text("Color picker:");
static bool alpha = false; static bool alpha = false;
static bool alpha_bar = false;
static bool label = true; static bool label = true;
static int inputs_mode = 0; static int inputs_mode = 0;
static float width = 200.0f; static float width = 200.0f;
ImGui::Checkbox("With Alpha", &alpha); ImGui::Checkbox("With Alpha", &alpha);
ImGui::Checkbox("With Alpha Bar", &alpha_bar);
ImGui::Checkbox("With Label", &label); ImGui::Checkbox("With Label", &label);
ImGui::Combo("Mode", &inputs_mode, "All Inputs\0No Inputs\0RGB Input\0HSV Input\0HEX Input\0"); ImGui::Combo("Mode", &inputs_mode, "All Inputs\0No Inputs\0RGB Input\0HSV Input\0HEX Input\0");
ImGui::DragFloat("Width", &width, 1.0f, 1.0f, 999.0f); ImGui::DragFloat("Width", &width, 1.0f, 1.0f, 999.0f);
ImGui::PushItemWidth(width); ImGui::PushItemWidth(width);
ImGuiColorEditFlags flags = (label ? 0 : ImGuiColorEditFlags_NoLabel); ImGuiColorEditFlags flags = (label ? 0 : ImGuiColorEditFlags_NoLabel) | (alpha_bar ? ImGuiColorEditFlags_AlphaBar : 0);
if (inputs_mode == 1) flags |= ImGuiColorEditFlags_NoInputs; if (inputs_mode == 1) flags |= ImGuiColorEditFlags_NoInputs;
if (inputs_mode == 2) flags |= ImGuiColorEditFlags_RGB; if (inputs_mode == 2) flags |= ImGuiColorEditFlags_RGB;
if (inputs_mode == 3) flags |= ImGuiColorEditFlags_HSV; if (inputs_mode == 3) flags |= ImGuiColorEditFlags_HSV;