View Source ExAequo.Color (ExAequo v0.6.3)
## Support for the 256 ANSI and full RGB colors
N.B. Does, of course, respect the usage of the $NO_COLOR
variable
The most basic approach is to use the generated escape sequences directly in your code, e.g.
IO.puts(ExAequo.Color.rgb(250, 148, 13) <> "Brownish Orange" <> ExAequo.Color.reset)
rgb
The generated escape codes would be:
iex(1)> rgb(250, 148, 13)
"\e[38;2;250;148;13m"
iex(2)> reset()
"\e[0m"
format
But like IO.ANSI
a convenience function called format
is available
iex(3)> format(["Hello", "World"])
["Hello", "World"]
As one can see it is tailor made for IO.puts
and may be converted into a string by means of
IO.chardata_to_string
, this conversion can also be done by format
itself
iex(4)> format(["Hello", "World"], to_string: true)
"HelloWorld"
Or the convenient shortcut...
format_as_str
iex(5)> format_as_str(["Hello", "World"])
"HelloWorld"
putc
A shortcut for
color_definition_list
|> format
|> IO.puts
RGB
In order to get colors into the mix we can use, atoms (for named colors or instructions like reset) or triples for RGB colors
iex(6)> format([{100, 20, 150}, "Deep Purple (pun intended)", :reset])
["\e[38;2;100;20;150m", "Deep Purple (pun intended)", "\e[0m"]
8 Color Space
And here are some nice names, which shall work on all terminals
iex(7)> format([:red, "red", :blue, "blue"])
["\e[31m", "red", "\e[34m", "blue"]
Oftentimes you will pass a variable to format
and not a literal array, then the usage of the reset: true
option
might come in handy
iex(8)> some_values = [:azure1, "The sky?"]
...(8)> format(some_values, reset: true, to_string: true)
"\e[38;2;240;255;255mThe sky?\e[0m"
ANSI decoration
bold
iex(9)> format([:bold, "important"], to_string: true)
"\e[1mimportant"
dim
iex(10)> format_as_str([:dim, "shady"])
"\e[2mshady"
italic
iex(11)> format_as_str([:italic, "emphasis"])
"\e[3memphasis"
underline
iex(12)> format_as_str([:underline, "under"])
"\e[4munder"
or, for the lazy
iex(13)> format_as_str([:uline, "under"])
"\e[4munder"
Background Coloring
iex(14)> format_as_str([:bg_yellow, "yellow"])
"\e[43myellow"
256 Colors
iex(15)> format([:color242, :color142, :color42])
["\e[38;5;242m", "\e[38;5;142m", "\e[38;5;42m"]
Summary
Functions
iex(20)> color_names() |> Enum.take(2)
[:aqua, :aquamarine1]
iex(21)> color_names(values: true) |> Enum.drop(2) |> Enum.take(4)
[{:aquamarine3, {95, 215, 175}}, {:azure1, {240, 255, 255}}, {:black, 30}, {:blue, 34}]
iex(22)> color_names(grep: "blue") |> Enum.drop(2) |> Enum.take(4)
[:blue3, :blue_violet, :cadet_blue, :cornflower_blue]
iex(16)> colorize("hello")
"hello"
iex(17)> colorize("<red>hello<reset>world")
"\e[31mhello\e[0mworld"
iex(18)> colorize("\\<red>hello<reset>world")
"<red>hello\e[0mworld"
.
before whitespace is not a leader of course
Functions
iex(20)> color_names() |> Enum.take(2)
[:aqua, :aquamarine1]
iex(21)> color_names(values: true) |> Enum.drop(2) |> Enum.take(4)
[{:aquamarine3, {95, 215, 175}}, {:azure1, {240, 255, 255}}, {:black, 30}, {:blue, 34}]
iex(22)> color_names(grep: "blue") |> Enum.drop(2) |> Enum.take(4)
[:blue3, :blue_violet, :cadet_blue, :cornflower_blue]
iex(16)> colorize("hello")
"hello"
iex(17)> colorize("<red>hello<reset>world")
"\e[31mhello\e[0mworld"
iex(18)> colorize("\\<red>hello<reset>world")
"<red>hello\e[0mworld"
.
before whitespace is not a leader of course
iex(19)> colorize("hello\\< world")
"hello< world"