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