Giter Site home page Giter Site logo

tlux / tint Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 4.0 175 KB

An Elixir library to work with colors, convert between different colorspaces and calculate color distances.

License: MIT License

Elixir 100.00%
convert-colors elixir-library hex colorspaces color cmyk rgb cielab hsv hex-codes

tint's Introduction

Tint

Build Coverage Status Module Version Hex Docs License Last Updated

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

Currently supports the following color models:

Prerequisites

  • Elixir 1.14 or greater

Installation

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

def deps do
  [
    {:tint, "~> 1.2"}
  ]
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)>

Using hex codes:

iex> Tint.RGB.from_hex("#FF0000")
{:ok, #Tint.RGB<255,0,0 (#FF0000)>}

iex> Tint.RGB.from_hex("#F00")
{:ok, #Tint.RGB<255,0,0 (#FF0000)>}

iex> Tint.RGB.from_hex("FF0000")
{:ok, #Tint.RGB<255,0,0 (#FF0000)>}

iex> Tint.RGB.from_hex!("F00")
#Tint.RGB<255,0,0 (#FF0000)>

iex> Tint.RGB.from_hex("invalid")
:error

iex> Tint.RGB.from_hex!("invalid")
** (ArgumentError) Invalid hex code: invalid

or

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

To convert RGB colors back to hex codes:

iex> color = Tint.RGB.new(255, 0, 0)
...> Tint.RGB.to_hex(color)
"#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

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)

Alternatively you can use convert/2 and convert!/2:

Tint.convert(color, :rgb)
Tint.convert(color, Tint.RGB)
Tint.convert!(color, :hsv)

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

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)>

Complementary Color

iex> Tint.RGB.complementary_color(~K[#FF0000])
#Tint.RGB<0,255,255 (#00FFFF)>

Docs

The API docs can be found at HexDocs.

tint's People

Contributors

tlux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

tint's Issues

Holy sh**

I spent my last 12 years working on a creative algorithm research project. I started with Ruby back then and used bare bones code from scratch to make my project work. Although, the outcome was a success, it was a commercial failure. 12 years later, here I am, trying to restart the project in Elixir but dealing with all the decimal conversion BS. And then I found your library after literally spending half my vacation time getting the old code to work. I found your work and probably saved the rest of my vacation now. Everything is just a simple function call. Your project helped me a LOT. It is super useful!
Thank you SO much!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.