Tint

Build Status Coverage Status Hex.pm

An Elixir library allowing calculations with colors and conversions between different colorspaces.

Currently supports the following color models:

Prerequisites

  • Elixir 1.8 or greater

Installation

The package can be installed by adding tint to your list of dependencies in mix.exs:

def deps do
  [
    {:tint, "1.0.0-rc.0"}
  ]
end

Usage

Colorspaces

RGB

iex> Tint.RGB.new(255, 0, 0)
#Tint.RGB<255,0,0 (#FF0000)>

or

iex> import Tint.Sigil
...> ~K[255, 0, 0]r
#Tint.RGB<255,0,0 (#FF0000)>

CMYK

iex> Tint.CMYK.new(0.55, 0.26, 0.24, 0.65)
#Tint.CMYK<55%,26%,24%,65%>

or

iex> import Tint.Sigil
...> ~K[0.55, 0.26, 0.24, 0.65]c
#Tint.CMYK<55%,26%,24%,65%>

HSV

iex> Tint.HSV.new(25.8, 0.882, 1)
#Tint.HSV<25.8°,88.2%,100%>

or

iex> import Tint.Sigil
...> ~K[25.8, 0.882, 1]h
#Tint.HSV<25.8°,88.2%,100%>

CIELAB

iex> Tint.Lab.new(53.2329, 80.1068, 67.2202)
#Tint.Lab<53.2329,80.1068,67.2202>

or

iex> import Tint.Sigil
...> ~K[53.2329, 80.1068, 67.2202]c
#Tint.Lab<53.2329,80.1068,67.2202>

DIN99

iex> Tint.DIN99.new(53.2329, 80.1068, 67.2202)
#Tint.DIN99<53.2329,80.1068,67.2202>

or

iex> import Tint.Sigil
...> ~K[53.2329, 80.1068, 67.2202]d
#Tint.DIN99<53.2329,80.1068,67.2202>

Conversion

Between Colorspaces

iex> rgb = Tint.RGB.new(255, 0, 0)
...> Tint.to_hsv(rgb)
#Tint.HSV<0°,100%,100%>

The complete list:

Tint.to_cmyk(color)
Tint.to_din99(color)
Tint.to_hsv(color)
Tint.to_lab(color)
Tint.to_rgb(color)
Tint.to_xyz(color)

Currently, only RGB can be converted to any other colorspace.

Hex Code

iex> Tint.RGB.from_hex("#FF0000")
{:ok, #Tint.RGB<255,0,0 (#FF0000)>}
iex> Tint.RGB.from_hex("invalid hex code")
:error
iex> Tint.RGB.to_hex(Tint.RGB.new(255, 0, 0))
"#FF0000"

Alternatively, you can use the sigil as a shortcut:

iex> import Tint.Sigil
...> ~K[#FF0000]
#Tint.RGB<255,0,0 (#FF0000)>

Color Distance

There are a couple of functions to calculate the distance between two colors.

Euclidean Distance

The Euclidean distance can be calculated on RGB colors. Calculating the Euclidean distance is very fast but may not be very precise. If you want maximum precision use the CIEDE2000 algorithm.

iex> Tint.RGB.euclidean_distance(~K[#FFFFFF], ~K[#000000])
441.6729559300637

You can also define weights for the individual red, green and blue color channels:

iex> Tint.RGB.euclidean_distance(~K[#FFFFFF], ~K[#000000], weights: {2, 4, 3})
765.0

To find the nearest color from a given palette:

iex> Tint.RGB.nearest_color(~K[#CC0000], [~K[#009900], ~K[#FF0000]])
#Tint.RGB<255,0,0 (#FF0000)>

CIEDE2000

CIEDE2000 is an algorithm that operates on the CIELAB colorspace. It is very slow compared to the Euclidean distance algorithm but it is optimized to human color perception.

iex> Tint.Lab.ciede2000_distance(~K[#FF0000], ~K[#000000])
50.3905024704449

To find the nearest color from a given palette:

iex> Tint.Lab.nearest_color(~K[#FF0000], [~K[#009900], ~K[#CC0000]])
#Tint.RGB<204,0,0 (#CC0000)>

Docs

The API docs can be found at HexDocs.