{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
-- |
-- Module      : Graphics.Color.Space.RGB.Derived.CIERGB
-- Copyright   : (c) Alexey Kuleshevich 2018-2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
--
module Graphics.Color.Space.RGB.Derived.CIERGB
  ( CIERGB
  , castLinearity
  ) where

import Data.Coerce
import Data.Typeable
import Foreign.Storable
import Graphics.Color.Model.Internal
import qualified Graphics.Color.Model.RGB as CM
import Graphics.Color.Space.Internal
import Graphics.Color.Space.RGB.Internal
import qualified Graphics.Color.Space.CIE1931.RGB as CIERGB


-- | The original @CIE1931 RGB@ color space with an arbitrary illuminant
data CIERGB (i :: k) (l :: Linearity)

-- | `CIERGB` color space (derived)
newtype instance Color (CIERGB i l) e = CIERGB (Color CM.RGB e)

-- | `CIERGB` color space (derived)
deriving instance Eq e => Eq (Color (CIERGB i l) e)
-- | `CIERGB` color space (derived)
deriving instance Ord e => Ord (Color (CIERGB i l) e)
-- | `CIERGB` color space (derived)
deriving instance Functor (Color (CIERGB i l))
-- | `CIERGB` color space (derived)
deriving instance Applicative (Color (CIERGB i l))
-- | `CIERGB` color space (derived)
deriving instance Foldable (Color (CIERGB i l))
-- | `CIERGB` color space (derived)
deriving instance Traversable (Color (CIERGB i l))
-- | `CIERGB` color space (derived)
deriving instance Storable e => Storable (Color (CIERGB i l) e)

-- | `CIERGB` color space (derived)
instance (Typeable l, Illuminant i, Elevator e) => Show (Color (CIERGB (i :: k) l) e) where
  showsPrec :: Int -> Color (CIERGB i l) e -> ShowS
showsPrec Int
_ = Color (CIERGB i l) e -> ShowS
forall cs e. ColorModel cs e => Color cs e -> ShowS
showsColorModel

-- | `CIERGB` color space (derived)
instance (Typeable l, Illuminant i, Elevator e) => ColorModel (CIERGB (i :: k) l) e where
  type Components (CIERGB i l) e = (e, e, e)
  toComponents :: Color (CIERGB i l) e -> Components (CIERGB i l) e
toComponents = Color RGB e -> (e, e, e)
forall cs e. ColorModel cs e => Color cs e -> Components cs e
toComponents (Color RGB e -> (e, e, e))
-> (Color (CIERGB i l) e -> Color RGB e)
-> Color (CIERGB i l) e
-> (e, e, e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (CIERGB i l) e -> Color RGB e
forall k (cs :: Linearity -> *) (i :: k) (l :: Linearity) e.
RedGreenBlue cs i =>
Color (cs l) e -> Color RGB e
unColorRGB
  {-# INLINE toComponents #-}
  fromComponents :: Components (CIERGB i l) e -> Color (CIERGB i l) e
fromComponents = Color RGB e -> Color (CIERGB i l) e
forall k (cs :: Linearity -> *) (i :: k) e (l :: Linearity).
RedGreenBlue cs i =>
Color RGB e -> Color (cs l) e
mkColorRGB (Color RGB e -> Color (CIERGB i l) e)
-> ((e, e, e) -> Color RGB e) -> (e, e, e) -> Color (CIERGB i l) e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e, e, e) -> Color RGB e
forall cs e. ColorModel cs e => Components cs e -> Color cs e
fromComponents
  {-# INLINE fromComponents #-}

-- | `CIERGB` color space (derived)
instance (Illuminant i, Typeable l, Elevator e) => ColorSpace (CIERGB i l) i e where
  type BaseModel (CIERGB i l) = CM.RGB
  toBaseSpace :: Color (CIERGB i l) e -> Color (BaseSpace (CIERGB i l)) e
toBaseSpace = Color (CIERGB i l) e -> Color (BaseSpace (CIERGB i l)) e
forall a. a -> a
id
  {-# INLINE toBaseSpace #-}
  fromBaseSpace :: Color (BaseSpace (CIERGB i l)) e -> Color (CIERGB i l) e
fromBaseSpace = Color (BaseSpace (CIERGB i l)) e -> Color (CIERGB i l) e
forall a. a -> a
id
  {-# INLINE fromBaseSpace #-}
  luminance :: Color (CIERGB i l) e -> Color (Y i) a
luminance = Color (CIERGB i 'Linear) a -> Color (Y i) a
forall k (cs :: Linearity -> *) (i :: k) e.
(RedGreenBlue cs i, ColorSpace (cs 'Linear) i e, RealFloat e) =>
Color (cs 'Linear) e -> Color (Y i) e
rgbLinearLuminance (Color (CIERGB i 'Linear) a -> Color (Y i) a)
-> (Color (CIERGB i l) e -> Color (CIERGB i 'Linear) a)
-> Color (CIERGB i l) e
-> Color (Y i) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (CIERGB i l) a -> Color (CIERGB i 'Linear) a
forall k (i :: k) (l' :: Linearity) e (l :: Linearity).
Color (CIERGB i l') e -> Color (CIERGB i l) e
castLinearity (Color (CIERGB i l) a -> Color (CIERGB i 'Linear) a)
-> (Color (CIERGB i l) e -> Color (CIERGB i l) a)
-> Color (CIERGB i l) e
-> Color (CIERGB i 'Linear) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> a) -> Color (CIERGB i l) e -> Color (CIERGB i l) a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap e -> a
forall e a. (Elevator e, Elevator a, RealFloat a) => e -> a
toRealFloat
  {-# INLINE luminance #-}
  toColorXYZ :: Color (CIERGB i l) e -> Color (XYZ i) a
toColorXYZ = Color (CIERGB i 'Linear) a -> Color (XYZ i) a
forall k (cs :: Linearity -> *) (i :: k) e.
(RedGreenBlue cs i, ColorSpace (cs 'NonLinear) i e,
 ColorSpace (cs 'Linear) i e, RealFloat e) =>
Color (cs 'Linear) e -> Color (XYZ i) e
rgbLinear2xyz (Color (CIERGB i 'Linear) a -> Color (XYZ i) a)
-> (Color (CIERGB i l) e -> Color (CIERGB i 'Linear) a)
-> Color (CIERGB i l) e
-> Color (XYZ i) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> a)
-> Color (CIERGB i 'Linear) e -> Color (CIERGB i 'Linear) a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap e -> a
forall e a. (Elevator e, Elevator a, RealFloat a) => e -> a
toRealFloat (Color (CIERGB i 'Linear) e -> Color (CIERGB i 'Linear) a)
-> (Color (CIERGB i l) e -> Color (CIERGB i 'Linear) e)
-> Color (CIERGB i l) e
-> Color (CIERGB i 'Linear) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (CIERGB i l) e -> Color (CIERGB i 'Linear) e
forall k (i :: k) (l' :: Linearity) e (l :: Linearity).
Color (CIERGB i l') e -> Color (CIERGB i l) e
castLinearity
  {-# INLINE toColorXYZ #-}
  fromColorXYZ :: Color (XYZ i) a -> Color (CIERGB i l) e
fromColorXYZ Color (XYZ i) a
xyz = Color (CIERGB i 'Linear) e -> Color (CIERGB i l) e
forall k (i :: k) (l' :: Linearity) e (l :: Linearity).
Color (CIERGB i l') e -> Color (CIERGB i l) e
castLinearity (a -> e
forall e a. (Elevator e, Elevator a, RealFloat a) => a -> e
fromRealFloat (a -> e)
-> Color (CIERGB i 'Linear) a -> Color (CIERGB i 'Linear) e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Color (XYZ i) a -> Color (CIERGB i 'Linear) a
forall k (cs :: Linearity -> *) (i :: k) e.
(RedGreenBlue cs i, ColorSpace (cs 'NonLinear) i e,
 ColorSpace (cs 'Linear) i e, RealFloat e) =>
Color (XYZ i) e -> Color (cs 'Linear) e
xyz2rgbLinear @(CIERGB i) Color (XYZ i) a
xyz)
  {-# INLINE fromColorXYZ #-}

-- | `CIERGB` color space (derived)
instance Illuminant i => RedGreenBlue (CIERGB i) i where
  gamut :: Gamut (CIERGB i) i e
gamut = Gamut CIERGB 'E e -> Gamut (CIERGB i) i e
forall k1 k2 (cs' :: Linearity -> *) (i' :: k1) e
       (cs :: Linearity -> *) (i :: k2).
Gamut cs' i' e -> Gamut cs i e
coerceGamut (forall k (cs :: Linearity -> *) (i :: k) e.
(RedGreenBlue cs i, RealFloat e) =>
Gamut cs i e
forall (i :: CIE1931) e.
(RedGreenBlue CIERGB i, RealFloat e) =>
Gamut CIERGB i e
gamut @_ @CIERGB.CIERGB)
  transfer :: e -> e
transfer = e -> e
forall a. a -> a
id
  {-# INLINE transfer #-}
  itransfer :: e -> e
itransfer = e -> e
forall a. a -> a
id
  {-# INLINE itransfer #-}

-- | CIE RGB does not utilize any gamma function, therefore it is safe to cast the
-- `Linearity` kind.
--
-- @since 0.2.0
castLinearity :: Color (CIERGB i l') e -> Color (CIERGB i l) e
castLinearity :: Color (CIERGB i l') e -> Color (CIERGB i l) e
castLinearity = Color (CIERGB i l') e -> Color (CIERGB i l) e
coerce