Skip to content

'Render' colours in REPL #514

@tecosaur

Description

@tecosaur

Hello, I was having trouble imagining what various colours looked like in the REPL so I cooked up a little code to show me the colours. Should there be interest, I'd be happy to turn this into a PR.

Result

image

Code

colorcode(rgb::Vector{<:Integer}, background=false) =
    string("\e[", if background "48" else "38" end,
           ";2;", rgb[1], ";", rgb[2], ";", rgb[3], "m")

colorcode(c::RGB, background=false) =
    colorcode(reinterpret.([red(c), green(c), blue(c)]), background)

colorcode(c::RGB{Float64}, background=false) =
    colorcode(round.(Int, 255 .* [red(c), green(c), blue(c)]), background)

colorcode(c::Colorant, background=false) =
    colorcode(color("#"*hex(c, :rrggbb)), background)

if ENV["COLORTERM"] == "truecolor"
    import Base.show
    function show(io::IO, ::MIME"text/plain", c::Colorant)
        print(colorcode(c, true), "  \e[0m ")
        show(io, c)
    end
    function show(io::IO, ::MIME"text/plain", cs::Vector{<:Colorant})
        println(join(colorcode.(cs,true), if length(cs) <= 40 "  " else " " end))
    end
    function show(io::IO, ::MIME"text/plain", cs::Array{<:Colorant, 2})
        for csrow in eachrow(cs)
            println(join(colorcode.(csrow,true), if size(cs,2) <= 40 "  " else " " end))
        end
    end
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions