Current section

Files

Jump to
gleamy_lights src gleamy_lights.erl
Raw

src/gleamy_lights.erl

-module(gleamy_lights).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([by_rgb/4, by_rgb_bg/4, by_hexcolor/2, by_hexcolor_bg/2]).
-spec by_rgb(binary(), integer(), integer(), integer()) -> binary().
by_rgb(Msg, R, G, B) ->
case envoy_ffi:get(<<"NO_COLOR"/utf8>>) of
{ok, <<"1"/utf8>>} ->
Msg;
_ ->
<<<<<<<<<<<<<<<<"\x{001b}[38;2;"/utf8,
(gleam@int:to_string(R))/binary>>/binary,
";"/utf8>>/binary,
(gleam@int:to_string(G))/binary>>/binary,
";"/utf8>>/binary,
(gleam@int:to_string(B))/binary>>/binary,
"m"/utf8>>/binary,
Msg/binary>>/binary,
"\x{001b}[0m"/utf8>>
end.
-spec by_rgb_bg(binary(), integer(), integer(), integer()) -> binary().
by_rgb_bg(Msg, R, G, B) ->
<<<<<<<<<<<<<<<<"\x{001b}[48;2;"/utf8, (gleam@int:to_string(R))/binary>>/binary,
";"/utf8>>/binary,
(gleam@int:to_string(G))/binary>>/binary,
";"/utf8>>/binary,
(gleam@int:to_string(B))/binary>>/binary,
"m"/utf8>>/binary,
Msg/binary>>/binary,
"\x{001b}[0m"/utf8>>.
-spec hexcolor_to_rgb(binary()) -> list(integer()).
hexcolor_to_rgb(Hex) ->
Hexcolor = gleam@string:replace(Hex, <<"#"/utf8>>, <<""/utf8>>),
R@1 = case gleam@int:base_parse(gleam@string:slice(Hexcolor, 0, 2), 16) of
{ok, R} ->
R;
{error, _} ->
0
end,
G@1 = case gleam@int:base_parse(gleam@string:slice(Hexcolor, 2, 2), 16) of
{ok, G} ->
G;
{error, _} ->
0
end,
B@1 = case gleam@int:base_parse(gleam@string:slice(Hexcolor, 4, 2), 16) of
{ok, B} ->
B;
{error, _} ->
0
end,
[R@1, G@1, B@1].
-spec by_hexcolor(binary(), binary()) -> binary().
by_hexcolor(Msg, Hexcolor) ->
Rgb = hexcolor_to_rgb(Hexcolor),
_assert_subject = begin
_pipe = Rgb,
gleam@list:first(_pipe)
end,
{ok, R} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolor"/utf8>>,
line => 80})
end,
_assert_subject@1 = begin
_pipe@1 = Rgb,
gleam@list:rest(_pipe@1)
end,
{ok, Rest} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolor"/utf8>>,
line => 83})
end,
_assert_subject@2 = begin
_pipe@2 = Rest,
gleam@list:first(_pipe@2)
end,
{ok, G} = case _assert_subject@2 of
{ok, _} -> _assert_subject@2;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@2,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolor"/utf8>>,
line => 86})
end,
_assert_subject@3 = begin
_pipe@3 = Rest,
gleam@list:rest(_pipe@3)
end,
{ok, Rest@1} = case _assert_subject@3 of
{ok, _} -> _assert_subject@3;
_assert_fail@3 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@3,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolor"/utf8>>,
line => 89})
end,
_assert_subject@4 = begin
_pipe@4 = Rest@1,
gleam@list:first(_pipe@4)
end,
{ok, B} = case _assert_subject@4 of
{ok, _} -> _assert_subject@4;
_assert_fail@4 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@4,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolor"/utf8>>,
line => 92})
end,
_pipe@5 = Msg,
by_rgb(_pipe@5, R, G, B).
-spec by_hexcolor_bg(binary(), binary()) -> binary().
by_hexcolor_bg(Msg, Hexcolor) ->
Rgb = hexcolor_to_rgb(Hexcolor),
_assert_subject = begin
_pipe = Rgb,
gleam@list:first(_pipe)
end,
{ok, R} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolor_bg"/utf8>>,
line => 103})
end,
_assert_subject@1 = begin
_pipe@1 = Rgb,
gleam@list:rest(_pipe@1)
end,
{ok, Rest} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolor_bg"/utf8>>,
line => 106})
end,
_assert_subject@2 = begin
_pipe@2 = Rest,
gleam@list:first(_pipe@2)
end,
{ok, G} = case _assert_subject@2 of
{ok, _} -> _assert_subject@2;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@2,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolor_bg"/utf8>>,
line => 109})
end,
_assert_subject@3 = begin
_pipe@3 = Rest,
gleam@list:rest(_pipe@3)
end,
{ok, Rest@1} = case _assert_subject@3 of
{ok, _} -> _assert_subject@3;
_assert_fail@3 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@3,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolor_bg"/utf8>>,
line => 112})
end,
_assert_subject@4 = begin
_pipe@4 = Rest@1,
gleam@list:first(_pipe@4)
end,
{ok, B} = case _assert_subject@4 of
{ok, _} -> _assert_subject@4;
_assert_fail@4 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@4,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolor_bg"/utf8>>,
line => 115})
end,
_pipe@5 = Msg,
by_rgb_bg(_pipe@5, R, G, B).