MCPcopy Create free account
hub / github.com/IppClub/Dora-SSR / ColorButton

Method ColorButton

Source/3rdParty/imgui/imgui_widgets.cpp:6448–6529  ·  view source on GitHub ↗

A little color square. Return true when clicked. FIXME: May want to display/ignore the alpha component in the color display? Yet show it in the tooltip. 'desc_id' is not called 'label' because we don't display it next to the button, but only in the tooltip. Note that 'col' may be encoded in HSV if ImGuiColorEditFlags_InputHSV is set.

Source from the content-addressed store, hash-verified

6446// 'desc_id' is not called 'label' because we don't display it next to the button, but only in the tooltip.
6447// Note that 'col' may be encoded in HSV if ImGuiColorEditFlags_InputHSV is set.
6448bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags, const ImVec2& size_arg)
6449{
6450 ImGuiWindow* window = GetCurrentWindow();
6451 if (window->SkipItems)
6452 return false;
6453
6454 ImGuiContext& g = *GImGui;
6455 const ImGuiID id = window->GetID(desc_id);
6456 const float default_size = GetFrameHeight();
6457 const ImVec2 size(size_arg.x == 0.0f ? default_size : size_arg.x, size_arg.y == 0.0f ? default_size : size_arg.y);
6458 const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size);
6459 ItemSize(bb, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f);
6460 if (!ItemAdd(bb, id))
6461 return false;
6462
6463 bool hovered, held;
6464 bool pressed = ButtonBehavior(bb, id, &hovered, &held);
6465
6466 if (flags & (ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_AlphaOpaque))
6467 flags &= ~(ImGuiColorEditFlags_AlphaNoBg | ImGuiColorEditFlags_AlphaPreviewHalf);
6468
6469 ImVec4 col_rgb = col;
6470 if (flags & ImGuiColorEditFlags_InputHSV)
6471 ColorConvertHSVtoRGB(col_rgb.x, col_rgb.y, col_rgb.z, col_rgb.x, col_rgb.y, col_rgb.z);
6472
6473 ImVec4 col_rgb_without_alpha(col_rgb.x, col_rgb.y, col_rgb.z, 1.0f);
6474 float grid_step = ImMin(size.x, size.y) / 2.99f;
6475 float rounding = ImMin(g.Style.FrameRounding, grid_step * 0.5f);
6476 ImRect bb_inner = bb;
6477 float off = 0.0f;
6478 if ((flags & ImGuiColorEditFlags_NoBorder) == 0)
6479 {
6480 off = -0.75f; // The border (using Col_FrameBg) tends to look off when color is near-opaque and rounding is enabled. This offset seemed like a good middle ground to reduce those artifacts.
6481 bb_inner.Expand(off);
6482 }
6483 if ((flags & ImGuiColorEditFlags_AlphaPreviewHalf) && col_rgb.w < 1.0f)
6484 {
6485 float mid_x = IM_ROUND((bb_inner.Min.x + bb_inner.Max.x) * 0.5f);
6486 if ((flags & ImGuiColorEditFlags_AlphaNoBg) == 0)
6487 RenderColorRectWithAlphaCheckerboard(window->DrawList, ImVec2(bb_inner.Min.x + grid_step, bb_inner.Min.y), bb_inner.Max, GetColorU32(col_rgb), grid_step, ImVec2(-grid_step + off, off), rounding, ImDrawFlags_RoundCornersRight);
6488 else
6489 window->DrawList->AddRectFilled(ImVec2(bb_inner.Min.x + grid_step, bb_inner.Min.y), bb_inner.Max, GetColorU32(col_rgb), rounding, ImDrawFlags_RoundCornersRight);
6490 window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawFlags_RoundCornersLeft);
6491 }
6492 else
6493 {
6494 // Because GetColorU32() multiplies by the global style Alpha and we don't want to display a checkerboard if the source code had no alpha
6495 ImVec4 col_source = (flags & ImGuiColorEditFlags_AlphaOpaque) ? col_rgb_without_alpha : col_rgb;
6496 if (col_source.w < 1.0f && (flags & ImGuiColorEditFlags_AlphaNoBg) == 0)
6497 RenderColorRectWithAlphaCheckerboard(window->DrawList, bb_inner.Min, bb_inner.Max, GetColorU32(col_source), grid_step, ImVec2(off, off), rounding);
6498 else
6499 window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding);
6500 }
6501 RenderNavCursor(bb, id);
6502 if ((flags & ImGuiColorEditFlags_NoBorder) == 0)
6503 {
6504 if (g.Style.FrameBorderSize > 0.0f)
6505 RenderFrameBorder(bb.Min, bb.Max, rounding);

Callers

nothing calls this directly

Calls 10

GetCurrentWindowFunction · 0.85
ItemSizeFunction · 0.85
ImMinFunction · 0.85
ImVec2Function · 0.85
ColorButtonFunction · 0.85
EndDragDropSourceFunction · 0.85
ExpandMethod · 0.80
AddRectFilledMethod · 0.80
AddRectMethod · 0.80
GetIDMethod · 0.45

Tested by

no test coverage detected