Current section
Files
Jump to
Current section
Files
src/ansel@bounding_box.erl
-module(ansel@bounding_box).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([ltwh/4, unchecked_ltwh/4, ltrb/4, x1y1x2y2/4, to_ltwh_tuple/1, to_ltrb_tuple/1, to_x1y1x2y2_tuple/1, shrink/2, expand/2, scale/2, cut/2, intersection/2, fit/2, make_relative/2]).
-export_type([bounding_box/0]).
-opaque bounding_box() :: {ltwh, integer(), integer(), integer(), integer()} |
{ltrb, integer(), integer(), integer(), integer()} |
{x1y1x2y2, integer(), integer(), integer(), integer()}.
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 27).
-spec ltwh(integer(), integer(), integer(), integer()) -> {ok, bounding_box()} |
{error, snag:snag()}.
ltwh(Left, Top, Width, Height) ->
case (((Width > 0) andalso (Height > 0)) andalso (Left >= 0)) andalso (Top
>= 0) of
true ->
{ok, {ltwh, Left, Top, Width, Height}};
false ->
snag:error(<<"Impossible ltwh bounding box values passed"/utf8>>)
end.
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 40).
-spec unchecked_ltwh(integer(), integer(), integer(), integer()) -> bounding_box().
unchecked_ltwh(Left, Top, Width, Height) ->
{ltwh, Left, Top, Width, Height}.
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 51).
-spec ltrb(integer(), integer(), integer(), integer()) -> {ok, bounding_box()} |
{error, snag:snag()}.
ltrb(Left, Top, Right, Bottom) ->
case (((Left < Right) andalso (Top < Bottom)) andalso (Left >= 0)) andalso (Top
>= 0) of
true ->
{ok, {ltrb, Left, Top, Right, Bottom}};
false ->
snag:error(<<"Impossible ltrb bounding box values passed"/utf8>>)
end.
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 65).
-spec x1y1x2y2(integer(), integer(), integer(), integer()) -> {ok,
bounding_box()} |
{error, snag:snag()}.
x1y1x2y2(X1, Y1, X2, Y2) ->
case (((X1 < X2) andalso (Y1 < Y2)) andalso (X1 >= 0)) andalso (Y1 >= 0) of
true ->
{ok, {x1y1x2y2, X1, Y1, X2, Y2}};
false ->
snag:error(
<<"Impossible x1y1x2y2 bounding box values passed"/utf8>>
)
end.
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 93).
-spec to_ltwh_tuple(bounding_box()) -> {integer(),
integer(),
integer(),
integer()}.
to_ltwh_tuple(Bounding_box) ->
case Bounding_box of
{ltwh, Left, Top, Width, Height} ->
{Left, Top, Width, Height};
{ltrb, Left@1, Top@1, Right, Bottom} ->
{Left@1, Top@1, Right - Left@1, Bottom - Top@1};
{x1y1x2y2, X1, Y1, X2, Y2} ->
{X1, Y1, X2 - X1, Y2 - Y1}
end.
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 110).
-spec to_ltrb_tuple(bounding_box()) -> {integer(),
integer(),
integer(),
integer()}.
to_ltrb_tuple(Bounding_box) ->
case Bounding_box of
{ltwh, Left, Top, Width, Height} ->
{Left, Top, Left + Width, Top + Height};
{ltrb, Left@1, Top@1, Right, Bottom} ->
{Left@1, Top@1, Right, Bottom};
{x1y1x2y2, X1, Y1, X2, Y2} ->
{X1, Y1, X2, Y2}
end.
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 127).
-spec to_x1y1x2y2_tuple(bounding_box()) -> {integer(),
integer(),
integer(),
integer()}.
to_x1y1x2y2_tuple(Bounding_box) ->
case Bounding_box of
{ltwh, Left, Top, Width, Height} ->
{Left, Top, Left + Width, Top + Height};
{ltrb, Left@1, Top@1, Right, Bottom} ->
{Left@1, Top@1, Right, Bottom};
{x1y1x2y2, X1, Y1, X2, Y2} ->
{X1, Y1, X2, Y2}
end.
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 138).
-spec shrink(bounding_box(), integer()) -> {ok, bounding_box()} | {error, nil}.
shrink(Bounding_box, Amount) ->
gleam@bool:guard(
Amount < 0,
{ok, Bounding_box},
fun() ->
{_, _, Width, Height} = to_ltwh_tuple(Bounding_box),
gleam@bool:guard(
((Amount * 2) >= Width) orelse ((Amount * 2) >= Height),
{error, nil},
fun() -> _pipe = case Bounding_box of
{ltwh, Left, Top, Width@1, Height@1} ->
{ltwh,
Left + Amount,
Top + Amount,
gleam@int:max(Width@1 - (Amount * 2), 0),
gleam@int:max(Height@1 - (Amount * 2), 0)};
{ltrb, Left@1, Top@1, Right, Bottom} ->
{ltrb,
Left@1 + Amount,
Top@1 + Amount,
gleam@int:max(Right - Amount, 0),
gleam@int:max(Bottom - Amount, 0)};
{x1y1x2y2, X1, Y1, X2, Y2} ->
{x1y1x2y2,
X1 + Amount,
Y1 + Amount,
gleam@int:max(X2 - Amount, 0),
gleam@int:max(Y2 - Amount, 0)}
end,
{ok, _pipe} end
)
end
).
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 179).
-spec expand(bounding_box(), integer()) -> bounding_box().
expand(Bounding_box, Amount) ->
gleam@bool:guard(Amount < 0, Bounding_box, fun() -> case Bounding_box of
{ltwh, Left, Top, Width, Height} ->
{ltwh,
gleam@int:max(Left - Amount, 0),
gleam@int:max(Top - Amount, 0),
Width + (Amount * 2),
Height + (Amount * 2)};
{ltrb, Left@1, Top@1, Right, Bottom} ->
{ltrb,
gleam@int:max(Left@1 - Amount, 0),
gleam@int:max(Top@1 - Amount, 0),
Right + Amount,
Bottom + Amount};
{x1y1x2y2, X1, Y1, X2, Y2} ->
{x1y1x2y2,
gleam@int:max(X1 - Amount, 0),
gleam@int:max(Y1 - Amount, 0),
X2 + Amount,
Y2 + Amount}
end end).
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 208).
-spec scale(bounding_box(), float()) -> bounding_box().
scale(Bounding_box, Scale) ->
{Left, Top, Right, Bottom} = to_ltrb_tuple(Bounding_box),
{ltrb,
gleam@float:round(gleam@int:to_float(Left) * Scale),
gleam@float:round(gleam@int:to_float(Top) * Scale),
gleam@float:round(gleam@int:to_float(Right) * Scale),
gleam@float:round(gleam@int:to_float(Bottom) * Scale)}.
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 221).
-spec cut(bounding_box(), bounding_box()) -> list(bounding_box()).
cut(To_cut, Cutter) ->
{X1, Y1, W1, H1} = to_ltwh_tuple(To_cut),
{X2, Y2, W2, H2} = to_ltwh_tuple(Cutter),
Int_left = gleam@int:max(X1, X2),
Int_top = gleam@int:max(Y1, Y2),
Int_right = gleam@int:min(X1 + W1, X2 + W2),
Int_bottom = gleam@int:min(Y1 + H1, Y2 + H2),
gleam@bool:guard(
(Int_left >= Int_right) orelse (Int_top >= Int_bottom),
[To_cut],
fun() ->
Cut_pieces = [case Y1 < Int_top of
true ->
{some, {ltwh, X1, Y1, W1, Int_top - Y1}};
false ->
none
end, case X1 < Int_left of
true ->
{some,
{ltwh,
X1,
Int_top,
Int_left - X1,
Int_bottom - Int_top}};
false ->
none
end, case Int_right < (X1 + W1) of
true ->
{some,
{ltwh,
Int_right,
Int_top,
(X1 + W1) - Int_right,
Int_bottom - Int_top}};
false ->
none
end, case Int_bottom < (Y1 + H1) of
true ->
{some,
{ltwh, X1, Int_bottom, W1, (Y1 + H1) - Int_bottom}};
false ->
none
end],
gleam@option:values(Cut_pieces)
end
).
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 284).
-spec intersection(bounding_box(), bounding_box()) -> gleam@option:option(bounding_box()).
intersection(Box1, Box2) ->
{L1, T1, R1, B1} = to_ltrb_tuple(Box1),
{L2, T2, R2, B2} = to_ltrb_tuple(Box2),
gleam@bool:guard(
(((L1 >= L2) andalso (T1 >= T2)) andalso (R1 =< R2)) andalso (B1 =< B2),
{some, Box1},
fun() ->
gleam@bool:guard(
(((L1 =< L2) andalso (T1 =< T2)) andalso (R1 >= R2)) andalso (B1
>= B2),
{some, Box2},
fun() ->
Left = gleam@int:max(L1, L2),
Top = gleam@int:max(T1, T2),
Right = gleam@int:min(R1, R2),
Bottom = gleam@int:min(B1, B2),
gleam@bool:guard(
(Left >= Right) orelse (Top >= Bottom),
none,
fun() -> _pipe = {ltrb, Left, Top, Right, Bottom},
{some, _pipe} end
)
end
)
end
).
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 314).
-spec fit(bounding_box(), bounding_box()) -> gleam@option:option(bounding_box()).
fit(Box, Reference) ->
{_, _, Width, Height} = to_ltwh_tuple(Reference),
{Left, Top, Right, Bottom} = to_ltrb_tuple(Box),
case {Left < Width, Top < Height} of
{true, true} ->
{some,
{ltrb,
Left,
Top,
gleam@int:min(Right, Width),
gleam@int:min(Bottom, Height)}};
{_, _} ->
none
end.
-file("/home/john/Repos/ansel/src/ansel/bounding_box.gleam", 355).
-spec make_relative(bounding_box(), bounding_box()) -> gleam@option:option(bounding_box()).
make_relative(Box, Reference) ->
{Left, Top, Right, Bottom} = to_ltrb_tuple(Box),
{Ref_left, Ref_top, _, _} = to_ltwh_tuple(Reference),
Adj_box = {ltrb,
gleam@int:max(Left - Ref_left, 0),
gleam@int:max(Top - Ref_top, 0),
gleam@int:max(Right - Ref_left, 0),
gleam@int:max(Bottom - Ref_top, 0)},
case Adj_box of
{ltrb, 0, 0, 0, 0} ->
none;
_ ->
fit(Adj_box, Reference)
end.