MCPcopy Create free account
hub / github.com/Raais/ImStudio / ColorButton

Method ColorButton

src/third-party/imgui/imgui_widgets.cpp:5456–5537  ·  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

5454// 'desc_id' is not called 'label' because we don't display it next to the button, but only in the tooltip.
5455// Note that 'col' may be encoded in HSV if ImGuiColorEditFlags_InputHSV is set.
5456bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags, ImVec2 size)
5457{
5458 ImGuiWindow* window = GetCurrentWindow();
5459 if (window->SkipItems)
5460 return false;
5461
5462 ImGuiContext& g = *GImGui;
5463 const ImGuiID id = window->GetID(desc_id);
5464 float default_size = GetFrameHeight();
5465 if (size.x == 0.0f)
5466 size.x = default_size;
5467 if (size.y == 0.0f)
5468 size.y = default_size;
5469 const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size);
5470 ItemSize(bb, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f);
5471 if (!ItemAdd(bb, id))
5472 return false;
5473
5474 bool hovered, held;
5475 bool pressed = ButtonBehavior(bb, id, &hovered, &held);
5476
5477 if (flags & ImGuiColorEditFlags_NoAlpha)
5478 flags &= ~(ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf);
5479
5480 ImVec4 col_rgb = col;
5481 if (flags & ImGuiColorEditFlags_InputHSV)
5482 ColorConvertHSVtoRGB(col_rgb.x, col_rgb.y, col_rgb.z, col_rgb.x, col_rgb.y, col_rgb.z);
5483
5484 ImVec4 col_rgb_without_alpha(col_rgb.x, col_rgb.y, col_rgb.z, 1.0f);
5485 float grid_step = ImMin(size.x, size.y) / 2.99f;
5486 float rounding = ImMin(g.Style.FrameRounding, grid_step * 0.5f);
5487 ImRect bb_inner = bb;
5488 float off = 0.0f;
5489 if ((flags & ImGuiColorEditFlags_NoBorder) == 0)
5490 {
5491 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.
5492 bb_inner.Expand(off);
5493 }
5494 if ((flags & ImGuiColorEditFlags_AlphaPreviewHalf) && col_rgb.w < 1.0f)
5495 {
5496 float mid_x = IM_ROUND((bb_inner.Min.x + bb_inner.Max.x) * 0.5f);
5497 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);
5498 window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawFlags_RoundCornersLeft);
5499 }
5500 else
5501 {
5502 // Because GetColorU32() multiplies by the global style Alpha and we don't want to display a checkerboard if the source code had no alpha
5503 ImVec4 col_source = (flags & ImGuiColorEditFlags_AlphaPreview) ? col_rgb : col_rgb_without_alpha;
5504 if (col_source.w < 1.0f)
5505 RenderColorRectWithAlphaCheckerboard(window->DrawList, bb_inner.Min, bb_inner.Max, GetColorU32(col_source), grid_step, ImVec2(off, off), rounding);
5506 else
5507 window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding);
5508 }
5509 RenderNavHighlight(bb, id);
5510 if ((flags & ImGuiColorEditFlags_NoBorder) == 0)
5511 {
5512 if (g.Style.FrameBorderSize > 0.0f)
5513 RenderFrameBorder(bb.Min, bb.Max, rounding);

Callers

nothing calls this directly

Calls 6

GetCurrentWindowFunction · 0.85
ImMinFunction · 0.85
ImVec2Function · 0.85
GetIDMethod · 0.80
AddRectFilledMethod · 0.80
AddRectMethod · 0.80

Tested by

no test coverage detected