Vivid.Buffer (vivid v1.0.0)

Copy Markdown View Source

Used to convert a Frame into a buffer for display.

You're unlikely to need to use this module directly, instead you will likely want to use Frame.buffer/2 instead.

Buffer implements the Enumerable protocol.

Example

iex> use Vivid
...> box = Box.init(Point.init(1,1), Point.init(18,8))
...> Frame.init(20, 10, RGBA.white())
...> |> Frame.push(box, RGBA.black())
...> |> Buffer.horizontal()
...> |> Stream.chunk_every(20)
...> |> Stream.map(fn line ->
...>   Stream.map(line, fn colour -> RGBA.to_ascii(colour) end)
...>   |> Enum.join()
...> end)
...> |> Enum.join("\n")
"@@@@@@@@@@@@@@@@@@@@\n" <>
"@                  @\n" <>
"@ @@@@@@@@@@@@@@@@ @\n" <>
"@ @@@@@@@@@@@@@@@@ @\n" <>
"@ @@@@@@@@@@@@@@@@ @\n" <>
"@ @@@@@@@@@@@@@@@@ @\n" <>
"@ @@@@@@@@@@@@@@@@ @\n" <>
"@ @@@@@@@@@@@@@@@@ @\n" <>
"@                  @\n" <>
"@@@@@@@@@@@@@@@@@@@@"

Summary

Functions

Returns the number of columns in the buffer.

Render the buffer horizontally, ie across rows then up columns.

Returns the number of rows in the buffer.

Convert the buffer into a binary of four byte RGBA pixels.

Render the buffer vertically, ie up columns then across rows.

Types

t()

@type t() :: %Vivid.Buffer{
  buffer: [Vivid.RGBA.t()],
  columns: integer(),
  rows: integer()
}

Functions

columns(buffer)

@spec columns(t()) :: pos_integer()

Returns the number of columns in the buffer.

horizontal(frame)

@spec horizontal(Vivid.Frame.t()) :: t()

Render the buffer horizontally, ie across rows then up columns.

Example

iex> use Vivid
...> Frame.init(5, 5, RGBA.white)
...> |> Frame.push(Line.init(Point.init(0, 2), Point.init(5, 2)), RGBA.black)
...> |> Buffer.horizontal
...> |> to_string
"@@@@@\n" <>
"@@@@@\n" <>
"     \n" <>
"@@@@@\n" <>
"@@@@@\n"

rows(buffer)

@spec rows(t()) :: pos_integer()

Returns the number of rows in the buffer.

to_binary(buffer)

@spec to_binary(t()) :: binary()

Convert the buffer into a binary of four byte RGBA pixels.

Rows are emitted from the top of the buffer downwards, and each row from left to right, which is the order image formats expect rather than the bottom-up order the buffer is indexed in.

Example

iex> use Vivid
...> Frame.init(2, 2, RGBA.black())
...> |> Frame.push(Point.init(0, 0), RGBA.white())
...> |> Frame.buffer()
...> |> Buffer.to_binary()
<<0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255, 255, 0, 0, 0, 255>>

vertical(frame)

@spec vertical(Vivid.Frame.t()) :: t()

Render the buffer vertically, ie up columns then across rows.

Example

iex> use Vivid
...> Frame.init(5, 5, RGBA.white)
...> |> Frame.push(Line.init(Point.init(0, 2), Point.init(5, 2)), RGBA.black)
...> |> Buffer.vertical
...> |> to_string
"@@ @@\n" <>
"@@ @@\n" <>
"@@ @@\n" <>
"@@ @@\n" <>
"@@ @@\n"