Color.Mix
(Color v0.4.0)
Copy Markdown
Color interpolation and gradient generation.
mix/3,4— linearly interpolate between two colors in a named working space. The space matters a lot: mixing red and green in sRGB gives muddy brown att = 0.5, while mixing in Oklab gives a clean olive. Default isColor.Oklab, matching CSS Color 4'scolor-mix()recommendation.gradient/4— return a list ofnevenly spaced colors fromstarttostop, inclusive of both endpoints.
Both functions accept anything Color.new/1 accepts and always
return the mixed color as a Color.SRGB struct (so it's ready to
display). If you need the result in a different space, pipe it
through Color.convert/2 afterwards.
Hue interpolation is handled specially for cylindrical spaces
(Color.LCHab, Color.LCHuv, Color.Oklch, Color.HSLuv,
Color.HPLuv, Color.HSL, Color.HSV): by default we take the
shorter arc around the hue circle. Pass hue: :longer,
hue: :increasing or hue: :decreasing to force a different path,
matching the CSS Color 4 hue-interpolation modes.
Summary
Functions
Generates an evenly-spaced gradient between start and stop.
Mixes two colors in the given working space.
Functions
@spec gradient(Color.input(), Color.input(), pos_integer(), keyword()) :: {:ok, [Color.SRGB.t()]} | {:error, Exception.t()}
Generates an evenly-spaced gradient between start and stop.
Arguments
startis any color accepted byColor.new/1.stopis any color accepted byColor.new/1.stepsis the number of colors to return,≥ 2. The first result isstartand the last result isstop.optionsis the same as formix/4.
Returns
{:ok, [%Color.SRGB{}, ...]}withstepselements.
Examples
iex> {:ok, colors} = Color.Mix.gradient("black", "white", 3)
iex> Enum.map(colors, &Color.SRGB.to_hex/1)
["#000000", "#636363", "#ffffff"]
iex> {:ok, colors} = Color.Mix.gradient("red", "blue", 5)
iex> length(colors)
5
@spec mix(Color.input(), Color.input(), number(), keyword()) :: {:ok, Color.SRGB.t()} | {:error, Exception.t()}
Mixes two colors in the given working space.
Arguments
ais any color accepted byColor.new/1.bis any color accepted byColor.new/1.tis the mixing parameter in[0, 1].0.0returnsa,1.0returnsb,0.5returns the midpoint.optionsis a keyword list.
Options
:inis the color space module to interpolate in. Defaults toColor.Oklab.:hueis the hue-interpolation mode for cylindrical spaces::shorter(default),:longer,:increasing,:decreasing.
Returns
- A
Color.SRGBstruct.
Examples
iex> {:ok, mid} = Color.Mix.mix("red", "lime", 0.5)
iex> hex = Color.SRGB.to_hex(mid) |> String.upcase()
iex> String.starts_with?(hex, "#")
true
iex> {:ok, mid} = Color.Mix.mix("red", "lime", 0.5, in: Color.SRGB)
iex> Color.SRGB.to_hex(mid) |> String.upcase()
"#7F8000"
iex> {:ok, a} = Color.Mix.mix("red", "blue", 0.0)
iex> {:ok, b} = Color.Mix.mix("red", "blue", 1.0)
iex> {Color.SRGB.to_hex(a), Color.SRGB.to_hex(b)}
{"#ff0000", "#0000ff"}