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, main/0, by_hexcolour/2, by_colour/2, by_hexcolour_bg/2, by_colour_bg/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" <h1 class=\"atx\" id=\"gleamy_lights\">gleamy_lights</h1>\n"
" <h2 class=\"atx\" id=\"modifiers\">Modifiers</h2>\n"
" <p>Modifiers are the functions that modify a string to add a colour to it. Premixes use these, and so are not counted as modifiers.</p>\n"
" <p>Among these are:</p>\n"
" <h6 class=\"atx\" id=\"background\">Background</h6>\n"
" <ul>\n"
" <li><p><a href=\"/gleamy_lights/gleamy_lights#by_colour_bg\"><code>by_colour_bg</code></a></p>\n"
" </li>\n"
" <li><p><a href=\"/gleamy_lights/gleamy_lights#by_rgb_bg\"><code>by_rgb_bg</code></a></p>\n"
" </li>\n"
" <li><p><a href=\"/gleamy_lights/gleamy_lights#by_hexcolour_bg\"><code>by_hexcolour_bg</code></a></p>\n"
" </li>\n"
" </ul>\n"
" <h6 class=\"atx\" id=\"foreground\">Foreground</h6>\n"
" <ul>\n"
" <li><p><a href=\"/gleamy_lights/gleamy_lights#by_colour\"><code>by_colour</code></a></p>\n"
" </li>\n"
" <li><p><a href=\"/gleamy_lights/gleamy_lights#by_rgb\"><code>by_rgb</code></a></p>\n"
" </li>\n"
" <li><p><a href=\"/gleamy_lights/gleamy_lights#by_hexcolour\"><code>by_hexcolour</code></a></p>\n"
" </li>\n"
" </ul>\n"
" <h2 class=\"atx\" id=\"premix-pallettes\">Helpers</h2>\n"
" <p>Helper functions</p>\n"
" <ul>\n"
" <li><p><a href=\"/gleamy_lights/gleamy_lights/helper#println\"><code>println</code></a></p>\n"
" </li>\n"
" <h2 class=\"atx\" id=\"premix-pallettes\">Premix pallettes</h2>\n"
" <p>These are modules within gleamy_lights allowing you to use a modifier with no need to enter the colour codes.</p>\n"
" <ul>\n"
" <li><p><a href=\"/gleamy_lights/gleamy_lights/premixed\">Premixed colours pallette</a></p>\n"
" </li>\n"
" <li><p><a href=\"/gleamy_lights/gleamy_lights/premixed/gleam_colours\">Gleam colours pallette</a></p>\n"
" </li>\n"
" </ul>\n"
).
-file("src/gleamy_lights.gleam", 71).
?DOC(
" # By RGB\n"
" Given a string and an RGB color, return the message with the color applied.\n"
" The RGB values should be between 0 and 255.\n"
).
-spec by_rgb(binary(), integer(), integer(), integer()) -> binary().
by_rgb(Msg, R, G, B) ->
F = fun() ->
<<<<<<<<<<<<<<<<"\x{001b}[38;2;"/utf8,
(erlang:integer_to_binary(R))/binary>>/binary,
";"/utf8>>/binary,
(erlang:integer_to_binary(G))/binary>>/binary,
";"/utf8>>/binary,
(erlang:integer_to_binary(B))/binary>>/binary,
"m"/utf8>>/binary,
Msg/binary>>/binary,
"\x{001b}[0m"/utf8>>
end,
case envoy_ffi:get(<<"NO_COLOR"/utf8>>) of
{ok, <<""/utf8>>} ->
F();
{ok, _} ->
Msg;
_ ->
F()
end.
-file("src/gleamy_lights.gleam", 101).
?DOC(
" # By RGB - Background\n"
" Given a string and an RGB colour, return the message with the background colour applied.\n"
" The RGB values should be between 0 and 255.\n"
).
-spec by_rgb_bg(binary(), integer(), integer(), integer()) -> binary().
by_rgb_bg(Msg, R, G, B) ->
F = fun() ->
<<<<<<<<<<<<<<<<"\x{001b}[48;2;"/utf8,
(erlang:integer_to_binary(R))/binary>>/binary,
";"/utf8>>/binary,
(erlang:integer_to_binary(G))/binary>>/binary,
";"/utf8>>/binary,
(erlang:integer_to_binary(B))/binary>>/binary,
"m"/utf8>>/binary,
Msg/binary>>/binary,
"\x{001b}[0m"/utf8>>
end,
case envoy_ffi:get(<<"NO_COLOR"/utf8>>) of
{ok, <<""/utf8>>} ->
F();
{ok, _} ->
Msg;
_ ->
F()
end.
-file("src/gleamy_lights.gleam", 39).
-spec main() -> nil.
main() ->
gleam_stdlib:println(by_rgb_bg(<<"Hello, world!"/utf8>>, 255, 0, 0)).
-file("src/gleamy_lights.gleam", 128).
?DOC(" (Internal) Convert a hex color to an RGB list.\n").
-spec hexcolour_to_rgb(binary()) -> list(integer()).
hexcolour_to_rgb(Hex) ->
Hexcolour = gleam@string:replace(Hex, <<"#"/utf8>>, <<""/utf8>>),
R@1 = case gleam@int:base_parse(gleam@string:slice(Hexcolour, 0, 2), 16) of
{ok, R} ->
R;
{error, _} ->
0
end,
G@1 = case gleam@int:base_parse(gleam@string:slice(Hexcolour, 2, 2), 16) of
{ok, G} ->
G;
{error, _} ->
0
end,
B@1 = case gleam@int:base_parse(gleam@string:slice(Hexcolour, 4, 2), 16) of
{ok, B} ->
B;
{error, _} ->
0
end,
[R@1, G@1, B@1].
-file("src/gleamy_lights.gleam", 161).
?DOC(
" # By Hex colour\n"
" Given a string and a hex colour, return the message with the colour applied.\n"
"\n"
" Will return black if the hex colour is invalid.\n"
).
-spec by_hexcolour(binary(), binary()) -> binary().
by_hexcolour(Msg, Hexcolour) ->
Rgb = hexcolour_to_rgb(Hexcolour),
_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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolour"/utf8>>,
line => 163})
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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolour"/utf8>>,
line => 166})
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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@2,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolour"/utf8>>,
line => 169})
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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@3,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolour"/utf8>>,
line => 172})
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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@4,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolour"/utf8>>,
line => 175})
end,
_pipe@5 = Msg,
by_rgb(_pipe@5, R, G, B).
-file("src/gleamy_lights.gleam", 54).
?DOC(
" # By Colour\n"
" Given a string and a <code><a href=\"https://hexdocs.pm/gleam_community_colour/gleam_community/colour.html#Colour\">Colour</a></code>, return the message with the colour applied.\n"
).
-spec by_colour(binary(), gleam_community@colour:colour()) -> binary().
by_colour(Msg, Clr) ->
by_hexcolour(Msg, gleam_community@colour:to_rgb_hex_string(Clr)).
-file("src/gleamy_lights.gleam", 187).
?DOC(
" # By Hexcolour - Background\n"
" Given a string and a hex colour, return the message with the background color applied.\n"
" Will return black if the hex colour is invalid.\n"
).
-spec by_hexcolour_bg(binary(), binary()) -> binary().
by_hexcolour_bg(Msg, Hexcolour) ->
Rgb = hexcolour_to_rgb(Hexcolour),
_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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolour_bg"/utf8>>,
line => 189})
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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolour_bg"/utf8>>,
line => 192})
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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@2,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolour_bg"/utf8>>,
line => 195})
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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@3,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolour_bg"/utf8>>,
line => 198})
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 => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@4,
module => <<"gleamy_lights"/utf8>>,
function => <<"by_hexcolour_bg"/utf8>>,
line => 201})
end,
_pipe@5 = Msg,
by_rgb_bg(_pipe@5, R, G, B).
-file("src/gleamy_lights.gleam", 64).
?DOC(
" # By Colour\n"
" Given a string and a <code><a href=\"https://hexdocs.pm/gleam_community_colour/gleam_community/colour.html#Colour\">Colour</a></code>, return the message with the background colour applied.\n"
).
-spec by_colour_bg(binary(), gleam_community@colour:colour()) -> binary().
by_colour_bg(Msg, Clr) ->
by_hexcolour_bg(Msg, gleam_community@colour:to_rgb_hex_string(Clr)).