Packages

A Gleam library for deterministic string to color conversion.

Current section

Files

Jump to
colorhash src colorhash.erl
Raw

src/colorhash.erl

-module(colorhash).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/colorhash.gleam").
-export([new/0, with_saturations/2, with_lightnesses/2, with_hue_ranges/2, to_hsl/2, to_rgb/2, to_hex/2]).
-export_type([color_hash/0]).
-type color_hash() :: {color_hash,
gleam@dict:dict(integer(), float()),
gleam@dict:dict(integer(), float()),
gleam@dict:dict(integer(), {float(), float()})}.
-file("src/colorhash.gleam", 16).
-spec new() -> color_hash().
new() ->
{color_hash,
begin
_pipe = [0.35, 0.5, 0.65],
_pipe@1 = gleam@list:index_map(_pipe, fun(X, I) -> {I, X} end),
maps:from_list(_pipe@1)
end,
begin
_pipe@2 = [0.35, 0.5, 0.65],
_pipe@3 = gleam@list:index_map(
_pipe@2,
fun(X@1, I@1) -> {I@1, X@1} end
),
maps:from_list(_pipe@3)
end,
begin
_pipe@4 = [{+0.0, 359.0}],
_pipe@5 = gleam@list:index_map(
_pipe@4,
fun(X@2, I@2) -> {I@2, X@2} end
),
maps:from_list(_pipe@5)
end}.
-file("src/colorhash.gleam", 30).
-spec with_saturations(color_hash(), list(float())) -> color_hash().
with_saturations(Color_hash, Saturations) ->
_record = Color_hash,
{color_hash,
begin
_pipe = Saturations,
_pipe@1 = gleam@list:index_map(_pipe, fun(X, I) -> {I, X} end),
maps:from_list(_pipe@1)
end,
erlang:element(3, _record),
erlang:element(4, _record)}.
-file("src/colorhash.gleam", 42).
-spec with_lightnesses(color_hash(), list(float())) -> color_hash().
with_lightnesses(Color_hash, Lightnesses) ->
_record = Color_hash,
{color_hash,
erlang:element(2, _record),
begin
_pipe = Lightnesses,
_pipe@1 = gleam@list:index_map(_pipe, fun(X, I) -> {I, X} end),
maps:from_list(_pipe@1)
end,
erlang:element(4, _record)}.
-file("src/colorhash.gleam", 54).
-spec with_hue_ranges(color_hash(), list({float(), float()})) -> color_hash().
with_hue_ranges(Color_hash, Hue_ranges) ->
_record = Color_hash,
{color_hash,
erlang:element(2, _record),
erlang:element(3, _record),
begin
_pipe = Hue_ranges,
_pipe@1 = gleam@list:index_map(_pipe, fun(X, I) -> {I, X} end),
maps:from_list(_pipe@1)
end}.
-file("src/colorhash.gleam", 66).
-spec to_hsl(color_hash(), binary()) -> {float(), float(), float()}.
to_hsl(Color_hash, Input) ->
Default_color_hash = new(),
Hue_ranges = case maps:size(erlang:element(4, Color_hash)) of
0 ->
erlang:element(4, Default_color_hash);
_ ->
erlang:element(4, Color_hash)
end,
Saturations = case maps:size(erlang:element(2, Color_hash)) of
0 ->
erlang:element(2, Default_color_hash);
_ ->
erlang:element(2, Color_hash)
end,
Lightnesses = case maps:size(erlang:element(3, Color_hash)) of
0 ->
erlang:element(3, Default_color_hash);
_ ->
erlang:element(3, Color_hash)
end,
Hash@1 = case gleam@crypto:hash(sha256, gleam_stdlib:identity(Input)) of
<<Hash:32, _/bitstring>> -> Hash;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"colorhash"/utf8>>,
function => <<"to_hsl"/utf8>>,
line => 81,
value => _assert_fail,
start => 1874,
'end' => 1967,
pattern_start => 1885,
pattern_end => 1904})
end,
Hue_resolution = 727,
{Hue_range_start@1, Hue_range_end@1} = case gleam_stdlib:map_get(
Hue_ranges,
case maps:size(Hue_ranges) of
0 -> 0;
Gleam@denominator -> Hash@1 rem Gleam@denominator
end
) of
{ok, {Hue_range_start, Hue_range_end}} -> {
Hue_range_start,
Hue_range_end};
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"colorhash"/utf8>>,
function => <<"to_hsl"/utf8>>,
line => 85,
value => _assert_fail@1,
start => 1998,
'end' => 2103,
pattern_start => 2009,
pattern_end => 2046})
end,
H = (case erlang:float(Hue_resolution) of
+0.0 -> +0.0;
-0.0 -> -0.0;
Gleam@denominator@3 -> erlang:float(case Hue_resolution of
0 -> 0;
Gleam@denominator@2 -> (case maps:size(Hue_ranges) of
0 -> 0;
Gleam@denominator@1 -> Hash@1 div Gleam@denominator@1
end) rem Gleam@denominator@2
end) * (Hue_range_end@1 - Hue_range_start@1) / Gleam@denominator@3
end) + Hue_range_start@1,
S@1 = case gleam_stdlib:map_get(Saturations, case maps:size(Saturations) of
0 -> 0;
Gleam@denominator@4 -> (Hash@1 div 360) rem Gleam@denominator@4
end) of
{ok, S} -> S;
_assert_fail@2 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"colorhash"/utf8>>,
function => <<"to_hsl"/utf8>>,
line => 93,
value => _assert_fail@2,
start => 2288,
'end' => 2373,
pattern_start => 2299,
pattern_end => 2304})
end,
L@1 = case gleam_stdlib:map_get(Lightnesses, case maps:size(Saturations) of
0 -> 0;
Gleam@denominator@6 -> (case maps:size(Saturations) of
0 -> 0;
Gleam@denominator@5 -> Hash@1 div 360 div Gleam@denominator@5
end) rem Gleam@denominator@6
end) of
{ok, L} -> L;
_assert_fail@3 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"colorhash"/utf8>>,
function => <<"to_hsl"/utf8>>,
line => 96,
value => _assert_fail@3,
start => 2377,
'end' => 2506,
pattern_start => 2388,
pattern_end => 2393})
end,
{H, S@1, L@1}.
-file("src/colorhash.gleam", 115).
-spec hsl_to_rgb(float(), float(), float()) -> {integer(), integer(), integer()}.
hsl_to_rgb(H, S, L) ->
C = (1.0 - gleam@float:absolute_value((2.0 * L) - 1.0)) * S,
H_prime = H / 60.0,
H_mod_2 = H_prime - (math:floor(H_prime / 2.0) * 2.0),
X = C * (1.0 - gleam@float:absolute_value(H_mod_2 - 1.0)),
M = L - (C / 2.0),
{R1, G1, B1} = case H of
H@1 when H@1 < 60.0 ->
{C, X, +0.0};
H@2 when H@2 < 120.0 ->
{X, C, +0.0};
H@3 when H@3 < 180.0 ->
{+0.0, C, X};
H@4 when H@4 < 240.0 ->
{+0.0, X, C};
H@5 when H@5 < 300.0 ->
{X, +0.0, C};
_ ->
{C, +0.0, X}
end,
{erlang:round((R1 + M) * 255.0),
erlang:round((G1 + M) * 255.0),
erlang:round((B1 + M) * 255.0)}.
-file("src/colorhash.gleam", 105).
-spec to_rgb(color_hash(), binary()) -> {integer(), integer(), integer()}.
to_rgb(Color_hash, Input) ->
{H, S, L} = to_hsl(Color_hash, Input),
hsl_to_rgb(H, S, L).
-file("src/colorhash.gleam", 110).
-spec to_hex(color_hash(), binary()) -> binary().
to_hex(Color_hash, Input) ->
{R, G, B} = to_rgb(Color_hash, Input),
<<<<<<"#"/utf8, (gleam@int:to_base16(R))/binary>>/binary,
(gleam@int:to_base16(G))/binary>>/binary,
(gleam@int:to_base16(B))/binary>>.