    {-
    Copyright (c) 2008
    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.
    -}
    -- |An 'RGBSpace' is characterized by 'Chromaticity' for red, green, and
    -- blue, the 'Chromaticity' of the white point, and it's
    -- 'TransferFunction'.
    module Data.Colour.RGBSpace
     (Colour
      -- *RGB Tuple
     ,RGB(..)
     ,uncurryRGB, curryRGB

     -- *RGB Gamut
     ,RGBGamut
     ,mkRGBGamut, primaries, whitePoint
     ,inGamut
     -- *RGB Space
     ,TransferFunction(..)
     ,linearTransferFunction, powerTransferFunction
     ,inverseTransferFunction

     ,RGBSpace()
     ,mkRGBSpace ,gamut, transferFunction
     ,linearRGBSpace
     ,rgbUsingSpace
     ,toRGBUsingSpace
     )
    where

    import Data.Colour.CIE.Chromaticity
    import Data.Colour.Matrix
    import Data.Colour.RGB
    import Data.Colour.SRGB.Linear

    -- |Returns 'True' if the given colour lies inside the given gamut.
    inGamut :: (Ord a, Fractional a) => RGBGamut -> Colour a -> Bool
    inGamut :: forall a. (Ord a, Fractional a) => RGBGamut -> Colour a -> Bool
    inGamut RGBGamut
    gamut Colour a
    c = Bool
    r Bool -> Bool -> Bool
    && Bool
    g Bool -> Bool -> Bool
    && Bool
    b
     where
      test :: a -> Bool
    test a
    x = a
    0 a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= a
    x Bool -> Bool -> Bool
    && a
    x a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= a
    1
      RGB Bool
    r Bool
    g Bool
    b = (a -> Bool) -> RGB a -> RGB Bool
    forall a b. (a -> b) -> RGB a -> RGB b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap a -> Bool
    forall {a}. (Ord a, Num a) => a -> Bool
    test (RGBGamut -> Colour a -> RGB a
    forall a. Fractional a => RGBGamut -> Colour a -> RGB a
    toRGBUsingGamut RGBGamut
    gamut Colour a
    c)

    rtf :: (Fractional b, Real a) => [[a]] -> [[b]]
    rtf :: forall b a. (Fractional b, Real a) => [[a]] -> [[b]]
    rtf = ([a] -> [b]) -> [[a]] -> [[b]]
    forall a b. (a -> b) -> [a] -> [b]
    map ((a -> b) -> [a] -> [b]
    forall a b. (a -> b) -> [a] -> [b]
    map a -> b
    forall a b. (Real a, Fractional b) => a -> b
    realToFrac)

    rgbUsingGamut :: (Fractional a) => RGBGamut -> a -> a -> a -> Colour a
    rgbUsingGamut :: forall a. Fractional a => RGBGamut -> a -> a -> a -> Colour a
    rgbUsingGamut RGBGamut
    gamut a
    r a
    g a
    b = a -> a -> a -> Colour a
    forall a. Fractional a => a -> a -> a -> Colour a
    rgb a
    r0 a
    g0 a
    b0
     where
      matrix :: [[a]]
    matrix = [[Rational]] -> [[a]]
    forall b a. (Fractional b, Real a) => [[a]] -> [[b]]
    rtf ([[Rational]] -> [[a]]) -> [[Rational]] -> [[a]]
    forall a b. (a -> b) -> a -> b
    $ [[Rational]] -> [[Rational]] -> [[Rational]]
    forall {a}. Num a => [[a]] -> [[a]] -> [[a]]
    matrixMult (RGBGamut -> [[Rational]]
    xyz2rgb RGBGamut
    sRGBGamut) (RGBGamut -> [[Rational]]
    rgb2xyz RGBGamut
    gamut)
      [a
    r0,a
    g0,a
    b0] = [[a]] -> [a] -> [a]
    forall {b}. Num b => [[b]] -> [b] -> [b]
    mult [[a]]
    matrix [a
    r,a
    g,a
    b]

    toRGBUsingGamut :: (Fractional a) => RGBGamut -> Colour a -> RGB a
    toRGBUsingGamut :: forall a. Fractional a => RGBGamut -> Colour a -> RGB a
    toRGBUsingGamut RGBGamut
    gamut Colour a
    c = a -> a -> a -> RGB a
    forall a. a -> a -> a -> RGB a
    RGB a
    r a
    g a
    b
     where
      RGB a
    r0 a
    g0 a
    b0 = Colour a -> RGB a
    forall a. Fractional a => Colour a -> RGB a
    toRGB Colour a
    c
      matrix :: [[a]]
    matrix = [[Rational]] -> [[a]]
    forall b a. (Fractional b, Real a) => [[a]] -> [[b]]
    rtf ([[Rational]] -> [[a]]) -> [[Rational]] -> [[a]]
    forall a b. (a -> b) -> a -> b
    $ [[Rational]] -> [[Rational]] -> [[Rational]]
    forall {a}. Num a => [[a]] -> [[a]] -> [[a]]
    matrixMult (RGBGamut -> [[Rational]]
    xyz2rgb RGBGamut
    gamut) (RGBGamut -> [[Rational]]
    rgb2xyz RGBGamut
    sRGBGamut)
      [a
    r,a
    g,a
    b] = [[a]] -> [a] -> [a]
    forall {b}. Num b => [[b]] -> [b] -> [b]
    mult [[a]]
    matrix [a
    r0,a
    g0,a
    b0]

    -- |A 'transfer' function is a function that typically translates linear
    -- colour space coordinates into non-linear coordinates.
    -- The 'transferInverse' function reverses this by translating non-linear
    -- colour space coordinates into linear coordinates.
    -- It is required that
    --
    -- > transfer . transferInverse === id === transferInverse . inverse
    --
    -- (or that this law holds up to floating point rounding errors).
    --
    -- We also require that 'transfer' is approximately @(**transferGamma)@
    -- (and hence 'transferInverse' is approximately
    -- @(**(recip transferGamma))@).
    -- The value 'transferGamma' is for informational purposes only, so there
    -- is no bound on how good this approximation needs to be.
    data TransferFunction a = TransferFunction
                              { forall a. TransferFunction a -> a -> a
    transfer :: a -> a
                              , forall a. TransferFunction a -> a -> a
    transferInverse :: a -> a
                              , forall a. TransferFunction a -> a
    transferGamma :: a }

    -- |This is the identity 'TransferFunction'.
    linearTransferFunction :: (Num a) => TransferFunction a
    linearTransferFunction :: forall a. Num a => TransferFunction a
    linearTransferFunction = (a -> a) -> (a -> a) -> a -> TransferFunction a
    forall a. (a -> a) -> (a -> a) -> a -> TransferFunction a
    TransferFunction a -> a
    forall a. a -> a
    id a -> a
    forall a. a -> a
    id a
    1

    -- |This is the @(**gamma)@ 'TransferFunction'.
    powerTransferFunction :: (Floating a) => a -> TransferFunction a
    powerTransferFunction :: forall a. Floating a => a -> TransferFunction a
    powerTransferFunction a
    gamma =
      (a -> a) -> (a -> a) -> a -> TransferFunction a
    forall a. (a -> a) -> (a -> a) -> a -> TransferFunction a
    TransferFunction (a -> a -> a
    forall a. Floating a => a -> a -> a
    **a
    gamma) (a -> a -> a
    forall a. Floating a => a -> a -> a
    **(a -> a
    forall a. Fractional a => a -> a
    recip a
    gamma)) a
    gamma

    -- |This reverses a 'TransferFunction'.
    inverseTransferFunction :: (Fractional a) => TransferFunction a -> TransferFunction a
    inverseTransferFunction :: forall a. Fractional a => TransferFunction a -> TransferFunction a
    inverseTransferFunction (TransferFunction a -> a
    for a -> a
    rev a
    g) =
      (a -> a) -> (a -> a) -> a -> TransferFunction a
    forall a. (a -> a) -> (a -> a) -> a -> TransferFunction a
    TransferFunction a -> a
    rev a -> a
    for (a -> a
    forall a. Fractional a => a -> a
    recip a
    g)

    instance (Num a) => Semigroup (TransferFunction a) where
     (TransferFunction a -> a
    f0 a -> a
    f1 a
    f) <> :: TransferFunction a -> TransferFunction a -> TransferFunction a
    <> (TransferFunction a -> a
    g0 a -> a
    g1 a
    g) =
       ((a -> a) -> (a -> a) -> a -> TransferFunction a
    forall a. (a -> a) -> (a -> a) -> a -> TransferFunction a
    TransferFunction (a -> a
    f0 (a -> a) -> (a -> a) -> a -> a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> a
    g0) (a -> a
    g1 (a -> a) -> (a -> a) -> a -> a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> a
    f1) (a
    fa -> a -> a
    forall a. Num a => a -> a -> a
    *a
    g))

    instance (Num a) => Monoid (TransferFunction a) where
     mempty :: TransferFunction a
    mempty = TransferFunction a
    forall a. Num a => TransferFunction a
    linearTransferFunction

    -- |An 'RGBSpace' is a colour coordinate system for colours laying
    -- 'inGamut' of 'gamut'.
    -- Linear coordinates are passed through a 'transferFunction' to
    -- produce non-linear 'RGB' values.
    data RGBSpace a = RGBSpace { forall a. RGBSpace a -> RGBGamut
    gamut :: RGBGamut,
                                 forall a. RGBSpace a -> TransferFunction a
    transferFunction :: TransferFunction a }

    -- |An RGBSpace is specified by an 'RGBGamut' and a 'TransferFunction'.
    mkRGBSpace :: RGBGamut
               -> TransferFunction a
               -> RGBSpace a
    mkRGBSpace :: forall a. RGBGamut -> TransferFunction a -> RGBSpace a
    mkRGBSpace = RGBGamut -> TransferFunction a -> RGBSpace a
    forall a. RGBGamut -> TransferFunction a -> RGBSpace a
    RGBSpace

    -- |Produce a linear colour space from an 'RGBGamut'.
    linearRGBSpace :: (Num a) => RGBGamut -> RGBSpace a
    linearRGBSpace :: forall a. Num a => RGBGamut -> RGBSpace a
    linearRGBSpace RGBGamut
    gamut = RGBGamut -> TransferFunction a -> RGBSpace a
    forall a. RGBGamut -> TransferFunction a -> RGBSpace a
    RGBSpace RGBGamut
    gamut TransferFunction a
    forall a. Monoid a => a
    mempty

    -- |Create a 'Colour' from red, green, and blue coordinates given in a
    -- general 'RGBSpace'.
    rgbUsingSpace :: (Fractional a) => RGBSpace a -> a -> a -> a -> Colour a
    rgbUsingSpace :: forall a. Fractional a => RGBSpace a -> a -> a -> a -> Colour a
    rgbUsingSpace RGBSpace a
    space =
      (RGB a -> Colour a) -> a -> a -> a -> Colour a
    forall a b. (RGB a -> b) -> a -> a -> a -> b
    curryRGB ((a -> a -> a -> Colour a) -> RGB a -> Colour a
    forall a b. (a -> a -> a -> b) -> RGB a -> b
    uncurryRGB (RGBGamut -> a -> a -> a -> Colour a
    forall a. Fractional a => RGBGamut -> a -> a -> a -> Colour a
    rgbUsingGamut (RGBSpace a -> RGBGamut
    forall a. RGBSpace a -> RGBGamut
    gamut RGBSpace a
    space)) (RGB a -> Colour a) -> (RGB a -> RGB a) -> RGB a -> Colour a
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (a -> a) -> RGB a -> RGB a
    forall a b. (a -> b) -> RGB a -> RGB b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap a -> a
    tinv)
     where
      tinv :: a -> a
    tinv = TransferFunction a -> a -> a
    forall a. TransferFunction a -> a -> a
    transferInverse (RGBSpace a -> TransferFunction a
    forall a. RGBSpace a -> TransferFunction a
    transferFunction RGBSpace a
    space)

    -- |Return the coordinates of a given 'Colour' for a general 'RGBSpace'.
    toRGBUsingSpace :: (Fractional a) => RGBSpace a -> Colour a -> RGB a
    toRGBUsingSpace :: forall a. Fractional a => RGBSpace a -> Colour a -> RGB a
    toRGBUsingSpace RGBSpace a
    space Colour a
    c = (a -> a) -> RGB a -> RGB a
    forall a b. (a -> b) -> RGB a -> RGB b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap a -> a
    t (RGBGamut -> Colour a -> RGB a
    forall a. Fractional a => RGBGamut -> Colour a -> RGB a
    toRGBUsingGamut (RGBSpace a -> RGBGamut
    forall a. RGBSpace a -> RGBGamut
    gamut RGBSpace a
    space) Colour a
    c)
     where
      t :: a -> a
    t = TransferFunction a -> a -> a
    forall a. TransferFunction a -> a -> a
    transfer (RGBSpace a -> TransferFunction a
    forall a. RGBSpace a -> TransferFunction a
    transferFunction RGBSpace a
    space)