    {-
    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.
    -}
    -- |Specifies 'Colour's in accordance with the sRGB standard.
    module Data.Colour.SRGB
     (Colour, RGB(..)
     ,sRGB24, sRGBBounded, sRGB
     ,toSRGB24, toSRGBBounded, toSRGB

     ,sRGB24shows, sRGB24show
     ,sRGB24reads, sRGB24read

     ,sRGBSpace
     )
    where

    import Data.Word
    import Numeric
    import Data.Colour.Internal (quantize)
    import Data.Colour.SRGB.Linear
    import Data.Colour.RGBSpace hiding (transferFunction)

    {- Non-linear colour space -}
    {- the sRGB transfer function approximates a gamma of about 1/2.2 -}
    transferFunction :: a -> a
    transferFunction lin :: a
    lin | a
    lin a -> a -> Bool
    forall a. Eq a => a -> a -> Bool
    == 1         = 1
                         | a
    lin a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= 0.0031308 = 12.92a -> a -> a
    forall a. Num a => a -> a -> a
    *a
    lin
                         | Bool
    otherwise        = (1 a -> a -> a
    forall a. Num a => a -> a -> a
    + a
    a)a -> a -> a
    forall a. Num a => a -> a -> a
    *a
    lina -> a -> a
    forall a. Floating a => a -> a -> a
    **(1a -> a -> a
    forall a. Fractional a => a -> a -> a
    /2.4) a -> a -> a
    forall a. Num a => a -> a -> a
    - a
    a
     where
      a :: a
    a = 0.055

    invTransferFunction :: a -> a
    invTransferFunction nonLin :: a
    nonLin | a
    nonLin a -> a -> Bool
    forall a. Eq a => a -> a -> Bool
    == 1       = 1
                               | a
    nonLin a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= 0.04045 = a
    nonLina -> a -> a
    forall a. Fractional a => a -> a -> a
    /12.92
                               | Bool
    otherwise         =
      ((a
    nonLin a -> a -> a
    forall a. Num a => a -> a -> a
    + a
    a)a -> a -> a
    forall a. Fractional a => a -> a -> a
    /(1 a -> a -> a
    forall a. Num a => a -> a -> a
    + a
    a))a -> a -> a
    forall a. Floating a => a -> a -> a
    **2.4
     where
      a :: a
    a = 0.055

    -- |Construct a colour from an sRGB specification.
    -- Input components are expected to be in the range [0..1].
    sRGB :: (Ord b, Floating b) =>  b -> b -> b -> Colour b
    sRGB :: b -> b -> b -> Colour b
    sRGB = (RGB b -> Colour b) -> b -> b -> b -> Colour b
    forall a b. (RGB a -> b) -> a -> a -> a -> b
    curryRGB ((b -> b -> b -> Colour b) -> RGB b -> Colour b
    forall a b. (a -> a -> a -> b) -> RGB a -> b
    uncurryRGB b -> b -> b -> Colour b
    forall a. Fractional a => a -> a -> a -> Colour a
    rgb (RGB b -> Colour b) -> (RGB b -> RGB b) -> RGB b -> Colour b
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (b -> b) -> RGB b -> RGB b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap b -> b
    forall a. (Ord a, Floating a) => a -> a
    invTransferFunction)

    -- |Construct a colour from an sRGB specification.
    -- Input components are expected to be in the range [0..'maxBound'].
    sRGBBounded :: (Ord b, Floating b, Integral a, Bounded a) =>
                   a -> a -> a -> Colour b
    sRGBBounded :: a -> a -> a -> Colour b
    sRGBBounded r' :: a
    r' g' :: a
    g' b' :: a
    b' = (b -> b -> b -> Colour b) -> RGB b -> Colour b
    forall a b. (a -> a -> a -> b) -> RGB a -> b
    uncurryRGB b -> b -> b -> Colour b
    forall b. (Ord b, Floating b) => b -> b -> b -> Colour b
    sRGB ((a -> b) -> RGB a -> RGB b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap a -> b
    forall a. Integral a => a -> b
    f (a -> a -> a -> RGB a
    forall a. a -> a -> a -> RGB a
    RGB a
    r' a
    g' a
    b'))
     where
      f :: a -> b
    f x' :: a
    x' = (a -> b
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral a
    x'b -> b -> b
    forall a. Fractional a => a -> a -> a
    /b
    m)
      m :: b
    m = a -> b
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a -> b) -> a -> b
    forall a b. (a -> b) -> a -> b
    $ a
    forall a. Bounded a => a
    maxBound a -> a -> a
    forall a. a -> a -> a
    `asTypeOf` a
    r'

    -- |Construct a colour from a 24-bit (three 8-bit words) sRGB
    -- specification.
    sRGB24 :: (Ord b, Floating b) => Word8 -> Word8 -> Word8 -> Colour b
    sRGB24 :: Word8 -> Word8 -> Word8 -> Colour b
    sRGB24 = Word8 -> Word8 -> Word8 -> Colour b
    forall b a.
    (Ord b, Floating b, Integral a, Bounded a) =>
    a -> a -> a -> Colour b
    sRGBBounded

    -- |Return the sRGB colour components in the range [0..1].
    toSRGB :: (Ord b, Floating b) => Colour b -> RGB b
    toSRGB :: Colour b -> RGB b
    toSRGB c :: Colour b
    c = (b -> b) -> RGB b -> RGB b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap b -> b
    forall a. (Ord a, Floating a) => a -> a
    transferFunction (Colour b -> RGB b
    forall a. Fractional a => Colour a -> RGB a
    toRGB Colour b
    c)

    {- Results are clamped and quantized -}
    -- |Return the approximate sRGB colour components in the range
    -- [0..'maxBound'].
    -- Out of range values are clamped.
    toSRGBBounded :: (RealFrac b, Floating b, Integral a, Bounded a) =>
                     Colour b -> RGB a
    toSRGBBounded :: Colour b -> RGB a
    toSRGBBounded c :: Colour b
    c = (b -> a) -> RGB b -> RGB a
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap b -> a
    f (Colour b -> RGB b
    forall b. (Ord b, Floating b) => Colour b -> RGB b
    toSRGB Colour b
    c)
     where
      f :: b -> a
    f x' :: b
    x' = b -> a
    forall a1 a. (RealFrac a1, Integral a, Bounded a) => a1 -> a
    quantize (b
    mb -> b -> b
    forall a. Num a => a -> a -> a
    *b
    x')
      m :: b
    m = a -> b
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (a -> b) -> a -> b
    forall a b. (a -> b) -> a -> b
    $ a
    forall a. Bounded a => a
    maxBound a -> a -> a
    forall a. a -> a -> a
    `asTypeOf` (b -> a
    f b
    forall a. HasCallStack => a
    undefined)

    -- |Return the approximate 24-bit sRGB colour components as three 8-bit
    -- components.
    -- Out of range values are clamped.
    toSRGB24 :: (RealFrac b, Floating b) => Colour b -> RGB Word8
    toSRGB24 :: Colour b -> RGB Word8
    toSRGB24 = Colour b -> RGB Word8
    forall b a.
    (RealFrac b, Floating b, Integral a, Bounded a) =>
    Colour b -> RGB a
    toSRGBBounded

    -- |Show a colour in hexadecimal form, e.g. \"#00aaff\"
    sRGB24shows :: (RealFrac b, Floating b) => Colour b -> ShowS
    sRGB24shows :: Colour b -> ShowS
    sRGB24shows c :: Colour b
    c =
      ("#"[Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++) ShowS -> ShowS -> ShowS
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Word8 -> ShowS
    forall a. (Integral a, Show a) => a -> ShowS
    showHex2 Word8
    r' ShowS -> ShowS -> ShowS
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Word8 -> ShowS
    forall a. (Integral a, Show a) => a -> ShowS
    showHex2 Word8
    g' ShowS -> ShowS -> ShowS
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Word8 -> ShowS
    forall a. (Integral a, Show a) => a -> ShowS
    showHex2 Word8
    b'
     where
      RGB r' :: Word8
    r' g' :: Word8
    g' b' :: Word8
    b' = Colour b -> RGB Word8
    forall b. (RealFrac b, Floating b) => Colour b -> RGB Word8
    toSRGB24 Colour b
    c
      showHex2 :: a -> ShowS
    showHex2 x :: a
    x | a
    x a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= 0xf = ("0"[Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++) ShowS -> ShowS -> ShowS
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . a -> ShowS
    forall a. (Integral a, Show a) => a -> ShowS
    showHex a
    x
                 | Bool
    otherwise = a -> ShowS
    forall a. (Integral a, Show a) => a -> ShowS
    showHex a
    x

    -- |Show a colour in hexadecimal form, e.g. \"#00aaff\"
    sRGB24show :: (RealFrac b, Floating b) => Colour b -> String
    sRGB24show :: Colour b -> [Char]
    sRGB24show x :: Colour b
    x = Colour b -> ShowS
    forall b. (RealFrac b, Floating b) => Colour b -> ShowS
    sRGB24shows Colour b
    x ""

    -- |Read a colour in hexadecimal form, e.g. \"#00aaff\" or \"00aaff\"
    sRGB24reads :: (Ord b, Floating b) => ReadS (Colour b)
    sRGB24reads :: ReadS (Colour b)
    sRGB24reads "" = []
    sRGB24reads x :: [Char]
    x =
      [(Word8 -> Word8 -> Word8 -> Colour b
    forall b.
    (Ord b, Floating b) =>
    Word8 -> Word8 -> Word8 -> Colour b
    sRGB24 Word8
    a Word8
    b Word8
    c, [Char]
    c0)
      |(a :: Word8
    a,a0 :: [Char]
    a0) <- [Char] -> [(Word8, [Char])]
    forall a. (Eq a, Num a) => [Char] -> [(a, [Char])]
    readPair [Char]
    x', (b :: Word8
    b,b0 :: [Char]
    b0) <- [Char] -> [(Word8, [Char])]
    forall a. (Eq a, Num a) => [Char] -> [(a, [Char])]
    readPair [Char]
    a0, (c :: Word8
    c,c0 :: [Char]
    c0) <- [Char] -> [(Word8, [Char])]
    forall a. (Eq a, Num a) => [Char] -> [(a, [Char])]
    readPair [Char]
    b0]
     where
      x' :: [Char]
    x' | [Char] -> Char
    forall a. [a] -> a
    head [Char]
    x Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == '#' = ShowS
    forall a. [a] -> [a]
    tail [Char]
    x
         | Bool
    otherwise = [Char]
    x
      readPair :: [Char] -> [(a, [Char])]
    readPair [] = []
      readPair [_] = []
      readPair a :: [Char]
    a = [(a
    x,[Char]
    a1)|(x :: a
    x,"") <- [Char] -> [(a, [Char])]
    forall a. (Eq a, Num a) => [Char] -> [(a, [Char])]
    readHex [Char]
    a0]
       where
        (a0 :: [Char]
    a0,a1 :: [Char]
    a1) = Int -> [Char] -> ([Char], [Char])
    forall a. Int -> [a] -> ([a], [a])
    splitAt 2 [Char]
    a

    -- |Read a colour in hexadecimal form, e.g. \"#00aaff\" or \"00aaff\"
    sRGB24read :: (Ord b, Floating b) => String -> (Colour b)
    sRGB24read :: [Char] -> Colour b
    sRGB24read x :: [Char]
    x | [(Colour b, [Char])] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [(Colour b, [Char])]
    rx Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    /= 1 Bool -> Bool -> Bool
    || Bool -> Bool
    not ([Char] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null ((Colour b, [Char]) -> [Char]
    forall a b. (a, b) -> b
    snd ([(Colour b, [Char])] -> (Colour b, [Char])
    forall a. [a] -> a
    head [(Colour b, [Char])]
    rx))) =
      [Char] -> Colour b
    forall a. HasCallStack => [Char] -> a
    error "Data.Colour.SRGB.sRGB24read: no parse"
                 | Bool
    otherwise = (Colour b, [Char]) -> Colour b
    forall a b. (a, b) -> a
    fst ([(Colour b, [Char])] -> (Colour b, [Char])
    forall a. [a] -> a
    head [(Colour b, [Char])]
    rx)
     where
      rx :: [(Colour b, [Char])]
    rx = ReadS (Colour b)
    forall b. (Ord b, Floating b) => ReadS (Colour b)
    sRGB24reads [Char]
    x

    -- |The sRGB colour space
    sRGBSpace :: (Ord a, Floating a) => RGBSpace a
    sRGBSpace :: RGBSpace a
    sRGBSpace = RGBGamut -> TransferFunction a -> RGBSpace a
    forall a. RGBGamut -> TransferFunction a -> RGBSpace a
    mkRGBSpace RGBGamut
    sRGBGamut TransferFunction a
    transfer
     where
      transfer :: TransferFunction a
    transfer = (a -> a) -> (a -> a) -> a -> TransferFunction a
    forall a. (a -> a) -> (a -> a) -> a -> TransferFunction a
    TransferFunction a -> a
    forall a. (Ord a, Floating a) => a -> a
    transferFunction a -> a
    forall a. (Ord a, Floating a) => a -> a
    invTransferFunction (a -> a
    forall a. Fractional a => a -> a
    recip 2.2)