Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 492
Reed Oei Avatar asked Jan 13 '15 19:01

Reed Oei


People also ask

How to print out message in different colors in Julia?

printstyled () function helps in printing out message in different colors. Julia also supports printf () function which is used in C language to print output on the console. Julia macro (which is used by prefacing it with the @ sign) provides the Printf package which needs to be imported in order to use. printf () is also used as a formatting tool.

How to print text on the screen in Julia?

In all programming languages – printing is the most importing step, where we print the text on the screen/console. Printing is done by predefined functions/methods. In Julia language – we use the print () and println () functions to print the text on the screen.

How to print the output of the program in Julia?

The most common function to print the output of the program in the console of Julia is print () and println (). To execute this command we just need to press Enter on the keyboard. The main difference is that the println () function adds a new line to the end of the output.

What is the use of printf () in Julia?

Julia also supports printf () function which is used in C language to print output on the console. Julia macro (which is used by prefacing it with the @ sign) provides the Printf package which needs to be imported in order to use. printf () is also used as a formatting tool.


2 Answers

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

like image 54
cormullion Avatar answered Nov 17 '22 01:11

cormullion


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.")
like image 42
Bob Zimmermann Avatar answered Nov 17 '22 00:11

Bob Zimmermann