{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
-- |
-- Module      : Graphics.Color.Space.RGB.ITU.Rec709
-- Copyright   : (c) Alexey Kuleshevich 2019-2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
--
module Graphics.Color.Space.RGB.ITU.Rec709
  ( pattern BT709
  , BT709
  , D65
  ) where

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 Graphics.Color.Space.RGB.ITU.Rec601 as Rec601 (D65, BT601_625)
import Graphics.Color.Space.RGB.Luma

-- | [ITU-R BT.709](https://en.wikipedia.org/wiki/Rec._709) color space
data BT709 (l :: Linearity)

newtype instance Color (BT709 l) e = BT709 (Color CM.RGB e)

-- | ITU-R BT.709 color space
deriving instance Eq e => Eq (Color (BT709 l) e)
-- | ITU-R BT.709 color space
deriving instance Ord e => Ord (Color (BT709 l) e)
-- | ITU-R BT.709 color space
deriving instance Functor (Color (BT709 l))
-- | ITU-R BT.709 color space
deriving instance Applicative (Color (BT709 l))
-- | ITU-R BT.709 color space
deriving instance Foldable (Color (BT709 l))
-- | ITU-R BT.709 color space
deriving instance Traversable (Color (BT709 l))
-- | ITU-R BT.709 color space
deriving instance Storable e => Storable (Color (BT709 l) e)

-- | ITU-R BT.709 color space
instance (Typeable l, Elevator e) => Show (Color (BT709 l) e) where
  showsPrec :: Int -> Color (BT709 l) e -> ShowS
showsPrec Int
_ = Color (BT709 l) e -> ShowS
forall cs e. ColorModel cs e => Color cs e -> ShowS
showsColorModel

-- | ITU-R BT.709 color space
instance (Typeable l, Elevator e) => ColorModel (BT709 l) e where
  type Components (BT709 l) e = (e, e, e)
  toComponents :: Color (BT709 l) e -> Components (BT709 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 (BT709 l) e -> Color RGB e)
-> Color (BT709 l) e
-> (e, e, e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (BT709 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 (BT709 l) e -> Color (BT709 l) e
fromComponents = Color RGB e -> Color (BT709 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 (BT709 l) e)
-> ((e, e, e) -> Color RGB e) -> (e, e, e) -> Color (BT709 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 #-}

-- | ITU-R BT.709 linear color space
instance Elevator e => ColorSpace (BT709 'Linear) D65 e where
  type BaseModel (BT709 'Linear) = CM.RGB
  toBaseSpace :: Color (BT709 'Linear) e -> Color (BaseSpace (BT709 'Linear)) e
toBaseSpace = Color (BT709 'Linear) e -> Color (BaseSpace (BT709 'Linear)) e
forall a. a -> a
id
  {-# INLINE toBaseSpace #-}
  fromBaseSpace :: Color (BaseSpace (BT709 'Linear)) e -> Color (BT709 'Linear) e
fromBaseSpace = Color (BaseSpace (BT709 'Linear)) e -> Color (BT709 'Linear) e
forall a. a -> a
id
  {-# INLINE fromBaseSpace #-}
  luminance :: Color (BT709 'Linear) e -> Color (Y D65) a
luminance = Color (BT709 'Linear) a -> Color (Y D65) 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 (BT709 'Linear) a -> Color (Y D65) a)
-> (Color (BT709 'Linear) e -> Color (BT709 'Linear) a)
-> Color (BT709 'Linear) e
-> Color (Y D65) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> a) -> Color (BT709 'Linear) e -> Color (BT709 '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
  {-# INLINE luminance #-}
  toColorXYZ :: Color (BT709 'Linear) e -> Color (XYZ D65) a
toColorXYZ = Color (BT709 'Linear) a -> Color (XYZ D65) 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 (BT709 'Linear) a -> Color (XYZ D65) a)
-> (Color (BT709 'Linear) e -> Color (BT709 'Linear) a)
-> Color (BT709 'Linear) e
-> Color (XYZ D65) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> a) -> Color (BT709 'Linear) e -> Color (BT709 '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
  {-# INLINE toColorXYZ #-}
  fromColorXYZ :: Color (XYZ D65) a -> Color (BT709 'Linear) e
fromColorXYZ = (a -> e) -> Color (BT709 'Linear) a -> Color (BT709 'Linear) e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> e
forall e a. (Elevator e, Elevator a, RealFloat a) => a -> e
fromRealFloat (Color (BT709 'Linear) a -> Color (BT709 'Linear) e)
-> (Color (XYZ D65) a -> Color (BT709 'Linear) a)
-> Color (XYZ D65) a
-> Color (BT709 'Linear) e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (XYZ D65) a -> Color (BT709 '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
  {-# INLINE fromColorXYZ #-}

-- | ITU-R BT.709 color space
instance Elevator e => ColorSpace (BT709 'NonLinear) D65 e where
  type BaseModel (BT709 'NonLinear) = CM.RGB
  toBaseSpace :: Color (BT709 'NonLinear) e
-> Color (BaseSpace (BT709 'NonLinear)) e
toBaseSpace = Color (BT709 'NonLinear) e
-> Color (BaseSpace (BT709 'NonLinear)) e
forall a. a -> a
id
  {-# INLINE toBaseSpace #-}
  fromBaseSpace :: Color (BaseSpace (BT709 'NonLinear)) e
-> Color (BT709 'NonLinear) e
fromBaseSpace = Color (BaseSpace (BT709 'NonLinear)) e
-> Color (BT709 'NonLinear) e
forall a. a -> a
id
  {-# INLINE fromBaseSpace #-}
  luminance :: Color (BT709 'NonLinear) e -> Color (Y D65) a
luminance = Color (BT709 'NonLinear) a -> Color (Y D65) a
forall k (cs :: Linearity -> *) (i :: k) e.
(RedGreenBlue cs i, ColorSpace (cs 'Linear) i e, RealFloat e) =>
Color (cs 'NonLinear) e -> Color (Y i) e
rgbLuminance (Color (BT709 'NonLinear) a -> Color (Y D65) a)
-> (Color (BT709 'NonLinear) e -> Color (BT709 'NonLinear) a)
-> Color (BT709 'NonLinear) e
-> Color (Y D65) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> a)
-> Color (BT709 'NonLinear) e -> Color (BT709 'NonLinear) 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 (BT709 'NonLinear) e -> Color (XYZ D65) a
toColorXYZ = Color (BT709 'NonLinear) a -> Color (XYZ D65) 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 'NonLinear) e -> Color (XYZ i) e
rgb2xyz (Color (BT709 'NonLinear) a -> Color (XYZ D65) a)
-> (Color (BT709 'NonLinear) e -> Color (BT709 'NonLinear) a)
-> Color (BT709 'NonLinear) e
-> Color (XYZ D65) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> a)
-> Color (BT709 'NonLinear) e -> Color (BT709 'NonLinear) 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 toColorXYZ #-}
  fromColorXYZ :: Color (XYZ D65) a -> Color (BT709 'NonLinear) e
fromColorXYZ = (a -> e)
-> Color (BT709 'NonLinear) a -> Color (BT709 'NonLinear) e
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> e
forall e a. (Elevator e, Elevator a, RealFloat a) => a -> e
fromRealFloat (Color (BT709 'NonLinear) a -> Color (BT709 'NonLinear) e)
-> (Color (XYZ D65) a -> Color (BT709 'NonLinear) a)
-> Color (XYZ D65) a
-> Color (BT709 'NonLinear) e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color (XYZ D65) a -> Color (BT709 'NonLinear) 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 'NonLinear) e
xyz2rgb
  {-# INLINE fromColorXYZ #-}

-- | ITU-R BT.709 color space
instance RedGreenBlue BT709 D65 where
  gamut :: Gamut BT709 D65 e
gamut = Primary D65 e
-> Primary D65 e -> Primary D65 e -> Gamut BT709 D65 e
forall k (cs :: Linearity -> *) (i :: k) e.
Primary i e -> Primary i e -> Primary i e -> Gamut cs i e
Gamut (e -> e -> Primary D65 e
forall k e (i :: k). e -> e -> Primary i e
Primary e
0.64 e
0.33)
                (e -> e -> Primary D65 e
forall k e (i :: k). e -> e -> Primary i e
Primary e
0.30 e
0.60)
                (e -> e -> Primary D65 e
forall k e (i :: k). e -> e -> Primary i e
Primary e
0.15 e
0.06)
  transfer :: e -> e
transfer = forall i e. (RedGreenBlue BT601_625 i, RealFloat e) => e -> e
forall k (cs :: Linearity -> *) (i :: k) e.
(RedGreenBlue cs i, RealFloat e) =>
e -> e
transfer @_ @BT601_625
  {-# INLINE transfer #-}
  itransfer :: e -> e
itransfer = forall i e. (RedGreenBlue BT601_625 i, RealFloat e) => e -> e
forall k (cs :: Linearity -> *) (i :: k) e.
(RedGreenBlue cs i, RealFloat e) =>
e -> e
itransfer @_ @BT601_625
  {-# INLINE itransfer #-}


instance Luma BT709 where
  rWeight :: Weight BT709 e
rWeight = Weight BT709 e
0.2126
  gWeight :: Weight BT709 e
gWeight = Weight BT709 e
0.7152
  bWeight :: Weight BT709 e
bWeight = Weight BT709 e
0.0722