{-
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 (foldl1')
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 :: forall a. Fractional a => a -> a -> a -> Colour a
cieXYZ a
x a
y a
z = forall a. Fractional a => a -> a -> a -> Colour a
rgb a
r a
g a
b
 where
  [a
r,a
g,a
b] = forall {b}. Num b => [[b]] -> [b] -> [b]
mult [[a]]
matrix [a
x,a
y,a
z]
  matrix :: [[a]]
matrix = forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a -> b) -> [a] -> [b]
map 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 :: forall a. Fractional a => Colour a -> (a, a, a)
cieXYZView Colour a
c = (a
x,a
y,a
z)
 where
  RGB a
r a
g a
b = forall a. Fractional a => Colour a -> RGB a
toRGB Colour a
c
  [a
x,a
y,a
z] = forall {b}. Num b => [[b]] -> [b] -> [b]
mult [[a]]
matrix [a
r,a
g,a
b]
  matrix :: [[a]]
matrix = forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a -> b) -> [a] -> [b]
map 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 = 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 :: forall a. Fractional a => Colour a -> a
luminance Colour a
c = a
y
 where
  (a
x,a
y,a
z) = forall a. Fractional a => Colour a -> (a, a, a)
toCIEXYZ Colour a
c

instance AffineSpace Chromaticity where
 affineCombo :: forall a.
Num a =>
[(a, Chromaticity a)] -> Chromaticity a -> Chromaticity a
affineCombo [(a, Chromaticity a)]
l Chromaticity a
z =
   forall a. (a -> a -> a) -> [a] -> a
foldl1' forall {a}.
Num a =>
Chromaticity a -> Chromaticity a -> Chromaticity a
chromaAdd [forall {a}. Num a => a -> Chromaticity a -> Chromaticity a
chromaScale a
w Chromaticity a
a | (a
w,Chromaticity a
a) <- (a
1forall a. Num a => a -> a -> a
-a
total,Chromaticity a
z)forall a. a -> [a] -> [a]
:[(a, Chromaticity a)]
l]
  where
   total :: a
total = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map 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) = forall a. a -> a -> Chromaticity a
Chroma (a
x0forall a. Num a => a -> a -> a
+a
x1) (a
y0forall a. Num a => a -> a -> a
+a
y1)
   a
s chromaScale :: a -> Chromaticity a -> Chromaticity a
`chromaScale` (Chroma a
x a
y) = forall a. a -> a -> Chromaticity a
Chroma (a
sforall a. Num a => a -> a -> a
*a
x) (a
sforall 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 :: forall a. Fractional a => Chromaticity a -> a -> Colour a
chromaColour Chromaticity a
ch a
y = forall a. Fractional a => a -> a -> a -> Colour a
cieXYZ (a
sforall a. Num a => a -> a -> a
*a
ch_x) a
y (a
sforall a. Num a => a -> a -> a
*a
ch_z)
 where
  (a
ch_x, a
ch_y, a
ch_z) = forall a. Fractional a => Chromaticity a -> (a, a, a)
chromaCoords Chromaticity a
ch
  s :: a
s = a
yforall 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 :: forall a. (Ord a, Floating a) => Chromaticity a -> Colour a -> a
lightness Chromaticity a
white_ch Colour a
c | (a
6forall a. Fractional a => a -> a -> a
/a
29)forall a b. (Num a, Integral b) => a -> b -> a
^Integer
3 forall a. Ord a => a -> a -> Bool
< a
y' = a
116forall a. Num a => a -> a -> a
*a
y'forall a. Floating a => a -> a -> a
**(a
1forall a. Fractional a => a -> a -> a
/a
3) forall a. Num a => a -> a -> a
- a
16
                     | Bool
otherwise = (a
29forall a. Fractional a => a -> a -> a
/a
3)forall a b. (Num a, Integral b) => a -> b -> a
^Integer
3forall a. Num a => a -> a -> a
*a
y'
 where
  white :: Colour a
white = forall a. Fractional a => Chromaticity a -> a -> Colour a
chromaColour Chromaticity a
white_ch a
1.0
  y' :: a
y' = (forall a. Fractional a => Colour a -> a
luminance Colour a
cforall a. Fractional a => a -> 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 :: forall a.
(Ord a, Floating a) =>
Chromaticity a -> Colour a -> (a, a, a)
cieLABView Chromaticity a
white_ch Colour a
c = (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 = forall a. Fractional a => Chromaticity a -> a -> Colour a
chromaColour Chromaticity a
white_ch a
1.0
  (a
x,a
y,a
z) = forall a. Fractional a => Colour a -> (a, a, a)
toCIEXYZ Colour a
c
  (a
xn,a
yn,a
zn) = forall a. Fractional a => Colour a -> (a, a, a)
toCIEXYZ Colour a
white
  (a
fx, a
fy, a
fz) = (forall {a}. (Floating a, Ord a) => a -> a
f (a
xforall a. Fractional a => a -> a -> a
/a
xn), forall {a}. (Floating a, Ord a) => a -> a
f (a
yforall a. Fractional a => a -> a -> a
/a
yn), forall {a}. (Floating a, Ord a) => a -> a
f (a
zforall a. Fractional a => a -> a -> a
/a
zn))
  a :: a
a = a
500forall a. Num a => a -> a -> a
*(a
fx forall a. Num a => a -> a -> a
- a
fy)
  b :: a
b = a
200forall a. Num a => a -> a -> a
*(a
fy forall a. Num a => a -> a -> a
- a
fz)
  f :: a -> a
f a
x | (a
6forall a. Fractional a => a -> a -> a
/a
29)forall a b. (Num a, Integral b) => a -> b -> a
^Integer
3 forall a. Ord a => a -> a -> Bool
< a
x = a
xforall a. Floating a => a -> a -> a
**(a
1forall a. Fractional a => a -> a -> a
/a
3)
      | Bool
otherwise = a
841forall a. Fractional a => a -> a -> a
/a
108forall a. Num a => a -> a -> a
*a
x forall a. Num a => a -> a -> a
+ a
4forall 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 :: forall a.
(Ord a, Floating a) =>
Chromaticity a -> a -> a -> a -> Colour a
cieLAB Chromaticity a
white_ch a
l a
a a
b = forall a. Fractional a => a -> a -> a -> Colour a
cieXYZ (a
xnforall a. Num a => a -> a -> a
*a -> a
transform a
fx)
                               (a
ynforall a. Num a => a -> a -> a
*a -> a
transform a
fy)
                               (a
znforall a. Num a => a -> a -> a
*a -> a
transform a
fz)
 where
  white :: Colour a
white = forall a. Fractional a => Chromaticity a -> a -> Colour a
chromaColour Chromaticity a
white_ch a
1.0
  (a
xn,a
yn,a
zn) = forall a. Fractional a => Colour a -> (a, a, a)
toCIEXYZ Colour a
white
  fx :: a
fx = a
fy forall a. Num a => a -> a -> a
+ a
aforall a. Fractional a => a -> a -> a
/a
500
  fy :: a
fy = (a
l forall a. Num a => a -> a -> a
+ a
16)forall a. Fractional a => a -> a -> a
/a
116
  fz :: a
fz = a
fy forall a. Num a => a -> a -> a
- a
bforall a. Fractional a => a -> a -> a
/a
200
  delta :: a
delta = a
6forall a. Fractional a => a -> a -> a
/a
29
  transform :: a -> a
transform a
fa | a
fa forall a. Ord a => a -> a -> Bool
> a
delta = a
faforall a b. (Num a, Integral b) => a -> b -> a
^Integer
3
               | Bool
otherwise = (a
fa forall a. Num a => a -> a -> a
- a
16forall a. Fractional a => a -> a -> a
/a
116)forall a. Num a => a -> a -> a
*a
3forall a. Num a => a -> a -> a
*a
deltaforall 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 :: forall a.
(Ord a, Floating a) =>
Chromaticity a -> Colour a -> (a, a, a)
cieLuv Chromaticity a
white_ch Colour a
c = (a
l, a
13forall a. Num a => a -> a -> a
*a
lforall a. Num a => a -> a -> a
*(a
u'forall a. Num a => a -> a -> a
-a
un'), a
13forall a. Num a => a -> a -> a
*a
lforall a. Num a => a -> a -> a
*(a
v'forall a. Num a => a -> a -> a
-a
vn'))
 where
  white :: Colour a
white = forall a. Fractional a => Chromaticity a -> a -> Colour a
chromaColour Chromaticity a
white_ch a
1.0
  (a
u', a
v') = forall a. (Ord a, Floating a) => Colour a -> (a, a)
u'v' Colour a
c
  (a
un', a
vn') = forall a. (Ord a, Floating a) => Colour a -> (a, a)
u'v' Colour a
white
  l :: a
l = 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' :: forall a. (Ord a, Floating a) => Colour a -> (a, a)
u'v' Colour a
c = (a
4forall a. Num a => a -> a -> a
*a
xforall a. Fractional a => a -> a -> a
/(a
xforall a. Num a => a -> a -> a
+a
15forall a. Num a => a -> a -> a
*a
yforall a. Num a => a -> a -> a
+a
3forall a. Num a => a -> a -> a
*a
z), a
9forall a. Num a => a -> a -> a
*a
yforall a. Fractional a => a -> a -> a
/(a
xforall a. Num a => a -> a -> a
+a
15forall a. Num a => a -> a -> a
*a
yforall a. Num a => a -> a -> a
+a
3forall a. Num a => a -> a -> a
*a
z))
 where
  (a
x,a
y,a
z) = forall a. Fractional a => Colour a -> (a, a, a)
toCIEXYZ Colour a
c

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

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