Small bug fixes and improments

This commit is contained in:
James Harton 2017-01-02 12:46:51 +13:00
parent c2c35774fe
commit 7e113327fb
3 changed files with 23 additions and 2 deletions

View file

@ -97,6 +97,13 @@ defmodule Vivid.Frame do
%{frame | shapes: [{shape, colour} | shapes]}
end
@doc """
Clear the frame of any shapes.
"""
def clear(%Frame{}=frame) do
%{frame | shapes: []}
end
@doc """
Return the width of the frame.
@ -127,10 +134,15 @@ defmodule Vivid.Frame do
"""
def background_colour(%Frame{background_colour: c}), do: c
def buffer(%Frame{shapes: shapes, width: w, height: h, background_colour: bg}) do
def buffer(%Frame{shapes: shapes, width: w, height: h, background_colour: bg}, direction \\ :horizontal) do
{w,h} = case direction do
:horizontal -> {w, h}
:vertical -> {h, w}
end
Enum.reduce(shapes, allocate_buffer(w * h, bg), fn({shape, colour}, buffer)->
points = Vivid.Rasterize.rasterize(shape, {0, 0, h-1, w-1})
points = Vivid.Rasterize.rasterize(shape, {0, 0, w-1, h-1})
Enum.reduce(points, buffer, fn(point, buffer) ->
point = translate_point(point, direction)
x = point |> Point.x
y = point |> Point.y
pos = (x * w) + y
@ -177,6 +189,8 @@ defmodule Vivid.Frame do
end
defp translate_point(point, :horizontal), do: point
defp translate_point(point, :vertical), do: Point.swap_xy(point)
defp allocate_buffer(size, colour) do
Enum.map((1..size), fn(_) -> colour end)

View file

@ -37,4 +37,10 @@ defmodule Vivid.Point do
27
"""
def y(%Point{y: y}), do: y
@doc """
Simple helper to swap X and Y coordinates - used
when translating the frame buffer to vertical.
"""
def swap_xy(%Point{x: x, y: y}), do: Point.init(y, x)
end

View file

@ -96,6 +96,7 @@ defmodule Vivid.RGBA do
#Vivid.RGBA<{0.5, 0.5, 0.5, 1.0}>
"""
def over(nil, %RGBA{}=colour), do: colour
def over(%RGBA{}, %RGBA{alpha: 1}=visible), do: visible
def over(%RGBA{}=visible, %RGBA{alpha: 0}), do: visible
def over(%RGBA{a_red: r0, a_green: g0, a_blue: b0, alpha: a0}, %RGBA{a_red: r1, a_green: g1, a_blue: b1, alpha: a1}) do