Skip to content

AddLineMultiColor#9317

Open
PhillipDaPaster wants to merge 2 commits intoocornut:masterfrom
PhillipDaPaster:master
Open

AddLineMultiColor#9317
PhillipDaPaster wants to merge 2 commits intoocornut:masterfrom
PhillipDaPaster:master

Conversation

@PhillipDaPaster
Copy link
Copy Markdown

@PhillipDaPaster PhillipDaPaster commented Mar 21, 2026

Added AddLineMultiColor to the api since i saw there way no gradient lines naturally supported

imgui
void DrawGradientCircle(ImDrawList* draw, const ImVec2& center, float radius, int segments, float thickness) {
    for (int i = 0; i < segments; i++) {
        float theta1 = (2.0f * IM_PI * i) / segments;
        float theta2 = (2.0f * IM_PI * (i + 1)) / segments;

        ImVec2 p1(center.x + cosf(theta1) * radius, center.y + sinf(theta1) * radius);
        ImVec2 p2(center.x + cosf(theta2) * radius, center.y + sinf(theta2) * radius);

        ImU32 col_start = IM_COL32(
            (int)((cosf(theta1) * 0.5f + 0.5f) * 255),
            (int)((sinf(theta1) * 0.5f + 0.5f) * 255),
            (int)((cosf(theta1 + IM_PI / 2) * 0.5f + 0.5f) * 255),
            255
        );

        ImU32 col_end = IM_COL32(
            (int)((cosf(theta2) * 0.5f + 0.5f) * 255),
            (int)((sinf(theta2) * 0.5f + 0.5f) * 255),
            (int)((cosf(theta2 + IM_PI / 2) * 0.5f + 0.5f) * 255),
            255
        );

        draw->AddLineMultiColor(p1, p2, col_start, col_end, thickness);
    }
}

void DrawSpiral(ImDrawList* draw, const ImVec2& center, float radius, int segments, float turns, float thickness) {
    for (int i = 0; i < segments; i++) {
        float t1 = (float)i / segments;
        float t2 = (float)(i + 1) / segments;

        float angle1 = 2.0f * IM_PI * turns * t1;
        float angle2 = 2.0f * IM_PI * turns * t2;

        float r1 = t1 * radius;
        float r2 = t2 * radius;

        ImVec2 p1(center.x + cosf(angle1) * r1, center.y + sinf(angle1) * r1);
        ImVec2 p2(center.x + cosf(angle2) * r2, center.y + sinf(angle2) * r2);

        ImU32 col_start = IM_COL32((int)(t1 * 255), 0, (int)((1 - t1) * 255), 255);
        ImU32 col_end = IM_COL32((int)(t2 * 255), 0, (int)((1 - t2) * 255), 255);

        draw->AddLineMultiColor(p1, p2, col_start, col_end, thickness);
    }
}

void DrawRays(ImDrawList* draw, const ImVec2& center, int rays, float length, float thickness) {
    for (int i = 0; i < rays; i++) {
        float theta = (2.0f * IM_PI * i) / rays;

        ImVec2 p1 = center;
        ImVec2 p2(center.x + cosf(theta) * length, center.y + sinf(theta) * length);

        ImU32 col_start = IM_COL32(255, 255, 0, 255); 
        ImU32 col_end = IM_COL32(255, 0, 0, 0);     

        draw->AddLineMultiColor(p1, p2, col_start, col_end, thickness);
    }
}

void DrawWave(ImDrawList* draw, const ImVec2& start, float length, float amplitude, int points, float thickness) {
    for (int i = 0; i < points; i++) {
        float t1 = (float)i / points;
        float t2 = (float)(i + 1) / points;

        ImVec2 p1(start.x + t1 * length, start.y + sinf(t1 * 4 * IM_PI) * amplitude);
        ImVec2 p2(start.x + t2 * length, start.y + sinf(t2 * 4 * IM_PI) * amplitude);

        ImU32 col_start = IM_COL32((int)(t1 * 255), (int)((1 - t1) * 255), 128, 255);
        ImU32 col_end = IM_COL32((int)(t2 * 255), (int)((1 - t2) * 255), 128, 255);

        draw->AddLineMultiColor(p1, p2, col_start, col_end, thickness);
    }
}

 ImGui::Begin("Example");

 ImGuiWindow* window = ImGui::GetCurrentWindow();
 ImDrawList* draw = window->DrawList;
 draw->Flags |= ImDrawListFlags_AntiAliasedLines;

 ImVec2 pos = ImGui::GetCursorScreenPos();

 DrawGradientCircle(draw, ImVec2(pos.x + 150, pos.y + 150), 100.0f, 128, 6.0f);
 DrawSpiral(draw, ImVec2(pos.x + 500, pos.y + 150), 80.0f, 200, 3.0f, 3.0f);
 DrawRays(draw, ImVec2(pos.x + 150, pos.y + 500), 32, 100.0f, 4.0f);
 DrawWave(draw, ImVec2(pos.x + 350, pos.y + 450), 300.0f, 40.0f, 200, 3.0f);

 ImGui::End();

Added AddLineMultiColor
Added the definition for AddLineMultiColor
@ocornut
Copy link
Copy Markdown
Owner

ocornut commented Mar 23, 2026

Thank you Philipp for your PR.

The main problem with this approach are that lines are not anti-aliased. So for now I'd suggest you use this in your code as a standalone function.

I think once we merge #7972 #2964 we can consider providing a version where a color is submitted per vertex, and then it becomes trivial to provide a AddLineMultiColor().

@PhillipDaPaster
Copy link
Copy Markdown
Author

all good i kinda just coded it well enough to work for my use case but looking foward to that drawlist update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants