module Data.Colour.CIE
(Colour
,cieXYZ, cieXYZView, luminance
,toCIEXYZ
,Chromaticity
,mkChromaticity, chromaCoords
,chromaX, chromaY, chromaZ
,chromaConvert
,chromaColour
,lightness, cieLABView, cieLAB
)
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
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
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
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)
chromaColour :: (Fractional a) =>
Chromaticity a
-> a
-> 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
lightness :: (Ord a, Floating a) => Chromaticity a
-> 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)
cieLABView :: (Ord a, Floating a) => Chromaticity a
-> 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
cieLAB :: (Ord a, Floating a) => Chromaticity a
-> a
-> a
-> a
-> 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
cieLuv :: (Ord a, Floating a) => Chromaticity a
-> 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
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