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.
| 5762 | // 'desc_id' is not called 'label' because we don't display it next to the button, but only in the tooltip. |
| 5763 | // Note that 'col' may be encoded in HSV if ImGuiColorEditFlags_InputHSV is set. |
| 5764 | bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFlags flags, const ImVec2& size_arg) |
| 5765 | { |
| 5766 | ImGuiWindow* window = GetCurrentWindow(); |
| 5767 | if (window->SkipItems) |
| 5768 | return false; |
| 5769 | |
| 5770 | ImGuiContext& g = *GImGui; |
| 5771 | const ImGuiID id = window->GetID(desc_id); |
| 5772 | const float default_size = GetFrameHeight(); |
| 5773 | const ImVec2 size(size_arg.x == 0.0f ? default_size : size_arg.x, size_arg.y == 0.0f ? default_size : size_arg.y); |
| 5774 | const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + size); |
| 5775 | ItemSize(bb, (size.y >= default_size) ? g.Style.FramePadding.y : 0.0f); |
| 5776 | if (!ItemAdd(bb, id)) |
| 5777 | return false; |
| 5778 | |
| 5779 | bool hovered, held; |
| 5780 | bool pressed = ButtonBehavior(bb, id, &hovered, &held); |
| 5781 | |
| 5782 | if (flags & ImGuiColorEditFlags_NoAlpha) |
| 5783 | flags &= ~(ImGuiColorEditFlags_AlphaPreview | ImGuiColorEditFlags_AlphaPreviewHalf); |
| 5784 | |
| 5785 | ImVec4 col_rgb = col; |
| 5786 | if (flags & ImGuiColorEditFlags_InputHSV) |
| 5787 | ColorConvertHSVtoRGB(col_rgb.x, col_rgb.y, col_rgb.z, col_rgb.x, col_rgb.y, col_rgb.z); |
| 5788 | |
| 5789 | ImVec4 col_rgb_without_alpha(col_rgb.x, col_rgb.y, col_rgb.z, 1.0f); |
| 5790 | float grid_step = ImMin(size.x, size.y) / 2.99f; |
| 5791 | float rounding = ImMin(g.Style.FrameRounding, grid_step * 0.5f); |
| 5792 | ImRect bb_inner = bb; |
| 5793 | float off = 0.0f; |
| 5794 | if ((flags & ImGuiColorEditFlags_NoBorder) == 0) |
| 5795 | { |
| 5796 | 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. |
| 5797 | bb_inner.Expand(off); |
| 5798 | } |
| 5799 | if ((flags & ImGuiColorEditFlags_AlphaPreviewHalf) && col_rgb.w < 1.0f) |
| 5800 | { |
| 5801 | float mid_x = IM_ROUND((bb_inner.Min.x + bb_inner.Max.x) * 0.5f); |
| 5802 | 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); |
| 5803 | window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawFlags_RoundCornersLeft); |
| 5804 | } |
| 5805 | else |
| 5806 | { |
| 5807 | // Because GetColorU32() multiplies by the global style Alpha and we don't want to display a checkerboard if the source code had no alpha |
| 5808 | ImVec4 col_source = (flags & ImGuiColorEditFlags_AlphaPreview) ? col_rgb : col_rgb_without_alpha; |
| 5809 | if (col_source.w < 1.0f) |
| 5810 | RenderColorRectWithAlphaCheckerboard(window->DrawList, bb_inner.Min, bb_inner.Max, GetColorU32(col_source), grid_step, ImVec2(off, off), rounding); |
| 5811 | else |
| 5812 | window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding); |
| 5813 | } |
| 5814 | RenderNavHighlight(bb, id); |
| 5815 | if ((flags & ImGuiColorEditFlags_NoBorder) == 0) |
| 5816 | { |
| 5817 | if (g.Style.FrameBorderSize > 0.0f) |
| 5818 | RenderFrameBorder(bb.Min, bb.Max, rounding); |
| 5819 | else |
| 5820 | window->DrawList->AddRect(bb.Min, bb.Max, GetColorU32(ImGuiCol_FrameBg), rounding); // Color button are often in need of some sort of border |
| 5821 | } |
nothing calls this directly
no test coverage detected