-
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.
renaming conversion functions removing commented out code better type checking adding kino renderers for Leds and for colors fixing bug in name loader adding type to Names module adding guard for color names adding a color type adding more Leds tests updating TODO list creating a livebook to give an introduction to colors and leds
- Loading branch information
Showing
16 changed files
with
487 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule Fledex.Color.KinoRender do | ||
import Fledex.Color.Names, only: [is_color_name: 1] | ||
alias Fledex.Color.Types | ||
alias Fledex.Color.Utils | ||
alias Fledex.Leds | ||
|
||
defguard is_byte(sub_pixel) when is_integer(sub_pixel) and sub_pixel >= 0 and sub_pixel <= 255 | ||
|
||
defstruct [:colors] | ||
@type t :: %__MODULE__{ | ||
colors: [Types.color] | ||
} | ||
|
||
@spec new(Types.color | [Types.color]) :: t | ||
def new(colors) when is_list(colors) do | ||
%__MODULE__{colors: colors} | ||
end | ||
def new(color) when is_integer(color) do | ||
new([color]) | ||
end | ||
def new({r, g, b} = color) when is_byte(r) and is_byte(g) and is_byte(b) do | ||
new([color]) | ||
end | ||
def new(color) when is_atom(color) and is_color_name(color) do | ||
new([color]) | ||
end | ||
|
||
@spec to_leds(t) :: Leds.t | ||
def to_leds(%__MODULE__{colors: colors}) do | ||
colors = Enum.map(colors, fn color -> | ||
Utils.to_colorint(Utils.to_rgb(color)) | ||
end) | ||
Leds.leds( | ||
length(colors), | ||
Map.new( | ||
Enum.reduce(colors, [], fn value, acc -> | ||
index = length(acc) + 1 | ||
[{index, value} | acc] | ||
end) | ||
), | ||
%{} | ||
) | ||
end | ||
|
||
@spec to_markdown(KinoRender.t) :: binary | ||
def to_markdown(%__MODULE__{} = colors) do | ||
Leds.to_markdown(to_leds(colors)) | ||
end | ||
|
||
defimpl Kino.Render do | ||
alias Fledex.Color.KinoRender | ||
|
||
@impl true | ||
def to_livebook(%KinoRender{} = colors) do | ||
md_kino = Kino.Markdown.new(KinoRender.to_markdown(colors)) | ||
i_kino = Kino.Inspect.new(colors) | ||
kino = Kino.Layout.tabs( | ||
Leds: md_kino, | ||
Raw: i_kino | ||
) | ||
Kino.Render.to_livebook(kino) | ||
end | ||
end | ||
end |
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,6 +1,7 @@ | ||
defmodule Fledex.Color.Types do | ||
@type rgb :: {red :: 0..255, green :: 0..255, blue :: 0..255} | ||
@type hsv :: {hue :: 0..255, saturation :: 0..255, value :: 0..255} | ||
@type hsl :: {hue :: 0..255, saturation :: 0..255, light :: 0..255} | ||
@type colorint :: 0..0xFFFFFF | ||
@type rgb :: {red :: 0..255, green :: 0..255, blue :: 0..255} | ||
@type hsv :: {hue :: 0..255, saturation :: 0..255, value :: 0..255} | ||
@type hsl :: {hue :: 0..255, saturation :: 0..255, light :: 0..255} | ||
@type colorint :: 0..0xFFFFFF | ||
@type color :: rgb | colorint | atom | Fledex.Color.Names.t | ||
end |
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
Oops, something went wrong.