Packages

Chameleon is a utility for converting colors from one color model to another. Conversions can be made to/from RGB, CMYK, Hex, HSL, Pantone, and even Keywords. For example: "FFFFFF" -> %{c: 0, m: 0, y: 0, k: 0}

Current section

Files

Jump to
chameleon lib chameleon cmyk.ex
Raw

lib/chameleon/cmyk.ex

defmodule Chameleon.CMYK do
alias Chameleon.CMYK
@enforce_keys [:c, :m, :y, :k]
defstruct @enforce_keys
@type t() :: %__MODULE__{c: integer(), m: integer(), y: integer(), k: integer()}
@doc """
Creates a new color struct.
## Examples
iex> _cmyk = Chameleon.CMYK.new(25, 30, 80, 0)
%Chameleon.CMYK{c: 25, m: 30, y: 80, k: 0}
"""
@spec new(non_neg_integer(), non_neg_integer(), non_neg_integer(), non_neg_integer()) ::
Chameleon.CMYK.t()
def new(c, m, y, k), do: %__MODULE__{c: c, m: m, y: y, k: k}
defimpl Chameleon.Color.RGB do
def from(cmyk), do: CMYK.to_rgb(cmyk)
end
#### / Conversion Functions / ########################################
@doc false
@spec to_rgb(Chameleon.CMYK.t()) :: Chameleon.RGB.t() | {:error, String.t()}
def to_rgb(cmyk) do
[c, m, y, k] = Enum.map([cmyk.c, cmyk.m, cmyk.y, cmyk.k], fn v -> v / 100.0 end)
r = round(255.0 * (1.0 - c) * (1.0 - k))
g = round(255.0 * (1.0 - m) * (1.0 - k))
b = round(255.0 * (1.0 - y) * (1.0 - k))
Chameleon.RGB.new(r, g, b)
end
end