Current section
Files
Jump to
Current section
Files
lib/utils.ex
defmodule Isotope.Utils do
@moduledoc """
Utility functions for working with noise maps.
## Visualization
{:ok, noise} = Isotope.Noise.new(%Isotope.Options{seed: 42})
nm = Isotope.Noise.noise_map(noise, {80, 40})
Isotope.Utils.show_noisemap(nm)
This renders the noise map to the terminal using ANSI 256-color codes.
Each value is mapped to a color index and displayed as a `▒` character.
Requires a terminal with ANSI color support.
"""
alias Isotope.NoiseMap
@doc """
Shows the given noise map on the stdout using ANSI color codes.
> This won't work if your terminal doesn't support ANSI color codes.
```elixir
{:ok, noise} = Isotope.Noise.new()
noise |> Isotope.Noise.noise_map({50, 50})
|> Isotope.Utils.show_noisemap()
# Outputs noise visualization
:ok
```
"""
@spec show_noisemap(NoiseMap.t()) :: :ok
def show_noisemap(%NoiseMap{} = noisemap) do
Enum.each(noisemap, fn row ->
Enum.each(row, fn x ->
color = (x * 255) |> floor() |> abs() |> rem(255)
ansi_char = IO.ANSI.color(color) <> "▒"
IO.write(ansi_char)
end)
IO.puts("")
end)
end
end