Color
Extensive collection of color spaces and color models
A library for dealing with Colors and pixels. It implements arbitrary color space conversion, chromatic adaptation and other color manipulations. The project is written primarily in Haskell, distributed under the Other license, first published in 2019. Key topics include: cielab, ciexyz, color, color-spaces, colour.
Color
A library for dealing with Colors and pixels. It implements arbitrary color space
conversion, chromatic adaptation and other color manipulations.
Status
| Language | Github Actions | Coveralls | Gitter.im |
|---|---|---|---|
| Package | Hackage | Nightly | LTS |
|---|---|---|---|
Color |
Description
There is a clear separation between color models, color spaces and alternative
representations of color spaces. All are distinct at the type level. The goal is to
prevent mixups of incompatible color types as well as utilize type information for
conversion between them.
Currently supported:
-
Color models:
YRGBHSIHSLHSVYCbCrCMYK
-
Color spaces and arbitrary conversions between them:
-
Y- luminance -
Y'- luma -
CIE XYZ -
CIE L*a*b* -
DIN99 -
RGB:-
sRGB- both standardized and derived -
AdobeRGB- both standardized and derived -
ITU:
Rec470,Rec601andRec709 -
Alternative representations:
HSIHSLHSVYCbCrCMYK
-
-
-
Illuminants:
- CIE1931 - 2 degree observer
- CIE1964 - 10 degree observer
- Some common alternatives
-
Chromatic adaptation:
-
VonKries adaptation with transformations:
VonKriesBradford(default)FairchildCIECAM02CMCCAT2000
-
-
Color Standards:
- RAL
- SVG
Example
Here is a short example how this library can be used. Here we assume a GHCi session that
can be started like so:
shell$ stack ghci --package Color
Perceived lightness
Let's say we need find the perceived lightness as described in this StackOverflow
answer
for an RGB triple (128, 255, 65) :: (Word8, Word8, Word8).
Before we can attempt getting the lightness we need to do these two things:
- Figure out what is the color space of the
RGBtriplet? In particular theIlluminant
and theLinearityof theRGBcolor space. - Convert your
RGBcolor toCIE L*a*b*and then we can get theL*
out, which is the perceived lightness.
More often than not an RGB image will be encoded in non-linear sRGB color space with 8 bits
per channel, so we'll use that for this example:
haskellghci> :set -XDataKinds ghci> import Graphics.Color.Space ghci> let rgb8 = ColorSRGB 128 255 65 :: Color (SRGB 'NonLinear) Word8 ghci> print rgb8 <SRGB 'NonLinear:(128,255, 65)>
Before we convert sRGB to CIE L*a*b* color space we need to increase the precision to
Double, because for now Word8 is not supported by the LAB color space implementation:
haskellghci> let rgb = toDouble <$> rgb8 ghci> print rgb <SRGB 'NonLinear:( 0.5019607843137255, 1.0000000000000000, 0.2549019607843137)>
In order to convert to another color space without changing the Illuminant we can use
convertColor function. So here is how we convert to CIELAB and extract the perceived
lightness L*:
haskellghci> let lab@(ColorLAB l _ _) = convertColor rgb :: Color (LAB D65) Double ghci> lab <LAB * D65:(90.0867507593648500,-65.7999116680496000,74.4643898323530600)> ghci> l 90.08675075936485
Color adaptation
When a change of Illuminant is also needed during color space conversion we can use
convert function
haskellghci> import Graphics.Color.Adaptation (convert) ghci> import qualified Graphics.Color.Illuminant.CIE1964 as CIE1964 ghci> let lab@(ColorLAB l _ _) = convert rgb :: Color (LAB 'CIE1964.D50) Double ghci> lab <LAB CIE1964 'D50:(90.2287735564601500,-59.3846969983265500,72.9304679742930800)>
External resources
-
Coloris on a list of curated Awesome Colour
resources. -
While working on this library the colour-science.org
and their Python implementation of colour
was used extensively as a reference.
Contributors
Showing top 8 contributors by commit count.
