Packages

A terminal styling and logging framework based off the Golang Charm Lipgloss framework.

Current section

Files

Jump to
the_stars src the_stars@color.erl
Raw

src/the_stars@color.erl

-module(the_stars@color).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/the_stars/color.gleam").
-export([rgb/1, to_rgb_hex_string/1, new/3, new_from_hex/1]).
-export_type([color/0]).
-type color() :: {color, integer(), integer(), integer()} | no_color.
-file("src/the_stars/color.gleam", 19).
-spec rgb_to_hex_string(integer()) -> {ok, binary()} |
{error, the_stars@errors:parse_error()}.
rgb_to_hex_string(Value) ->
First = case 16 of
0 -> 0;
Gleam@denominator -> Value div Gleam@denominator
end,
Second = case 16 of
0 -> 0;
Gleam@denominator@1 -> Value rem Gleam@denominator@1
end,
gleam@result:'try'(
begin
_pipe = [First, Second],
gleam@list:try_map(_pipe, fun the_stars@utils:int_to_hex_char/1)
end,
fun(Values) -> {ok, gleam@string:join(Values, <<""/utf8>>)} end
).
-file("src/the_stars/color.gleam", 113).
-spec rgb(color()) -> {integer(), integer(), integer()}.
rgb(Color) ->
case Color of
{color, R, G, B} ->
{R, G, B};
no_color ->
{0, 0, 0}
end.
-file("src/the_stars/color.gleam", 31).
-spec to_rgb_hex_string(color()) -> binary().
to_rgb_hex_string(Color) ->
{R, G, B} = rgb(Color),
R@2 = case rgb_to_hex_string(R) of
{ok, R@1} -> R@1;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"the_stars/color"/utf8>>,
function => <<"to_rgb_hex_string"/utf8>>,
line => 33,
value => _assert_fail,
start => 702,
'end' => 741,
pattern_start => 713,
pattern_end => 718})
end,
G@2 = case rgb_to_hex_string(G) of
{ok, G@1} -> G@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"the_stars/color"/utf8>>,
function => <<"to_rgb_hex_string"/utf8>>,
line => 34,
value => _assert_fail@1,
start => 744,
'end' => 783,
pattern_start => 755,
pattern_end => 760})
end,
B@2 = case rgb_to_hex_string(B) of
{ok, B@1} -> B@1;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"the_stars/color"/utf8>>,
function => <<"to_rgb_hex_string"/utf8>>,
line => 35,
value => _assert_fail@2,
start => 786,
'end' => 825,
pattern_start => 797,
pattern_end => 802})
end,
_pipe = [R@2, G@2, B@2],
gleam@string:join(_pipe, <<""/utf8>>).
-file("src/the_stars/color.gleam", 40).
-spec new(integer(), integer(), integer()) -> color().
new(Red, Green, Blue) ->
Red@1 = the_stars@utils:clamp_int(Red, 0, 255),
Green@1 = the_stars@utils:clamp_int(Green, 0, 255),
Blue@1 = the_stars@utils:clamp_int(Blue, 0, 255),
{color, Red@1, Green@1, Blue@1}.
-file("src/the_stars/color.gleam", 56).
-spec parse_hex_values_aux(list(binary())) -> {ok, color()} |
{error, the_stars@errors:parse_error()}.
parse_hex_values_aux(Chars) ->
gleam@result:'try'(
begin
_pipe = Chars,
_pipe@1 = gleam@list:try_map(
_pipe,
fun the_stars@utils:hex_to_int/1
),
gleam@result:map_error(_pipe@1, fun(_) -> invalid_char end)
end,
fun(Values) ->
Values@1 = begin
_pipe@2 = Values,
_pipe@3 = gleam@list:index_fold(
_pipe@2,
[],
fun(Acc, V, I) -> case (I rem 2) =:= 0 of
true ->
[V * (15 + 1) | Acc];
false ->
{First@1, Acc@2} = case Acc of
[First | Acc@1] -> {First, Acc@1};
_assert_fail ->
erlang:error(
#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"the_stars/color"/utf8>>,
function => <<"parse_hex_values_aux"/utf8>>,
line => 70,
value => _assert_fail,
start => 1861,
'end' => 1892,
pattern_start => 1872,
pattern_end => 1886}
)
end,
[First@1 + V | Acc@2]
end end
),
lists:reverse(_pipe@3)
end,
Color = begin
_pipe@4 = case erlang:length(Values@1) of
3 ->
Values@1;
_ ->
erlang:error(#{gleam_error => panic,
message => (<<"length should already be checked
previously "/utf8,
(erlang:integer_to_binary(
erlang:length(Values@1)
))/binary>>),
file => <<?FILEPATH/utf8>>,
module => <<"the_stars/color"/utf8>>,
function => <<"parse_hex_values_aux"/utf8>>,
line => 80})
end,
gleam@list:index_fold(
_pipe@4,
new(0, 0, 0),
fun(C, V@1, I@1) -> case C of
no_color ->
erlang:error(#{gleam_error => panic,
message => <<"should have a base color as accumulator"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"the_stars/color"/utf8>>,
function => <<"parse_hex_values_aux"/utf8>>,
line => 88});
{color, _, _, _} ->
case I@1 of
0 ->
{color,
V@1,
erlang:element(3, C),
erlang:element(4, C)};
1 ->
{color,
erlang:element(2, C),
V@1,
erlang:element(4, C)};
2 ->
{color,
erlang:element(2, C),
erlang:element(3, C),
V@1};
_ ->
erlang:error(#{gleam_error => panic,
message => <<"length should already be checked previously"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"the_stars/color"/utf8>>,
function => <<"parse_hex_values_aux"/utf8>>,
line => 94})
end
end end
)
end,
{ok, Color}
end
).
-file("src/the_stars/color.gleam", 104).
-spec parse_hex_values(binary()) -> {ok, color()} |
{error, the_stars@errors:parse_error()}.
parse_hex_values(S) ->
Chars = gleam@string:to_graphemes(S),
case erlang:length(Chars) of
6 ->
parse_hex_values_aux(Chars);
_ ->
{error, invalid_length}
end.
-file("src/the_stars/color.gleam", 48).
-spec new_from_hex(binary()) -> {ok, color()} |
{error, the_stars@errors:parse_error()}.
new_from_hex(S) ->
S@1 = gleam@string:trim(S),
case gleam_stdlib:string_starts_with(S@1, <<"#"/utf8>>) of
true ->
new_from_hex(gleam@string:drop_start(S@1, 1));
false ->
parse_hex_values(S@1)
end.