Color conversion from terminal color to Colors.jl color

Something like this might get you started

using Luxor
using Colors

low_ansi_colors = Dict(
    0 => RGB(0, 0, 0),    
    1 => RGB(0.5, 0, 0), 
    2 => RGB(0, 0.5, 0), 
    3 => RGB(0.5, 0.5, 0), 
    4 => RGB(0, 0, 0.5), 
    5 => RGB(0.5, 0, 0.5),
    6 => RGB(0, 0.5, 0.5),
    7 => RGB(0.75, 0.75, 0.75),
    8 => RGB(0.5, 0.5, 0.5),
    9 => RGB(1, 0, 0), 
    10 => RGB(0, 1, 0), 
    11 => RGB(1, 1, 0),
    12 => RGB(0, 0, 1), 
    13 => RGB(1, 0, 1),
    14 => RGB(0, 1, 1),
    15 => RGB(1, 1, 1)
)

function ansi_to_colorant(ansi_code)
    if ansi_code < 16
        return low_ansi_colors[ansi_code]
    elseif ansi_code < 232
        r = (ansi_code - 16) ÷ 36
        g = ((ansi_code - 16) ÷ 6) % 6
        b = (ansi_code - 16) % 6
        return RGB(r / 5, g / 5, b / 5)
    else
        grey = (ansi_code - 232) / 23
        return RGB(grey, grey, grey)
    end
end

@drawsvg begin
    background("black")
    table = Table(16, 16, 30, 30)
    for (pos, n) in table
        sethue(ansi_to_colorant(n))
        box(table, n, :fill)
        sethue("white")
        text(string(n), pos, halign=:center)
    end
end

4 Likes