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.
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.
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.
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.
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.
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
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.")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With