vivid_png/lib/vivid/png/shape.ex

33 lines
799 B
Elixir
Raw Normal View History

2017-01-12 08:40:31 +13:00
defmodule Vivid.PNG.ShapeToPng do
alias Vivid.{Bounds, Frame, PNG, RGBA, Shape, Transform}
2017-01-12 08:40:31 +13:00
2017-01-12 08:55:58 +13:00
@moduledoc false
@doc false
2018-09-04 14:52:12 +12:00
@spec to_png(Shape.t(), Path.t()) :: :ok | {:error, any}
2017-01-12 08:40:31 +13:00
def to_png(shape, file) do
bounds = Bounds.bounds(shape)
2018-09-04 14:52:12 +12:00
width = bounds |> Bounds.width() |> round |> Kernel.+(3)
height = bounds |> Bounds.height() |> round |> Kernel.+(3)
2017-01-12 08:40:31 +13:00
frame = Frame.init(width, height)
2018-09-04 14:52:12 +12:00
shape =
shape
2017-01-12 08:40:31 +13:00
|> Transform.center(frame)
2018-09-04 14:52:12 +12:00
|> Transform.apply()
2017-01-12 08:40:31 +13:00
frame
2018-09-04 14:52:12 +12:00
|> Frame.push(shape, RGBA.black())
2017-01-12 08:40:31 +13:00
|> PNG.to_png(file)
end
end
Enum.each(~w(Arc Box Circle Group Line Path Polygon), fn mod ->
mod = Module.concat(Vivid, mod)
2018-09-04 14:52:12 +12:00
2017-01-12 08:40:31 +13:00
defimpl Vivid.PNG, for: mod do
def to_png(shape, file), do: Vivid.PNG.ShapeToPng.to_png(shape, file)
end
2018-09-04 14:52:12 +12:00
end)