-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5e3214
commit 4e502e5
Showing
11 changed files
with
339 additions
and
189 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 |
---|---|---|
@@ -1,85 +1,87 @@ | ||
defmodule Chameleon.Cmyk do | ||
alias Chameleon.{Rgb, Hex, Util} | ||
@enforce_keys [:c, :m, :y, :k] | ||
defstruct @enforce_keys | ||
|
||
@doc """ | ||
Converts a cmyk color to its rgb value. | ||
## Examples | ||
iex> Chameleon.Cmyk.to_rgb([100, 0, 100, 0]) | ||
%{r: 0, g: 255, b: 0} | ||
iex> Chameleon.Cmyk.to_rgb(%Chameleon.Cmyk{c: 100, m: 0, y: 100, k: 0}) | ||
%Chameleon.Rgb{r: 0, g: 255, b: 0} | ||
""" | ||
@spec to_rgb(list(integer)) :: list(integer) | ||
@spec to_rgb(struct()) :: struct() | ||
def to_rgb(cmyk) do | ||
adjusted_cmyk = Enum.map(cmyk, fn v -> v / 100.0 end) | ||
[c, m, y, k] = adjusted_cmyk | ||
[c, m, y, k] = Enum.map([cmyk.c, cmyk.m, cmyk.y, cmyk.k], fn v -> v / 100.0 end) | ||
|
||
r = round(Float.round(255.0 * (1.0 - c) * (1.0 - k))) | ||
g = round(Float.round(255.0 * (1.0 - m) * (1.0 - k))) | ||
b = round(Float.round(255.0 * (1.0 - y) * (1.0 - k))) | ||
|
||
%{r: r, g: g, b: b} | ||
Chameleon.Color.new(%{r: r, g: g, b: b}) | ||
end | ||
|
||
@doc """ | ||
Converts a cmyk color to its hsl value. | ||
## Examples | ||
iex> Chameleon.Cmyk.to_hsl([100, 0, 100, 0]) | ||
%{h: 120, s: 100, l: 50} | ||
iex> Chameleon.Cmyk.to_hsl(%Chameleon.Cmyk{c: 100, m: 0, y: 100, k: 0}) | ||
%Chameleon.Hsl{h: 120, s: 100, l: 50} | ||
""" | ||
@spec to_hsl(list(integer)) :: list(integer) | ||
@spec to_hsl(struct()) :: struct() | ||
def to_hsl(cmyk) do | ||
cmyk | ||
|> to_rgb() | ||
|> rgb_values() | ||
|> Rgb.to_hsl() | ||
|> Chameleon.Converter.convert(:hsl) | ||
end | ||
|
||
@doc """ | ||
Converts a cmyk color to its hex value. | ||
## Examples | ||
iex> Chameleon.Cmyk.to_hex([100, 0, 100, 0]) | ||
"00FF00" | ||
iex> Chameleon.Cmyk.to_hex(%Chameleon.Cmyk{c: 100, m: 0, y: 100, k: 0}) | ||
%Chameleon.Hex{hex: "00FF00"} | ||
""" | ||
@spec to_hex(list(integer)) :: charlist | ||
@spec to_hex(struct()) :: struct() | ||
def to_hex(cmyk) do | ||
cmyk | ||
|> to_rgb() | ||
|> rgb_values() | ||
|> Rgb.to_hex() | ||
|> Chameleon.Converter.convert(:hex) | ||
end | ||
|
||
@doc """ | ||
Converts a cmyk color to its pantone value. | ||
## Examples | ||
iex> Chameleon.Cmyk.to_pantone([0, 0, 0, 100]) | ||
"30" | ||
iex> Chameleon.Cmyk.to_pantone(%Chameleon.Cmyk{c: 0, m: 0, y: 0, k: 100}) | ||
%Chameleon.Pantone{pantone: "30"} | ||
""" | ||
@spec to_pantone(list(integer)) :: charlist | ||
@spec to_pantone(struct()) :: struct() | ||
def to_pantone(cmyk) do | ||
cmyk | ||
|> to_hex() | ||
|> Hex.to_pantone() | ||
|> Chameleon.Converter.convert(:pantone) | ||
end | ||
|
||
@doc """ | ||
Converts a cmyk color to its rgb value. | ||
## Examples | ||
iex> Chameleon.Cmyk.to_keyword([100, 0, 100, 0]) | ||
"lime" | ||
iex> Chameleon.Cmyk.to_keyword(%Chameleon.Cmyk{c: 100, m: 0, y: 100, k: 0}) | ||
%Chameleon.Keyword{keyword: "lime"} | ||
""" | ||
@spec to_keyword(list(integer)) :: charlist | ||
@spec to_keyword(struct()) :: struct() | ||
def to_keyword(cmyk) do | ||
cmyk | ||
|> to_rgb() | ||
|> rgb_values() | ||
|> Rgb.to_keyword() | ||
|> Chameleon.Converter.convert(:keyword) | ||
end | ||
end | ||
|
||
#### Helper Functions ####################################################################### | ||
|
||
defdelegate rgb_values(rgb_map), to: Util | ||
defimpl Chameleon.Converter, for: Chameleon.Cmyk do | ||
def convert(cmyk, :rgb), do: Chameleon.Cmyk.to_rgb(cmyk) | ||
def convert(cmyk, :hsl), do: Chameleon.Cmyk.to_hsl(cmyk) | ||
def convert(cmyk, :hex), do: Chameleon.Cmyk.to_hex(cmyk) | ||
def convert(cmyk, :pantone), do: Chameleon.Cmyk.to_pantone(cmyk) | ||
def convert(cmyk, :keyword), do: Chameleon.Cmyk.to_keyword(cmyk) | ||
def convert(cmyk, :cmyk), do: cmyk | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule Chameleon.Color do | ||
@moduledoc """ | ||
Chameleon.Color | ||
Converts color inputs into supported structs. | ||
""" | ||
|
||
@doc """ | ||
Converts input into valid struct. | ||
## Examples | ||
iex> Chameleon.Color.new(%{hex: "000000"}) | ||
%Chameleon.Hex{hex: "000000"} | ||
iex> Chameleon.Color.new(%{c: 0, m: 0, y: 0, k: 100}) | ||
%Chameleon.Cmyk{c: 0, m: 0, y: 0, k: 100} | ||
""" | ||
@spec new(map()) :: struct() | ||
def new(%{c: c, m: m, y: y, k: k}), do: %Chameleon.Cmyk{c: c, m: m, y: y, k: k} | ||
def new(%{r: r, g: g, b: b}), do: %Chameleon.Rgb{r: r, g: g, b: b} | ||
def new(%{h: h, s: s, l: l}), do: %Chameleon.Hsl{h: h, s: s, l: l} | ||
def new(%{hex: hex}), do: %Chameleon.Hex{hex: hex} | ||
def new(%{pantone: pantone}), do: %Chameleon.Pantone{pantone: pantone} | ||
def new(%{keyword: keyword}), do: %Chameleon.Keyword{keyword: keyword} | ||
def new(_other), do: argument_error() | ||
def new(), do: argument_error() | ||
|
||
defp argument_error do | ||
message = """ | ||
A color argument must be included in one of the following formats: | ||
%{c: 0, m: 0, y: 0, k: 0} | ||
%{r: 0, g: 0, b: 0} | ||
%{h: 0, s: 0, l: 0} | ||
%{hex: "000000"} | ||
%{pantone: "30"} | ||
%{keyword: "black"} | ||
""" | ||
|
||
Mix.raise(message) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
defprotocol Chameleon.Converter do | ||
@moduledoc """ | ||
Performs the conversions from one color model to another. | ||
""" | ||
@spec convert(struct(), atom()) :: struct() | ||
def convert(base, to) | ||
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
Oops, something went wrong.