exercism/elixir/paint-by-number/lib/paint_by_number.ex

42 lines
1021 B
Elixir

defmodule PaintByNumber do
def palette_bit_size(color_count, pow \\ 1) do
if color_count <= 2 ** pow do
pow
else
palette_bit_size(color_count, pow + 1)
end
end
def empty_picture() do
<<>>
end
def test_picture() do
<<0::2, 1::2, 2::2, 3::2>>
end
def prepend_pixel(picture, color_count, pixel_color_index) do
size = palette_bit_size(color_count)
pixel = <<pixel_color_index::size(size)>>
<<pixel::bitstring, picture::bitstring>>
end
def get_first_pixel(<<>>, _color_count), do: nil
def get_first_pixel(picture, color_count) do
size = palette_bit_size(color_count)
<<value::size(size), _rest::bitstring>> = picture
value
end
def drop_first_pixel(<<>>, _color_count), do: <<>>
def drop_first_pixel(picture, color_count) do
size = palette_bit_size(color_count)
<<_value::size(size), rest::bitstring>> = picture
rest
end
def concat_pictures(picture1, picture2) do
<<picture1::bitstring, picture2::bitstring>>
end
end