Skip to content

Commit bdd31ec

Browse files
committed
Internal: Refactor: Moved RenderColorRectWithAlphaCheckerboard() to imgui_draw.cpp, tweaked signature.
1 parent 0bbbbe0 commit bdd31ec

File tree

4 files changed

+55
-51
lines changed

4 files changed

+55
-51
lines changed

imgui.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1791,10 +1791,19 @@ int ImTextCountUtf8BytesFromStr(const ImWchar* in_text, const ImWchar* in_text_e
17911791
}
17921792

17931793
//-----------------------------------------------------------------------------
1794-
// [SECTION] MISC HELPERS/UTILTIES (Color functions)
1794+
// [SECTION] MISC HELPERS/UTILITIES (Color functions)
17951795
// Note: The Convert functions are early design which are not consistent with other API.
17961796
//-----------------------------------------------------------------------------
17971797

1798+
IMGUI_API ImU32 ImAlphaBlendColors(ImU32 col_a, ImU32 col_b)
1799+
{
1800+
float t = ((col_b >> IM_COL32_A_SHIFT) & 0xFF) / 255.f;
1801+
int r = ImLerp((int)(col_a >> IM_COL32_R_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_R_SHIFT) & 0xFF, t);
1802+
int g = ImLerp((int)(col_a >> IM_COL32_G_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_G_SHIFT) & 0xFF, t);
1803+
int b = ImLerp((int)(col_a >> IM_COL32_B_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_B_SHIFT) & 0xFF, t);
1804+
return IM_COL32(r, g, b, 0xFF);
1805+
}
1806+
17981807
ImVec4 ImGui::ColorConvertU32ToFloat4(ImU32 in)
17991808
{
18001809
float s = 1.0f/255.0f;

imgui_draw.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,6 +3172,7 @@ void ImFont::RenderText(ImDrawList* draw_list, float size, ImVec2 pos, ImU32 col
31723172
// - RenderRectFilledRangeH()
31733173
//-----------------------------------------------------------------------------
31743174
// Function in need of a redesign (legacy mess)
3175+
// - RenderColorRectWithAlphaCheckerboard()
31753176
//-----------------------------------------------------------------------------
31763177

31773178
// Render an arrow aimed to be aligned with text (p_min is a position in the same space text would be positioned). To e.g. denote expanded/collapsed state
@@ -3329,6 +3330,43 @@ void ImGui::RenderRectFilledRangeH(ImDrawList* draw_list, const ImRect& rect, Im
33293330
draw_list->PathFillConvex(col);
33303331
}
33313332

3333+
// Helper for ColorPicker4()
3334+
// NB: This is rather brittle and will show artifact when rounding this enabled if rounded corners overlap multiple cells. Caller currently responsible for avoiding that.
3335+
// Spent a non reasonable amount of time trying to getting this right for ColorButton with rounding+anti-aliasing+ImGuiColorEditFlags_HalfAlphaPreview flag + various grid sizes and offsets, and eventually gave up... probably more reasonable to disable rounding alltogether.
3336+
// FIXME: uses ImGui::GetColorU32
3337+
void ImGui::RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 col, float grid_step, ImVec2 grid_off, float rounding, int rounding_corners_flags)
3338+
{
3339+
if (((col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT) < 0xFF)
3340+
{
3341+
ImU32 col_bg1 = ImGui::GetColorU32(ImAlphaBlendColors(IM_COL32(204, 204, 204, 255), col));
3342+
ImU32 col_bg2 = ImGui::GetColorU32(ImAlphaBlendColors(IM_COL32(128, 128, 128, 255), col));
3343+
draw_list->AddRectFilled(p_min, p_max, col_bg1, rounding, rounding_corners_flags);
3344+
3345+
int yi = 0;
3346+
for (float y = p_min.y + grid_off.y; y < p_max.y; y += grid_step, yi++)
3347+
{
3348+
float y1 = ImClamp(y, p_min.y, p_max.y), y2 = ImMin(y + grid_step, p_max.y);
3349+
if (y2 <= y1)
3350+
continue;
3351+
for (float x = p_min.x + grid_off.x + (yi & 1) * grid_step; x < p_max.x; x += grid_step * 2.0f)
3352+
{
3353+
float x1 = ImClamp(x, p_min.x, p_max.x), x2 = ImMin(x + grid_step, p_max.x);
3354+
if (x2 <= x1)
3355+
continue;
3356+
int rounding_corners_flags_cell = 0;
3357+
if (y1 <= p_min.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopRight; }
3358+
if (y2 >= p_max.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotRight; }
3359+
rounding_corners_flags_cell &= rounding_corners_flags;
3360+
draw_list->AddRectFilled(ImVec2(x1, y1), ImVec2(x2, y2), col_bg2, rounding_corners_flags_cell ? rounding : 0.0f, rounding_corners_flags_cell);
3361+
}
3362+
}
3363+
}
3364+
else
3365+
{
3366+
draw_list->AddRectFilled(p_min, p_max, col, rounding, rounding_corners_flags);
3367+
}
3368+
}
3369+
33323370
//-----------------------------------------------------------------------------
33333371
// [SECTION] Decompression code
33343372
//-----------------------------------------------------------------------------

imgui_internal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ IMGUI_API ImU32 ImHashStr(const char* data, size_t data_size = 0, ImU32
218218
static inline ImU32 ImHash(const void* data, int size, ImU32 seed = 0) { return size ? ImHashData(data, (size_t)size, seed) : ImHashStr((const char*)data, 0, seed); } // [moved to ImHashStr/ImHashData in 1.68]
219219
#endif
220220

221+
// Helpers: Color Blending
222+
IMGUI_API ImU32 ImAlphaBlendColors(ImU32 col_a, ImU32 col_b);
223+
221224
// Helpers: Bit manipulation
222225
static inline bool ImIsPowerOfTwo(int v) { return v != 0 && (v & (v - 1)) == 0; }
223226
static inline int ImUpperPowerOfTwo(int v) { v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; return v; }
@@ -1812,7 +1815,7 @@ namespace ImGui
18121815
IMGUI_API void RenderTextEllipsis(ImDrawList* draw_list, const ImVec2& pos_min, const ImVec2& pos_max, float clip_max_x, float ellipsis_max_x, const char* text, const char* text_end, const ImVec2* text_size_if_known);
18131816
IMGUI_API void RenderFrame(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, bool border = true, float rounding = 0.0f);
18141817
IMGUI_API void RenderFrameBorder(ImVec2 p_min, ImVec2 p_max, float rounding = 0.0f);
1815-
IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, int rounding_corners_flags = ~0);
1818+
IMGUI_API void RenderColorRectWithAlphaCheckerboard(ImDrawList* draw_list, ImVec2 p_min, ImVec2 p_max, ImU32 fill_col, float grid_step, ImVec2 grid_off, float rounding = 0.0f, int rounding_corners_flags = ~0);
18161819
IMGUI_API void RenderNavHighlight(const ImRect& bb, ImGuiID id, ImGuiNavHighlightFlags flags = ImGuiNavHighlightFlags_TypeDefault); // Navigation highlight
18171820
IMGUI_API const char* FindRenderedTextEnd(const char* text, const char* text_end = NULL); // Find the optional ## from which we stop displaying text.
18181821
IMGUI_API void LogRenderedText(const ImVec2* ref_pos, const char* text, const char* text_end = NULL);

imgui_widgets.cpp

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4493,52 +4493,6 @@ bool ImGui::ColorPicker3(const char* label, float col[3], ImGuiColorEditFlags fl
44934493
return true;
44944494
}
44954495

4496-
static inline ImU32 ImAlphaBlendColor(ImU32 col_a, ImU32 col_b)
4497-
{
4498-
float t = ((col_b >> IM_COL32_A_SHIFT) & 0xFF) / 255.f;
4499-
int r = ImLerp((int)(col_a >> IM_COL32_R_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_R_SHIFT) & 0xFF, t);
4500-
int g = ImLerp((int)(col_a >> IM_COL32_G_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_G_SHIFT) & 0xFF, t);
4501-
int b = ImLerp((int)(col_a >> IM_COL32_B_SHIFT) & 0xFF, (int)(col_b >> IM_COL32_B_SHIFT) & 0xFF, t);
4502-
return IM_COL32(r, g, b, 0xFF);
4503-
}
4504-
4505-
// Helper for ColorPicker4()
4506-
// NB: This is rather brittle and will show artifact when rounding this enabled if rounded corners overlap multiple cells. Caller currently responsible for avoiding that.
4507-
// I spent a non reasonable amount of time trying to getting this right for ColorButton with rounding+anti-aliasing+ImGuiColorEditFlags_HalfAlphaPreview flag + various grid sizes and offsets, and eventually gave up... probably more reasonable to disable rounding alltogether.
4508-
void ImGui::RenderColorRectWithAlphaCheckerboard(ImVec2 p_min, ImVec2 p_max, ImU32 col, float grid_step, ImVec2 grid_off, float rounding, int rounding_corners_flags)
4509-
{
4510-
ImGuiWindow* window = GetCurrentWindow();
4511-
if (((col & IM_COL32_A_MASK) >> IM_COL32_A_SHIFT) < 0xFF)
4512-
{
4513-
ImU32 col_bg1 = GetColorU32(ImAlphaBlendColor(IM_COL32(204,204,204,255), col));
4514-
ImU32 col_bg2 = GetColorU32(ImAlphaBlendColor(IM_COL32(128,128,128,255), col));
4515-
window->DrawList->AddRectFilled(p_min, p_max, col_bg1, rounding, rounding_corners_flags);
4516-
4517-
int yi = 0;
4518-
for (float y = p_min.y + grid_off.y; y < p_max.y; y += grid_step, yi++)
4519-
{
4520-
float y1 = ImClamp(y, p_min.y, p_max.y), y2 = ImMin(y + grid_step, p_max.y);
4521-
if (y2 <= y1)
4522-
continue;
4523-
for (float x = p_min.x + grid_off.x + (yi & 1) * grid_step; x < p_max.x; x += grid_step * 2.0f)
4524-
{
4525-
float x1 = ImClamp(x, p_min.x, p_max.x), x2 = ImMin(x + grid_step, p_max.x);
4526-
if (x2 <= x1)
4527-
continue;
4528-
int rounding_corners_flags_cell = 0;
4529-
if (y1 <= p_min.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_TopRight; }
4530-
if (y2 >= p_max.y) { if (x1 <= p_min.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotLeft; if (x2 >= p_max.x) rounding_corners_flags_cell |= ImDrawCornerFlags_BotRight; }
4531-
rounding_corners_flags_cell &= rounding_corners_flags;
4532-
window->DrawList->AddRectFilled(ImVec2(x1,y1), ImVec2(x2,y2), col_bg2, rounding_corners_flags_cell ? rounding : 0.0f, rounding_corners_flags_cell);
4533-
}
4534-
}
4535-
}
4536-
else
4537-
{
4538-
window->DrawList->AddRectFilled(p_min, p_max, col, rounding, rounding_corners_flags);
4539-
}
4540-
}
4541-
45424496
// Helper for ColorPicker4()
45434497
static void RenderArrowsForVerticalBar(ImDrawList* draw_list, ImVec2 pos, ImVec2 half_sz, float bar_w, float alpha)
45444498
{
@@ -4909,7 +4863,7 @@ bool ImGui::ColorPicker4(const char* label, float col[4], ImGuiColorEditFlags fl
49094863
{
49104864
float alpha = ImSaturate(col[3]);
49114865
ImRect bar1_bb(bar1_pos_x, picker_pos.y, bar1_pos_x + bars_width, picker_pos.y + sv_picker_size);
4912-
RenderColorRectWithAlphaCheckerboard(bar1_bb.Min, bar1_bb.Max, 0, bar1_bb.GetWidth() / 2.0f, ImVec2(0.0f, 0.0f));
4866+
RenderColorRectWithAlphaCheckerboard(draw_list, bar1_bb.Min, bar1_bb.Max, 0, bar1_bb.GetWidth() / 2.0f, ImVec2(0.0f, 0.0f));
49134867
draw_list->AddRectFilledMultiColor(bar1_bb.Min, bar1_bb.Max, user_col32_striped_of_alpha, user_col32_striped_of_alpha, user_col32_striped_of_alpha & ~IM_COL32_A_MASK, user_col32_striped_of_alpha & ~IM_COL32_A_MASK);
49144868
float bar1_line_y = IM_ROUND(picker_pos.y + (1.0f - alpha) * sv_picker_size);
49154869
RenderFrameBorder(bar1_bb.Min, bar1_bb.Max, 0.0f);
@@ -4973,15 +4927,15 @@ bool ImGui::ColorButton(const char* desc_id, const ImVec4& col, ImGuiColorEditFl
49734927
if ((flags & ImGuiColorEditFlags_AlphaPreviewHalf) && col_rgb.w < 1.0f)
49744928
{
49754929
float mid_x = IM_ROUND((bb_inner.Min.x + bb_inner.Max.x) * 0.5f);
4976-
RenderColorRectWithAlphaCheckerboard(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, ImDrawCornerFlags_TopRight| ImDrawCornerFlags_BotRight);
4930+
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, ImDrawCornerFlags_TopRight| ImDrawCornerFlags_BotRight);
49774931
window->DrawList->AddRectFilled(bb_inner.Min, ImVec2(mid_x, bb_inner.Max.y), GetColorU32(col_rgb_without_alpha), rounding, ImDrawCornerFlags_TopLeft|ImDrawCornerFlags_BotLeft);
49784932
}
49794933
else
49804934
{
49814935
// Because GetColorU32() multiplies by the global style Alpha and we don't want to display a checkerboard if the source code had no alpha
49824936
ImVec4 col_source = (flags & ImGuiColorEditFlags_AlphaPreview) ? col_rgb : col_rgb_without_alpha;
49834937
if (col_source.w < 1.0f)
4984-
RenderColorRectWithAlphaCheckerboard(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), grid_step, ImVec2(off, off), rounding);
4938+
RenderColorRectWithAlphaCheckerboard(window->DrawList, bb_inner.Min, bb_inner.Max, GetColorU32(col_source), grid_step, ImVec2(off, off), rounding);
49854939
else
49864940
window->DrawList->AddRectFilled(bb_inner.Min, bb_inner.Max, GetColorU32(col_source), rounding, ImDrawCornerFlags_All);
49874941
}

0 commit comments

Comments
 (0)