Skip to content

Commit

Permalink
Add Owl.TrueColor module
Browse files Browse the repository at this point in the history
  • Loading branch information
fuelen committed Jul 21, 2024
1 parent 0ae928f commit 30f6093
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/owl/data/sequence.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ defmodule Owl.Data.Sequence do
case binary do
"\e[38;5;" <> _ -> binary
"\e[48;5;" <> _ -> binary
"\e[38;2;" <> _ -> binary
"\e[48;2;" <> _ -> binary
_ -> name_by_sequence(binary)
end
end
Expand All @@ -48,6 +50,8 @@ defmodule Owl.Data.Sequence do
def type!(sequence) when is_atom(sequence), do: type_by_name(sequence)
def type!("\e[38;5;" <> _), do: :foreground
def type!("\e[48;5;" <> _), do: :background
def type!("\e[38;2;" <> _), do: :foreground
def type!("\e[48;2;" <> _), do: :background

@doc """
Get the default value of a sequence type.
Expand Down
34 changes: 34 additions & 0 deletions lib/owl/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,40 @@ defmodule Owl.Tag do
concat(["IO.ANSI.color_background(", to_doc(number, opts), ")"])
end

defp inspect_sequence("\e[38;2;" <> rest, opts) do
[r, g, b] = String.split(rest, ";")
{r, ""} = Integer.parse(r)
{g, ""} = Integer.parse(g)
{b, "m"} = Integer.parse(b)

concat([
"Owl.TrueColor.color(",
to_doc(r, opts),
",",
to_doc(g, opts),
",",
to_doc(b, opts),
")"
])
end

defp inspect_sequence("\e[48;2;" <> rest, opts) do
[r, g, b] = String.split(rest, ";")
{r, ""} = Integer.parse(r)
{g, ""} = Integer.parse(g)
{b, "m"} = Integer.parse(b)

concat([
"Owl.TrueColor.color_background(",
to_doc(r, opts),
",",
to_doc(g, opts),
",",
to_doc(b, opts),
")"
])
end

defp inspect_sequence(sequence, opts) do
to_doc(sequence, opts)
end
Expand Down
36 changes: 36 additions & 0 deletions lib/owl/true_color.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Owl.TrueColor do
@moduledoc """
A module for true color escape sequences
## Example
"Hello"
|> Owl.Data.tag([Owl.TrueColor.color(1, 244, 74), Owl.TrueColor.color_background(133, 48, 100)])
|> Owl.IO.puts()
"""

@doc ~S"""
Returns a true color foreground escape sequence for the given RGB values.
## Example
iex> Owl.TrueColor.color(1, 244, 74)
"\e[38;2;1;244;74m"
"""
@spec color(0..255, 0..255, 0..255) :: String.t()
def color(r, g, b) when r in 0..255 and g in 0..255 and b in 0..255 do
"\e[38;2;#{r};#{g};#{b}m"
end

@doc ~S"""
Returns a true color background escape sequence for the given RGB values.
## Example
iex> Owl.TrueColor.color_background(133, 48, 100)
"\e[48;2;133;48;100m"
"""
@spec color_background(0..255, 0..255, 0..255) :: String.t()
def color_background(r, g, b) when r in 0..255 and g in 0..255 and b in 0..255 do
"\e[48;2;#{r};#{g};#{b}m"
end
end
4 changes: 4 additions & 0 deletions test/owl/true_color_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Owl.TrueColorTest do
use ExUnit.Case, async: true
doctest Owl.TrueColor
end

0 comments on commit 30f6093

Please sign in to comment.