{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NegativeLiterals #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeInType #-}
module Graphics.Color.Adaptation.VonKries
(
convert
, VonKries(..)
, CAT(..)
, ICAT(..)
, ChromaticAdaptationTransform
, cat
, icat
, vonKriesAdaptation
, bradfordAdaptation
, fairchildAdaptation
, ciecat02Adaptation
, cmccat2000Adaptation
, adaptationMatrix
) where
import Data.Coerce
import Data.Proxy
import Graphics.Color.Adaptation.Internal
import Graphics.Color.Algebra
import Graphics.Color.Space.Internal
import Data.Typeable
data VonKries
= VonKries
| Bradford
| Fairchild
| CIECAT02
| CMCCAT2000
newtype CAT (t :: k) e =
CAT (M3x3 e)
deriving (CAT t e -> CAT t e -> Bool
(CAT t e -> CAT t e -> Bool)
-> (CAT t e -> CAT t e -> Bool) -> Eq (CAT t e)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (t :: k) e. Eq e => CAT t e -> CAT t e -> Bool
/= :: CAT t e -> CAT t e -> Bool
$c/= :: forall k (t :: k) e. Eq e => CAT t e -> CAT t e -> Bool
== :: CAT t e -> CAT t e -> Bool
$c== :: forall k (t :: k) e. Eq e => CAT t e -> CAT t e -> Bool
Eq)
instance (Typeable t, Typeable k, Elevator e) => Show (CAT (t :: k) e) where
show :: CAT t e -> String
show m :: CAT t e
m@(CAT M3x3 e
m3x3) = CAT t e -> (Proxy (CAT t e) -> ShowS) -> ShowS
forall p t. p -> (Proxy p -> t) -> t
asProxy CAT t e
m Proxy (CAT t e) -> ShowS
forall k (t :: k) (proxy :: k -> *). Typeable t => proxy t -> ShowS
showsType String
"\n" String -> ShowS
forall a. [a] -> [a] -> [a]
++ M3x3 e -> String
forall a. Show a => a -> String
show M3x3 e
m3x3
newtype ICAT (t :: k) e =
ICAT (M3x3 e)
deriving (ICAT t e -> ICAT t e -> Bool
(ICAT t e -> ICAT t e -> Bool)
-> (ICAT t e -> ICAT t e -> Bool) -> Eq (ICAT t e)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (t :: k) e. Eq e => ICAT t e -> ICAT t e -> Bool
/= :: ICAT t e -> ICAT t e -> Bool
$c/= :: forall k (t :: k) e. Eq e => ICAT t e -> ICAT t e -> Bool
== :: ICAT t e -> ICAT t e -> Bool
$c== :: forall k (t :: k) e. Eq e => ICAT t e -> ICAT t e -> Bool
Eq)
instance (Typeable t, Typeable k, Elevator e) => Show (ICAT (t :: k) e) where
show :: ICAT t e -> String
show m :: ICAT t e
m@(ICAT M3x3 e
m3x3) = ICAT t e -> (Proxy (ICAT t e) -> ShowS) -> ShowS
forall p t. p -> (Proxy p -> t) -> t
asProxy ICAT t e
m Proxy (ICAT t e) -> ShowS
forall k (t :: k) (proxy :: k -> *). Typeable t => proxy t -> ShowS
showsType String
"\n" String -> ShowS
forall a. [a] -> [a] -> [a]
++ M3x3 e -> String
forall a. Show a => a -> String
show M3x3 e
m3x3
icat :: forall t e . (ChromaticAdaptationTransform t, RealFloat e) => ICAT t e
icat :: ICAT t e
icat = M3x3 e -> ICAT t e
forall k (t :: k) e. M3x3 e -> ICAT t e
ICAT (M3x3 e -> M3x3 e
forall a. Fractional a => M3x3 a -> M3x3 a
invertM3x3 M3x3 e
m3x3)
where CAT M3x3 e
m3x3 = CAT t e
forall (t :: VonKries) e.
(ChromaticAdaptationTransform t, RealFloat e) =>
CAT t e
cat :: CAT t e
class ChromaticAdaptationTransform (t :: VonKries) where
cat :: RealFloat e => CAT t e
instance ChromaticAdaptationTransform 'VonKries where
cat :: CAT 'VonKries e
cat = M3x3 e -> CAT 'VonKries e
forall k (t :: k) e. M3x3 e -> CAT t e
CAT (V3 e -> V3 e -> V3 e -> M3x3 e
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 (e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
0.40024 e
0.70760 e
-0.08081)
(e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
-0.22630 e
1.16532 e
0.04570)
(e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
0.00000 e
0.00000 e
0.91822))
instance ChromaticAdaptationTransform 'Bradford where
cat :: CAT 'Bradford e
cat = M3x3 e -> CAT 'Bradford e
forall k (t :: k) e. M3x3 e -> CAT t e
CAT (V3 e -> V3 e -> V3 e -> M3x3 e
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 (e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
0.8951 e
0.2664 e
-0.1614)
(e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
-0.7502 e
1.7135 e
0.0367)
(e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
0.0389 e
-0.0685 e
1.0296))
instance ChromaticAdaptationTransform 'Fairchild where
cat :: CAT 'Fairchild e
cat = M3x3 e -> CAT 'Fairchild e
forall k (t :: k) e. M3x3 e -> CAT t e
CAT (V3 e -> V3 e -> V3 e -> M3x3 e
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 (e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
0.8562 e
0.3372 e
-0.1934)
(e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
-0.8360 e
1.8327 e
0.0033)
(e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
0.0357 e
-0.0469 e
1.0112))
instance ChromaticAdaptationTransform 'CIECAT02 where
cat :: CAT 'CIECAT02 e
cat = M3x3 e -> CAT 'CIECAT02 e
forall k (t :: k) e. M3x3 e -> CAT t e
CAT (V3 e -> V3 e -> V3 e -> M3x3 e
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 (e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
0.7328 e
0.4296 e
-0.1624)
(e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
-0.7036 e
1.6975 e
0.0061)
(e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
0.0030 e
0.0136 e
0.9834))
instance ChromaticAdaptationTransform 'CMCCAT2000 where
cat :: CAT 'CMCCAT2000 e
cat = M3x3 e -> CAT 'CMCCAT2000 e
forall k (t :: k) e. M3x3 e -> CAT t e
CAT (V3 e -> V3 e -> V3 e -> M3x3 e
forall a. V3 a -> V3 a -> V3 a -> M3x3 a
M3x3 (e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
0.7982 e
0.3389 e
-0.1371)
(e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
-0.5918 e
1.5512 e
0.0406)
(e -> e -> e -> V3 e
forall a. a -> a -> a -> V3 a
V3 e
0.0008 e
0.0239 e
0.9753))
instance (Illuminant it, Illuminant ir, Elevator e, RealFloat e) =>
ChromaticAdaptation (t :: VonKries) (it :: kt) (ir :: kr) e where
newtype Adaptation (t :: VonKries) (it :: kt) (ir :: kr) e =
AdaptationMatrix (M3x3 e) deriving (Adaptation t it ir e -> Adaptation t it ir e -> Bool
(Adaptation t it ir e -> Adaptation t it ir e -> Bool)
-> (Adaptation t it ir e -> Adaptation t it ir e -> Bool)
-> Eq (Adaptation t it ir e)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (t :: VonKries) kt (it :: kt) kr (ir :: kr) e.
Eq e =>
Adaptation t it ir e -> Adaptation t it ir e -> Bool
/= :: Adaptation t it ir e -> Adaptation t it ir e -> Bool
$c/= :: forall (t :: VonKries) kt (it :: kt) kr (ir :: kr) e.
Eq e =>
Adaptation t it ir e -> Adaptation t it ir e -> Bool
== :: Adaptation t it ir e -> Adaptation t it ir e -> Bool
$c== :: forall (t :: VonKries) kt (it :: kt) kr (ir :: kr) e.
Eq e =>
Adaptation t it ir e -> Adaptation t it ir e -> Bool
Eq)
adaptColorXYZ :: Adaptation t it ir e -> Color (XYZ it) e -> Color (XYZ ir) e
adaptColorXYZ (AdaptationMatrix m3x3) Color (XYZ it) e
px = V3 e -> Color (XYZ ir) e
coerce (M3x3 e -> V3 e -> V3 e
forall a. Num a => M3x3 a -> V3 a -> V3 a
multM3x3byV3 M3x3 e
m3x3 (Color (XYZ it) e -> V3 e
coerce Color (XYZ it) e
px))
{-# INLINE adaptColorXYZ #-}
data I (i :: k) = I deriving Int -> I i -> ShowS
[I i] -> ShowS
I i -> String
(Int -> I i -> ShowS)
-> (I i -> String) -> ([I i] -> ShowS) -> Show (I i)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (i :: k). Int -> I i -> ShowS
forall k (i :: k). [I i] -> ShowS
forall k (i :: k). I i -> String
showList :: [I i] -> ShowS
$cshowList :: forall k (i :: k). [I i] -> ShowS
show :: I i -> String
$cshow :: forall k (i :: k). I i -> String
showsPrec :: Int -> I i -> ShowS
$cshowsPrec :: forall k (i :: k). Int -> I i -> ShowS
Show
instance (Illuminant it, Illuminant ir, Elevator e) =>
Show (Adaptation (t :: VonKries) (it :: kt) (ir :: kr) e) where
showsPrec :: Int -> Adaptation t it ir e -> ShowS
showsPrec Int
_ (AdaptationMatrix m3x3) =
(String
"AdaptationMatrix (" String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Proxy (I it) -> ShowS
forall k (t :: k) (proxy :: k -> *). Typeable t => proxy t -> ShowS
showsType (Proxy (I it)
forall k (t :: k). Proxy t
Proxy :: Proxy (I (it :: kt))) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(String
") (" String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proxy (I ir) -> ShowS
forall k (t :: k) (proxy :: k -> *). Typeable t => proxy t -> ShowS
showsType (Proxy (I ir)
forall k (t :: k). Proxy t
Proxy :: Proxy (I (ir :: kr))) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
")\n" String -> ShowS
forall a. [a] -> [a] -> [a]
++) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M3x3 e -> ShowS
forall a. Show a => a -> ShowS
shows M3x3 e
m3x3
adaptationMatrix ::
forall t it ir e. (ChromaticAdaptationTransform t, ChromaticAdaptation t it ir e)
=> Adaptation (t :: VonKries) it ir e
adaptationMatrix :: Adaptation t it ir e
adaptationMatrix =
M3x3 e -> Adaptation t it ir e
forall kt kr (t :: VonKries) (it :: kt) (ir :: kr) e.
M3x3 e -> Adaptation t it ir e
AdaptationMatrix (M3x3 e -> M3x3 e -> M3x3 e
forall a. Num a => M3x3 a -> M3x3 a -> M3x3 a
multM3x3byM3x3 (M3x3 e -> V3 e -> M3x3 e
forall a. Num a => M3x3 a -> V3 a -> M3x3 a
multM3x3byV3d M3x3 e
im3x3 V3 e
diag) M3x3 e
m3x3)
where
diag :: V3 e
diag = M3x3 e -> V3 e -> V3 e
forall a. Num a => M3x3 a -> V3 a -> V3 a
multM3x3byV3 M3x3 e
m3x3 V3 e
wpRef V3 e -> V3 e -> V3 e
forall a. Fractional a => a -> a -> a
/ M3x3 e -> V3 e -> V3 e
forall a. Num a => M3x3 a -> V3 a -> V3 a
multM3x3byV3 M3x3 e
m3x3 V3 e
wpTest
CAT M3x3 e
m3x3 = CAT t e
forall (t :: VonKries) e.
(ChromaticAdaptationTransform t, RealFloat e) =>
CAT t e
cat :: CAT t e
ICAT M3x3 e
im3x3 = ICAT t e
forall (t :: VonKries) e.
(ChromaticAdaptationTransform t, RealFloat e) =>
ICAT t e
icat :: ICAT t e
wpTest :: V3 e
wpTest = Color (XYZ it) e -> V3 e
coerce (Color (XYZ it) e
forall k (i :: k) e.
(Illuminant i, RealFloat e, Elevator e) =>
Color (XYZ i) e
whitePointTristimulus :: Color (XYZ it) e)
wpRef :: V3 e
wpRef = Color (XYZ ir) e -> V3 e
coerce (Color (XYZ ir) e
forall k (i :: k) e.
(Illuminant i, RealFloat e, Elevator e) =>
Color (XYZ i) e
whitePointTristimulus :: Color (XYZ ir) e)
vonKriesAdaptation :: ChromaticAdaptation 'VonKries it ir e => Adaptation 'VonKries it ir e
vonKriesAdaptation :: Adaptation 'VonKries it ir e
vonKriesAdaptation = Adaptation 'VonKries it ir e
forall kt kr (t :: VonKries) (it :: kt) (ir :: kr) e.
(ChromaticAdaptationTransform t, ChromaticAdaptation t it ir e) =>
Adaptation t it ir e
adaptationMatrix
{-# INLINE vonKriesAdaptation #-}
fairchildAdaptation :: ChromaticAdaptation 'Fairchild it ir e => Adaptation 'Fairchild it ir e
fairchildAdaptation :: Adaptation 'Fairchild it ir e
fairchildAdaptation = Adaptation 'Fairchild it ir e
forall kt kr (t :: VonKries) (it :: kt) (ir :: kr) e.
(ChromaticAdaptationTransform t, ChromaticAdaptation t it ir e) =>
Adaptation t it ir e
adaptationMatrix
{-# INLINE fairchildAdaptation #-}
bradfordAdaptation :: ChromaticAdaptation 'Bradford it ir e => Adaptation 'Bradford it ir e
bradfordAdaptation :: Adaptation 'Bradford it ir e
bradfordAdaptation = Adaptation 'Bradford it ir e
forall kt kr (t :: VonKries) (it :: kt) (ir :: kr) e.
(ChromaticAdaptationTransform t, ChromaticAdaptation t it ir e) =>
Adaptation t it ir e
adaptationMatrix
{-# INLINE bradfordAdaptation #-}
ciecat02Adaptation :: ChromaticAdaptation 'CIECAT02 it ir e => Adaptation 'CIECAT02 it ir e
ciecat02Adaptation :: Adaptation 'CIECAT02 it ir e
ciecat02Adaptation = Adaptation 'CIECAT02 it ir e
forall kt kr (t :: VonKries) (it :: kt) (ir :: kr) e.
(ChromaticAdaptationTransform t, ChromaticAdaptation t it ir e) =>
Adaptation t it ir e
adaptationMatrix
{-# INLINE ciecat02Adaptation #-}
cmccat2000Adaptation :: ChromaticAdaptation 'CMCCAT2000 it ir e => Adaptation 'CIECAT02 it ir e
cmccat2000Adaptation :: Adaptation 'CIECAT02 it ir e
cmccat2000Adaptation = Adaptation 'CIECAT02 it ir e
forall kt kr (t :: VonKries) (it :: kt) (ir :: kr) e.
(ChromaticAdaptationTransform t, ChromaticAdaptation t it ir e) =>
Adaptation t it ir e
adaptationMatrix
{-# INLINE cmccat2000Adaptation #-}
convert :: (ColorSpace cs' i' e', ColorSpace cs i e) => Color cs' e' -> Color cs e
convert :: Color cs' e' -> Color cs e
convert = Adaptation 'Bradford i' i Double -> Color cs' e' -> Color cs e
forall k kt kr (t :: k) (i' :: kt) (i :: kr) a cs' e' cs e.
(ChromaticAdaptation t i' i a, ColorSpace cs' i' e',
ColorSpace cs i e) =>
Adaptation t i' i a -> Color cs' e' -> Color cs e
convertElevatedWith ((ChromaticAdaptationTransform 'Bradford,
ChromaticAdaptation 'Bradford i' i Double) =>
Adaptation 'Bradford i' i Double
forall kt kr (t :: VonKries) (it :: kt) (ir :: kr) e.
(ChromaticAdaptationTransform t, ChromaticAdaptation t it ir e) =>
Adaptation t it ir e
adaptationMatrix @'Bradford @_ @_ @Double)
{-# INLINE convert #-}