Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorful text #5208

Closed
LEVNI-Alii opened this issue Apr 16, 2022 · 1 comment
Closed

Colorful text #5208

LEVNI-Alii opened this issue Apr 16, 2022 · 1 comment

Comments

@LEVNI-Alii
Copy link

LEVNI-Alii commented Apr 16, 2022

Hello,

Type the character you have specified for the color you want after the $ sign.

Example:

static const auto& code = R"($r#include $y<iostream>
$busing namespace $wstd$l; $d// for easy

$bauto $wmain$l() -> $bint $l{
    $pif $l($o2 $l+ $o2 $l== $o4$l)
        $wcout $l<< $f"Hello, World." $l<< $m'\n'$l;
    $preturn $o0$l;
};
)";
static const ImVec4&
    white = { 1,1,1,1 },
    blue = { 0.000f, 0.703f, 0.917f,1 },
    red = { 0.976f, 0.117f, 0.265f ,1 },
    grey = { 0.230f, 0.226f, 0.289f,1 },
    lgrey = { 0.630f, 0.626f, 0.689f,1 },
    green = { 0.000f, 0.386f, 0.265f,1 },
    lime = { 0.55f, 0.90f, 0.06f,1 },
    yellow = { 0.91f, 1.00f, 0.21f,1 },
    purple = { 1,0,1,1 },
    orange = { 1.00f, 0.36f, 0.09f,1 };
im::ColorfulText(code, {
    {'w', white},
    {'b', blue},
    {'d', grey},
    {'l', lgrey},
    {'f', green},
    {'m', lime},
    {'y', yellow},
    {'p', purple},
    {'r', red},
    {'o', orange}
});

image

Souce Code

using str = std::string;
template <typename T>
using list = std::pmr::vector<T>;
namespace im = ImGui;
void ColorfulText(const str& text, const list<pair<char, ImVec4>>& colors = {}) {
    auto p = im::GetCursorScreenPos();
    const auto first_px = p.x, first_py = p.y;
    auto im_colors = ImGui::GetStyle().Colors;
    const auto default_color = im_colors[ImGuiCol_Text];
    str temp_str;
    struct text_t {
        ImVec4 color;
        str text;
    };
    list<text_t> texts;
    bool color_time = false;
    ImVec4 last_color = default_color;
    for (const auto& i : text) {
        if (color_time) {
            const auto& f = std::find_if(colors.begin(), colors.end(), [i](const auto& v) { return v.first == i; });
            if (f != colors.end())
                last_color = f->second;
            else
                temp_str += i;
            color_time = false;
            continue;
        };
        switch (i) {
        case '$':
            color_time = true;
            if (!temp_str.empty()) {
                texts.push_back({ last_color, temp_str });
                temp_str.clear();
            };
            break;
        default:
            temp_str += i;
        };
    };
    if (!temp_str.empty()) {
        texts.push_back({ last_color, temp_str });
        temp_str.clear();
    };
    float max_x = p.x;
    for (const auto& i : texts) {
        im_colors[ImGuiCol_Text] = i.color;
        list<str> lines;
        temp_str.clear();
        for (const auto& lc : i.text) {
            if (lc == '\n') {
                lines.push_back(temp_str += lc);
                temp_str.clear();
            }
            else
                temp_str += lc;
        };
        bool last_is_line = false;
        if (!temp_str.empty())
            lines.push_back(temp_str);
        else
            last_is_line = true;
        float last_px = 0.f;
        for (const auto& j : lines) {
            im::RenderText(p, j.c_str());
            p.y += 15.f;
            last_px = p.x;
            max_x = (max_x < last_px) ? last_px : max_x;
            p.x = first_px;
        };
        const auto& last = lines.back();
        if (last.back() != '\n')
            p.x = last_px;
        else
            p.x = first_px;
        if (!last_is_line)
            p.y -= 15.f;
        if (i.text.back() != '\n')
            p.x += im::CalcTextSize(last.c_str()).x;
    };
    im_colors[ImGuiCol_Text] = default_color;
    im::Dummy({ max_x - p.x, p.y - first_py });
};
@ocornut
Copy link
Owner

ocornut commented Apr 19, 2022

Hello,
Thanks for posting this. I'll close as duplicate of #902 it would be nice if you copied your post over to that issue too.

@ocornut ocornut closed this as completed Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants