{-
Copyright (c) 2008, 2009
Russell O'Connor

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-}
-- |Colour operations defined by the International Commission on
-- Illumination (CIE).
module Data.Colour.CIE
 (Colour
 ,cieXYZ, cieXYZView, luminance
 ,toCIEXYZ -- depricated

 ,Chromaticity
 ,mkChromaticity, chromaCoords
 ,chromaX, chromaY, chromaZ
 ,chromaConvert
 ,chromaColour

 ,lightness, cieLABView, cieLAB --cieLuv
 )
where

import Data.List
import Data.Colour
import Data.Colour.RGB
import Data.Colour.SRGB.Linear
import Data.Colour.CIE.Chromaticity
import Data.Colour.Matrix

-- |Construct a 'Colour' from XYZ coordinates for the 2° standard
-- (colourimetric) observer.
cieXYZ :: (Fractional a) => a -> a -> a -> Colour a
cieXYZ :: a -> a -> a -> Colour a
cieXYZ a
x a
y a
z = a -> a -> a -> Colour a
forall a. Fractional a => a -> a -> a -> Colour a
rgb a
r a
g a
b
 where
  [a
r,a
g,a
b] = [[a]] -> [a] -> [a]
forall b. Num b => [[b]] -> [b] -> [b]
mult [[a]]
matrix [a
x,a
y,a
z]
  matrix :: [[a]]
matrix = ([Rational] -> [a]) -> [[Rational]] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map ((Rational -> a) -> [Rational] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Rational -> a
forall a. Fractional a => Rational -> a
fromRational) [[Rational]]
xyz2rgb709

-- |Returns the XYZ colour coordinates for the 2° standard
-- (colourimetric) observer.
cieXYZView :: (Fractional a) => Colour a -> (a,a,a)
cieXYZView :: Colour a -> (a, a, a)
cieXYZView Colour a
c = (a
x,a
y,a
z)
 where
  RGB a
r a
g a
b = Colour a -> RGB a
forall a. Fractional a => Colour a -> RGB a
toRGB Colour a
c
  [a
x,a
y,a
z] = [[a]] -> [a] -> [a]
forall b. Num b => [[b]] -> [b] -> [b]
mult [[a]]
matrix [a
r,a
g,a
b]
  matrix :: [[a]]
matrix = ([Rational] -> [a]) -> [[Rational]] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map ((Rational -> a) -> [Rational] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Rational -> a
forall a. Fractional a => Rational -> a
fromRational) [[Rational]]
rgb7092xyz

{-# DEPRECATED toCIEXYZ "`toCIEXYZ' has been renamed `cieXYZView'" #-}
toCIEXYZ :: Colour a -> (a, a, a)
toCIEXYZ Colour a
x = Colour a -> (a, a, a)
forall a. Fractional a => Colour a -> (a, a, a)
cieXYZView Colour a
x

{- CIE luminance -}
-- |Returns the Y colour coordinate (luminance) for the 2° standard
-- (colourimetric) observer.
luminance :: (Fractional a) => Colour a -> a
luminance :: Colour a -> a
luminance Colour a
c = a
y
 where
  (a
x,a
y,a
z) = Colour a -> (a, a, a)
forall a. Fractional a => Colour a -> (a, a, a)
toCIEXYZ Colour a
c

instance AffineSpace Chromaticity where
 affineCombo :: [(a, Chromaticity a)] -> Chromaticity a -> Chromaticity a
affineCombo [(a, Chromaticity a)]
l Chromaticity a
z =
   (Chromaticity a -> Chromaticity a -> Chromaticity a)
-> [Chromaticity a] -> Chromaticity a
forall a. (a -> a -> a) -> [a] -> a
foldl1' Chromaticity a -> Chromaticity a -> Chromaticity a
forall a.
Num a =>
Chromaticity a -> Chromaticity a -> Chromaticity a
chromaAdd [a -> Chromaticity a -> Chromaticity a
forall a. Num a => a -> Chromaticity a -> Chromaticity a
chromaScale a
w Chromaticity a
a | (a
w,Chromaticity a
a) <- (a
1a -> a -> a
forall a. Num a => a -> a -> a
-a
total,Chromaticity a
z)(a, Chromaticity a)
-> [(a, Chromaticity a)] -> [(a, Chromaticity a)]
forall a. a -> [a] -> [a]
:[(a, Chromaticity a)]
l]
  where
   total :: a
total = [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([a] -> a) -> [a] -> a
forall a b. (a -> b) -> a -> b
$ ((a, Chromaticity a) -> a) -> [(a, Chromaticity a)] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (a, Chromaticity a) -> a
forall a b. (a, b) -> a
fst [(a, Chromaticity a)]
l
   (Chroma a
x0 a
y0) chromaAdd :: Chromaticity a -> Chromaticity a -> Chromaticity a
`chromaAdd` (Chroma a
x1 a
y1) = a -> a -> Chromaticity a
forall a. a -> a -> Chromaticity a
Chroma (a
x0a -> a -> a
forall a. Num a => a -> a -> a
+a
x1) (a
y0a -> a -> a
forall a. Num a => a -> a -> a
+a
y1)
   a
s chromaScale :: a -> Chromaticity a -> Chromaticity a
`chromaScale` (Chroma a
x a
y) = a -> a -> Chromaticity a
forall a. a -> a -> Chromaticity a
Chroma (a
sa -> a -> a
forall a. Num a => a -> a -> a
*a
x) (a
sa -> a -> a
forall a. Num a => a -> a -> a
*a
y)

-- |Constructs a colour from the given 'Chromaticity' and 'luminance'.
chromaColour :: (Fractional a) =>
                Chromaticity a
             -> a              -- ^ 'luminance'
             -> Colour a
chromaColour :: Chromaticity a -> a -> Colour a
chromaColour Chromaticity a
ch a
y = a -> a -> a -> Colour a
forall a. Fractional a => a -> a -> a -> Colour a
cieXYZ (a
sa -> a -> a
forall a. Num a => a -> a -> a
*a
ch_x) a
y (a
sa -> a -> a
forall a. Num a => a -> a -> a
*a
ch_z)
 where
  (a
ch_x, a
ch_y, a
ch_z) = Chromaticity a -> (a, a, a)
forall a. Fractional a => Chromaticity a -> (a, a, a)
chromaCoords Chromaticity a
ch
  s :: a
s = a
ya -> a -> a
forall a. Fractional a => a -> a -> a
/a
ch_y

-- |Returns the lightness of a colour with respect to a given white point.
-- Lightness is a perceptually uniform measure.
lightness :: (Ord a, Floating a) => Chromaticity a -- ^White point
                                 -> Colour a -> a
lightness :: Chromaticity a -> Colour a -> a
lightness Chromaticity a
white_ch Colour a
c | (a
6a -> a -> a
forall a. Fractional a => a -> a -> a
/a
29)a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
3 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
y' = a
116a -> a -> a
forall a. Num a => a -> a -> a
*a
y'a -> a -> a
forall a. Floating a => a -> a -> a
**(a
1a -> a -> a
forall a. Fractional a => a -> a -> a
/a
3) a -> a -> a
forall a. Num a => a -> a -> a
- a
16
                     | Bool
otherwise = (a
29a -> a -> a
forall a. Fractional a => a -> a -> a
/a
3)a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
3a -> a -> a
forall a. Num a => a -> a -> a
*a
y'
 where
  white :: Colour a
white = Chromaticity a -> a -> Colour a
forall a. Fractional a => Chromaticity a -> a -> Colour a
chromaColour Chromaticity a
white_ch a
1.0
  y' :: a
y' = (Colour a -> a
forall a. Fractional a => Colour a -> a
luminance Colour a
ca -> a -> a
forall a. Fractional a => a -> a -> a
/Colour a -> a
forall a. Fractional a => Colour a -> a
luminance Colour a
white)

-- |Returns the CIELAB coordinates of a colour, which is a
-- perceptually uniform colour space.
-- The first coordinate is 'lightness'.
-- If you don't know what white point to use, use
-- 'Data.Colour.CIE.Illuminant.d65'.
cieLABView :: (Ord a, Floating a) => Chromaticity a -- ^White point
                              -> Colour a -> (a,a,a)
cieLABView :: Chromaticity a -> Colour a -> (a, a, a)
cieLABView Chromaticity a
white_ch Colour a
c = (Chromaticity a -> Colour a -> a
forall a. (Ord a, Floating a) => Chromaticity a -> Colour a -> a
lightness Chromaticity a
white_ch Colour a
c, a
a, a
b)
 where
  white :: Colour a
white = Chromaticity a -> a -> Colour a
forall a. Fractional a => Chromaticity a -> a -> Colour a
chromaColour Chromaticity a
white_ch a
1.0
  (a
x,a
y,a
z) = Colour a -> (a, a, a)
forall a. Fractional a => Colour a -> (a, a, a)
toCIEXYZ Colour a
c
  (a
xn,a
yn,a
zn) = Colour a -> (a, a, a)
forall a. Fractional a => Colour a -> (a, a, a)
toCIEXYZ Colour a
white
  (a
fx, a
fy, a
fz) = (a -> a
forall a. (Floating a, Ord a) => a -> a
f (a
xa -> a -> a
forall a. Fractional a => a -> a -> a
/a
xn), a -> a
forall a. (Floating a, Ord a) => a -> a
f (a
ya -> a -> a
forall a. Fractional a => a -> a -> a
/a
yn), a -> a
forall a. (Floating a, Ord a) => a -> a
f (a
za -> a -> a
forall a. Fractional a => a -> a -> a
/a
zn))
  a :: a
a = a
500a -> a -> a
forall a. Num a => a -> a -> a
*(a
fx a -> a -> a
forall a. Num a => a -> a -> a
- a
fy)
  b :: a
b = a
200a -> a -> a
forall a. Num a => a -> a -> a
*(a
fy a -> a -> a
forall a. Num a => a -> a -> a
- a
fz)
  f :: a -> a
f a
x | (a
6a -> a -> a
forall a. Fractional a => a -> a -> a
/a
29)a -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
3 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
x = a
xa -> a -> a
forall a. Floating a => a -> a -> a
**(a
1a -> a -> a
forall a. Fractional a => a -> a -> a
/a
3)
      | Bool
otherwise = a
841a -> a -> a
forall a. Fractional a => a -> a -> a
/a
108a -> a -> a
forall a. Num a => a -> a -> a
*a
x a -> a -> a
forall a. Num a => a -> a -> a
+ a
4a -> a -> a
forall a. Fractional a => a -> a -> a
/a
29

-- |Returns the colour for given CIELAB coordinates, which is a
-- perceptually uniform colour space.
-- If you don't know what white point to use, use
-- 'Data.Colour.CIE.Illuminant.d65'.
cieLAB :: (Ord a, Floating a) => Chromaticity a -- ^White point
                              -> a              -- ^L* coordinate (lightness)
                              -> a              -- ^a* coordinate
                              -> a              -- ^b* coordinate
                              -> Colour a
cieLAB :: Chromaticity a -> a -> a -> a -> Colour a
cieLAB Chromaticity a
white_ch a
l a
a a
b = a -> a -> a -> Colour a
forall a. Fractional a => a -> a -> a -> Colour a
cieXYZ (a
xna -> a -> a
forall a. Num a => a -> a -> a
*a -> a
transform a
fx)
                               (a
yna -> a -> a
forall a. Num a => a -> a -> a
*a -> a
transform a
fy)
                               (a
zna -> a -> a
forall a. Num a => a -> a -> a
*a -> a
transform a
fz)
 where
  white :: Colour a
white = Chromaticity a -> a -> Colour a
forall a. Fractional a => Chromaticity a -> a -> Colour a
chromaColour Chromaticity a
white_ch a
1.0
  (a
xn,a
yn,a
zn) = Colour a -> (a, a, a)
forall a. Fractional a => Colour a -> (a, a, a)
toCIEXYZ Colour a
white
  fx :: a
fx = a
fy a -> a -> a
forall a. Num a => a -> a -> a
+ a
aa -> a -> a
forall a. Fractional a => a -> a -> a
/a
500
  fy :: a
fy = (a
l a -> a -> a
forall a. Num a => a -> a -> a
+ a
16)a -> a -> a
forall a. Fractional a => a -> a -> a
/a
116
  fz :: a
fz = a
fy a -> a -> a
forall a. Num a => a -> a -> a
- a
ba -> a -> a
forall a. Fractional a => a -> a -> a
/a
200
  delta :: a
delta = a
6a -> a -> a
forall a. Fractional a => a -> a -> a
/a
29
  transform :: a -> a
transform a
fa | a
fa a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
delta = a
faa -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
3
               | Bool
otherwise = (a
fa a -> a -> a
forall a. Num a => a -> a -> a
- a
16a -> a -> a
forall a. Fractional a => a -> a -> a
/a
116)a -> a -> a
forall a. Num a => a -> a -> a
*a
3a -> a -> a
forall a. Num a => a -> a -> a
*a
deltaa -> Integer -> a
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
2

-- |Returns the CIELUV coordinates of a colour, which is a
-- perceptually uniform colour space.
-- If you don't know what white point to use, use
-- 'Data.Colour.CIE.Illuminant.d65'.
cieLuv :: (Ord a, Floating a) => Chromaticity a -- ^White point
                              -> Colour a -> (a,a,a)
cieLuv :: Chromaticity a -> Colour a -> (a, a, a)
cieLuv Chromaticity a
white_ch Colour a
c = (a
l, a
13a -> a -> a
forall a. Num a => a -> a -> a
*a
la -> a -> a
forall a. Num a => a -> a -> a
*(a
u'a -> a -> a
forall a. Num a => a -> a -> a
-a
un'), a
13a -> a -> a
forall a. Num a => a -> a -> a
*a
la -> a -> a
forall a. Num a => a -> a -> a
*(a
v'a -> a -> a
forall a. Num a => a -> a -> a
-a
vn'))
 where
  white :: Colour a
white = Chromaticity a -> a -> Colour a
forall a. Fractional a => Chromaticity a -> a -> Colour a
chromaColour Chromaticity a
white_ch a
1.0
  (a
u', a
v') = Colour a -> (a, a)
forall a. (Ord a, Floating a) => Colour a -> (a, a)
u'v' Colour a
c
  (a
un', a
vn') = Colour a -> (a, a)
forall a. (Ord a, Floating a) => Colour a -> (a, a)
u'v' Colour a
white
  l :: a
l = Chromaticity a -> Colour a -> a
forall a. (Ord a, Floating a) => Chromaticity a -> Colour a -> a
lightness Chromaticity a
white_ch Colour a
c
--------------------------------------------------------------------------
{- not for export -}
u'v' :: (Ord a, Floating a) => Colour a -> (a,a)
u'v' :: Colour a -> (a, a)
u'v' Colour a
c = (a
4a -> a -> a
forall a. Num a => a -> a -> a
*a
xa -> a -> a
forall a. Fractional a => a -> a -> a
/(a
xa -> a -> a
forall a. Num a => a -> a -> a
+a
15a -> a -> a
forall a. Num a => a -> a -> a
*a
ya -> a -> a
forall a. Num a => a -> a -> a
+a
3a -> a -> a
forall a. Num a => a -> a -> a
*a
z), a
9a -> a -> a
forall a. Num a => a -> a -> a
*a
ya -> a -> a
forall a. Fractional a => a -> a -> a
/(a
xa -> a -> a
forall a. Num a => a -> a -> a
+a
15a -> a -> a
forall a. Num a => a -> a -> a
*a
ya -> a -> a
forall a. Num a => a -> a -> a
+a
3a -> a -> a
forall a. Num a => a -> a -> a
*a
z))
 where
  (a
x,a
y,a
z) = Colour a -> (a, a, a)
forall a. Fractional a => Colour a -> (a, a, a)
toCIEXYZ Colour a
c

rgb7092xyz :: [[Rational]]
rgb7092xyz = (RGBGamut -> [[Rational]]
rgb2xyz RGBGamut
sRGBGamut)

xyz2rgb709 :: [[Rational]]
xyz2rgb709 = [[Rational]] -> [[Rational]]
forall a. Fractional a => [[a]] -> [[a]]
inverse [[Rational]]
rgb7092xyz