diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..2bed17c --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,3 @@ +[ + inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..c1a576f --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,29 @@ +image: elixir:latest + +cache: + key: "$CI_JOB_NAME" + paths: + - deps + - _build + - /root/.mix + +variables: + MIX_ENV: "test" + +before_script: + - mix local.hex --force + - mix local.rebar --force + - mix deps.get --only test + +test: + script: + - mix test + +credo: + script: + - mix credo --strict + +# inch: +# script: +# - mix inch --pedantic + diff --git a/lib/vivid/png/buffer.ex b/lib/vivid/png/buffer.ex index aabcef4..087e0cd 100644 --- a/lib/vivid/png/buffer.ex +++ b/lib/vivid/png/buffer.ex @@ -8,34 +8,35 @@ defimpl Vivid.PNG, for: Vivid.Buffer do @doc """ Convert a Vivid buffer into a PNG file. """ - @spec to_png(Buffer.t, Path.t) :: :ok | {:error, any} + @spec to_png(Buffer.t(), Path.t()) :: :ok | {:error, any} def to_png(buffer, path) do with {:ok, file} <- File.open(path, [:write]), - png <- create_png(buffer, file), - :ok <- :png.close(png), - :ok <- File.close(file), + png <- create_png(buffer, file), + :ok <- :png.close(png), + :ok <- File.close(file), do: :ok end defp config(%Buffer{rows: height, columns: width}, file) do %{ - size: {width, height}, - mode: {:rgba, 8}, - file: file, + size: {width, height}, + mode: {:rgba, 8}, + file: file } end - defp create_png(%Buffer{columns: width}=buffer, file) do - png = buffer + defp create_png(%Buffer{columns: width} = buffer, file) do + png = + buffer |> config(file) - |> :png.create + |> :png.create() buffer - |> Stream.map(&colour_to_binary(&1)) - |> Stream.chunk(width) - |> Stream.map(&Enum.join(&1)) - |> Enum.reverse - |> Enum.reduce(png, &(:png.append(&2, {:row, &1}))) + |> Stream.map(&colour_to_binary(&1)) + |> Stream.chunk(width) + |> Stream.map(&Enum.join(&1)) + |> Enum.reverse() + |> Enum.reduce(png, &:png.append(&2, {:row, &1})) end defp colour_to_binary(%RGBA{red: r, green: g, blue: b, alpha: a}) do @@ -43,6 +44,6 @@ defimpl Vivid.PNG, for: Vivid.Buffer do g = round(g * 255) b = round(b * 255) a = round(a * 255) - <> + <> end -end \ No newline at end of file +end diff --git a/lib/vivid/png/frame.ex b/lib/vivid/png/frame.ex index b1a00b4..69dfb39 100644 --- a/lib/vivid/png/frame.ex +++ b/lib/vivid/png/frame.ex @@ -8,10 +8,10 @@ defimpl Vivid.PNG, for: Vivid.Frame do @doc """ Convert a Vivid frame into a PNG file. """ - @spec to_png(Frame.t, Path.t) :: :ok | {:error, any} + @spec to_png(Frame.t(), Path.t()) :: :ok | {:error, any} def to_png(frame, file) do frame |> Frame.buffer(:horizontal) |> PNG.to_png(file) end -end \ No newline at end of file +end diff --git a/lib/vivid/png/shape.ex b/lib/vivid/png/shape.ex index 5be1eed..0fb4fed 100644 --- a/lib/vivid/png/shape.ex +++ b/lib/vivid/png/shape.ex @@ -1,30 +1,32 @@ defmodule Vivid.PNG.ShapeToPng do - alias Vivid.{PNG, Shape, Bounds, Transform, Frame} + alias Vivid.{Bounds, Frame, PNG, Shape, Transform} @moduledoc false @doc false - @spec to_png(Shape.t, Path.t) :: :ok | {:error, any} + @spec to_png(Shape.t(), Path.t()) :: :ok | {:error, any} def to_png(shape, file) do bounds = Bounds.bounds(shape) - width = bounds |> Bounds.width |> round |> Kernel.+(3) - height = bounds |> Bounds.height |> round |> Kernel.+(3) + width = bounds |> Bounds.width() |> round |> Kernel.+(3) + height = bounds |> Bounds.height() |> round |> Kernel.+(3) frame = Frame.init(width, height) - shape = shape + shape = + shape |> Transform.center(frame) - |> Transform.apply + |> Transform.apply() frame - |> Frame.push(shape, RGBA.black) + |> Frame.push(shape, RGBA.black()) |> PNG.to_png(file) end end Enum.each(~w(Arc Box Circle Group Line Path Polygon), fn mod -> mod = Module.concat(Vivid, mod) + defimpl Vivid.PNG, for: mod do def to_png(shape, file), do: Vivid.PNG.ShapeToPng.to_png(shape, file) end -end) \ No newline at end of file +end) diff --git a/lib/vivid_png.ex b/lib/vivid_png.ex index 45fde85..57abe5f 100644 --- a/lib/vivid_png.ex +++ b/lib/vivid_png.ex @@ -1,5 +1,6 @@ defprotocol Vivid.PNG do alias Vivid.{Shape, Frame, Buffer} + @moduledoc """ Turn a Vivid frame or shape into a PNG """ @@ -7,6 +8,6 @@ defprotocol Vivid.PNG do @doc """ Turn a frame or shape into a PNG file. """ - @spec to_png(Shape.t | Frame.t | Buffer.t, Path.t) :: :ok | {:error, any} + @spec to_png(Shape.t() | Frame.t() | Buffer.t(), Path.t()) :: :ok | {:error, any} def to_png(shape, file) end diff --git a/mix.exs b/mix.exs index 7605592..4751f59 100644 --- a/mix.exs +++ b/mix.exs @@ -2,14 +2,16 @@ defmodule Vivid.PNG.Mixfile do use Mix.Project def project do - [app: :vivid_png, - version: "0.1.0", - description: description, - elixir: "~> 1.3", - build_embedded: Mix.env == :prod, - start_permanent: Mix.env == :prod, - package: package, - deps: deps()] + [ + app: :vivid_png, + version: "0.2.0", + description: description(), + elixir: "~> 1.3", + build_embedded: Mix.env() == :prod, + start_permanent: Mix.env() == :prod, + package: package(), + deps: deps() + ] end def application do @@ -24,10 +26,10 @@ defmodule Vivid.PNG.Mixfile do def package do [ - maintainers: [ "James Harton " ], - licenses: [ "MIT" ], + maintainers: ["James Harton "], + licenses: ["MIT"], links: %{ - "Source" => "https://github.com/jamesotron/vivid_png.ex" + "Source" => "https://gitlab.com/jimsy/vivid_png.ex" } ] end @@ -35,8 +37,10 @@ defmodule Vivid.PNG.Mixfile do defp deps do [ {:ex_doc, ">= 0.0.0", only: :dev}, - {:png, "~> 0.1.1"}, - {:vivid, ">= 0.3.0"} + {:credo, "~> 0.10", only: ~w(dev test)a, runtime: false}, + {:inch_ex, "~> 1.0", only: ~w(dev test)a, runtime: false}, + {:png, "~> 0.1"}, + {:vivid, "~> 0.4"} ] end end diff --git a/mix.lock b/mix.lock index 1f54146..bf2454a 100644 --- a/mix.lock +++ b/mix.lock @@ -1,4 +1,14 @@ -%{"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []}, - "ex_doc": {:hex, :ex_doc, "0.14.5", "c0433c8117e948404d93ca69411dd575ec6be39b47802e81ca8d91017a0cf83c", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]}, - "png": {:hex, :png, "0.1.1", "57eeab907d4c2b78d4c3803bc355af025248db9ebd612e4275c3cab65b809171", [:rebar], []}, - "vivid": {:hex, :vivid, "0.3.0", "98c6203e6c4f2e791530645529d7a767387a7d949f05be282d06de85b5dec898", [:mix], []}} +%{ + "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"}, + "credo": {:hex, :credo, "0.10.0", "66234a95effaf9067edb19fc5d0cd5c6b461ad841baac42467afed96c78e5e9e", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"}, + "earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm"}, + "ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"}, + "inch_ex": {:hex, :inch_ex, "1.0.0", "18496a900ca4b7542a1ff1159e7f8be6c2012b74ca55ac70de5e805f14cdf939", [:mix], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"}, + "jason": {:hex, :jason, "1.1.1", "d3ccb840dfb06f2f90a6d335b536dd074db748b3e7f5b11ab61d239506585eb2", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"}, + "makeup": {:hex, :makeup, "0.5.1", "966c5c2296da272d42f1de178c1d135e432662eca795d6dc12e5e8787514edf7", [:mix], [{:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"}, + "makeup_elixir": {:hex, :makeup_elixir, "0.8.0", "1204a2f5b4f181775a0e456154830524cf2207cf4f9112215c05e0b76e4eca8b", [:mix], [{:makeup, "~> 0.5.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 0.2.2", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"}, + "nimble_parsec": {:hex, :nimble_parsec, "0.2.2", "d526b23bdceb04c7ad15b33c57c4526bf5f50aaa70c7c141b4b4624555c68259", [:mix], [], "hexpm"}, + "png": {:hex, :png, "0.1.1", "57eeab907d4c2b78d4c3803bc355af025248db9ebd612e4275c3cab65b809171", [:rebar], [], "hexpm"}, + "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, + "vivid": {:hex, :vivid, "0.4.3", "ea0abddb41a1ac778392c05f11c70118a5d36d71f0dee9e4b10bcb757a54f1c2", [:mix], [], "hexpm"}, +}