-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change order of behaviour arguments
- Loading branch information
Showing
4 changed files
with
47 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,49 @@ | ||
defmodule Exa.Color.Colorb do | ||
@moduledoc "A behaviour for byte colors." | ||
@moduledoc """ | ||
A behaviour for byte colors, | ||
with a generic interface that dispatches | ||
to concrete implementations. | ||
""" | ||
|
||
import Exa.Color.Types | ||
alias Exa.Color.Types, as: C | ||
|
||
alias Exa.Color.Col1b | ||
alias Exa.Color.Col3b | ||
alias Exa.Color.Col4b | ||
|
||
# dispatch map from tags to implementation module | ||
@disp %{ | ||
:gray => Col1b, | ||
:index => Col1b, | ||
:rgb => Col3b, | ||
:bgr => Col3b, | ||
:rgba => Col4b, | ||
:bgra => Col4b, | ||
:argb => Col4b, | ||
:abgr => Col4b, | ||
} | ||
|
||
# it's more natural to have the buffer as first arg for piping | ||
# but dispatching implementations must have the tag as 1st arg | ||
|
||
@doc "Convert a color to a binary." | ||
@callback to_bin(C.colorb(), C.pixel()) :: binary() | ||
@callback to_bin(C.pixel(), C.colorb()) :: binary() | ||
|
||
@doc "Append a color to a binary." | ||
@callback append_bin(binary(), C.pixel(), C.colorb()) :: binary() | ||
@callback append_bin(C.pixel(), binary(), C.colorb()) :: binary() | ||
|
||
@doc "Read a color from a binary." | ||
@callback from_bin(binary(), C.pixel()) :: {C.colorb(), binary()} | ||
|
||
# kinda like a protocol, but with tagged pixels instead of a struct... | ||
|
||
def from_bin(buf, :gray), do: Col1b.from_bin(buf, :gray) | ||
def from_bin(buf, :index), do: Col1b.from_bin(buf, :index) | ||
def from_bin(buf, :rgb), do: Col3b.from_bin(buf, :rgb) | ||
def from_bin(buf, :bgr), do: Col3b.from_bin(buf, :bgr) | ||
def from_bin(buf, :rgba), do: Col4b.from_bin(buf, :rgba) | ||
def from_bin(buf, :bgra), do: Col4b.from_bin(buf, :bgra) | ||
def from_bin(buf, :argb), do: Col4b.from_bin(buf, :argb) | ||
def from_bin(buf, :abgr), do: Col4b.from_bin(buf, :abgr) | ||
|
||
def append_bin(buf, :gray, col), do: Col1b.append_bin(buf, :gray, col) | ||
def append_bin(buf, :index, col), do: Col1b.append_bin(buf, :index, col) | ||
def append_bin(buf, :rgb, col), do: Col3b.append_bin(buf, :rgb, col) | ||
def append_bin(buf, :bgr, col), do: Col3b.append_bin(buf, :bgr, col) | ||
def append_bin(buf, :rgba, col), do: Col4b.append_bin(buf, :rgba, col) | ||
def append_bin(buf, :bgra, col), do: Col4b.append_bin(buf, :bgra, col) | ||
def append_bin(buf, :argb, col), do: Col4b.append_bin(buf, :argb, col) | ||
def append_bin(buf, :abgr, col), do: Col4b.append_bin(buf, :abgr, col) | ||
|
||
def to_bin(col, :gray) when is_col1b(col), do: Col1b.to_bin(col, :gray) | ||
def to_bin(col, :index) when is_col1b(col), do: Col1b.to_bin(col, :index) | ||
def to_bin(col, :rgb) when is_col3b(col), do: Col3b.to_bin(col, :rgb) | ||
def to_bin(col, :bgr) when is_col3b(col), do: Col3b.to_bin(col, :bgr) | ||
def to_bin(col, :argb) when is_col4b(col), do: Col4b.to_bin(col, :argb) | ||
def to_bin(col, :abgr) when is_col4b(col), do: Col4b.to_bin(col, :abgr) | ||
def to_bin(col, :rgba) when is_col4b(col), do: Col4b.to_bin(col, :rgba) | ||
def to_bin(col, :bgra) when is_col4b(col), do: Col4b.to_bin(col, :bgra) | ||
@callback from_bin(C.pixel(), binary()) :: {C.colorb(), binary()} | ||
|
||
def from_bin(pix, buf) do | ||
Exa.Dispatch.dispatch(@disp, pix, :from_bin, [buf]) | ||
end | ||
|
||
def append_bin(pix, buf, col) do | ||
Exa.Dispatch.dispatch(@disp, pix, :append_bin, [buf, col]) | ||
end | ||
|
||
def to_bin(pix, col) do | ||
Exa.Dispatch.dispatch(@disp, pix, :to_bin, [col]) | ||
end | ||
end |