Reed Oei
Reed Oei

Reputation: 1518

How to print text with a specified RGB value in Julia?

For example, say I want to print text using the following color:

R: 0.5

G: 0.8

B: 0.1

I know about print_with_color() but as far as I know it has to use a Symbol to print, and I do not know how to create one for any arbitrary color, or if that is actually possible.

Upvotes: 8

Views: 3834

Answers (3)

Arnold
Arnold

Reputation: 31

You might try 'printstyled' from 'Base' package, it require at least julia 1.7.

printstyled("pouet pouet"; color = :blue, blink = true)

Upvotes: 3

Bob Zimmermann
Bob Zimmermann

Reputation: 993

You might try Crayons.jl. Your specification is float, and Crayons expects specification of 0-255, so some conversion is necessary:

julia> import Pkg; Pkg.add("Crayons")
julia> using Crayons
julia> a = (0.5, 0.8, 0.1)
(0.5, 0.8, 0.1)

julia> b = round.(Int, a .* 255)
(128, 204, 26)

julia> print(Crayon(foreground = b) , "My color string.")

Crayons.jl also supports hex RGB specification in string macros:

julia> print(crayon"#80cc1a", "My color string.")

Upvotes: 3

cormullion
cormullion

Reputation: 1672

Possibly:

julia> function print_rgb(r, g, b, t)
           print("\e[1m\e[38;2;$r;$g;$b;249m",t)
       end
print_rgb (generic function with 1 method)

julia> for i in 0:100
           print_rgb(rand(0:255), rand(0:255), rand(0:255), "hello!")
       end

Julia terminal with pretty colors

Upvotes: 4

Related Questions