{-
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 (Word8)
import Numeric (readHex, showHex)
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 a
lin | a
lin forall a. Eq a => a -> a -> Bool
== a
1         = a
1
                     | a
lin forall a. Ord a => a -> a -> Bool
<= a
0.0031308 = a
12.92forall a. Num a => a -> a -> a
*a
lin
                     | Bool
otherwise        = (a
1 forall a. Num a => a -> a -> a
+ a
a)forall a. Num a => a -> a -> a
*a
linforall a. Floating a => a -> a -> a
**(a
1forall a. Fractional a => a -> a -> a
/a
2.4) forall a. Num a => a -> a -> a
- a
a
 where
  a :: a
a = a
0.055

invTransferFunction :: a -> a
invTransferFunction a
nonLin | a
nonLin forall a. Eq a => a -> a -> Bool
== a
1       = a
1
                           | a
nonLin forall a. Ord a => a -> a -> Bool
<= a
0.04045 = a
nonLinforall a. Fractional a => a -> a -> a
/a
12.92
                           | Bool
otherwise         =
  ((a
nonLin forall a. Num a => a -> a -> a
+ a
a)forall a. Fractional a => a -> a -> a
/(a
1 forall a. Num a => a -> a -> a
+ a
a))forall a. Floating a => a -> a -> a
**a
2.4
 where
  a :: 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 :: forall b. (Ord b, Floating b) => b -> b -> b -> Colour b
sRGB = forall a b. (RGB a -> b) -> a -> a -> a -> b
curryRGB (forall a b. (a -> a -> a -> b) -> RGB a -> b
uncurryRGB forall a. Fractional a => a -> a -> a -> Colour a
rgb forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap 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 :: forall b a.
(Ord b, Floating b, Integral a, Bounded a) =>
a -> a -> a -> Colour b
sRGBBounded a
r' a
g' a
b' = forall a b. (a -> a -> a -> b) -> RGB a -> b
uncurryRGB forall b. (Ord b, Floating b) => b -> b -> b -> Colour b
sRGB (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {a}. Integral a => a -> b
f (forall a. a -> a -> a -> RGB a
RGB a
r' a
g' a
b'))
 where
  f :: a -> b
f a
x' = (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
x'forall a. Fractional a => a -> a -> a
/b
m)
  m :: b
m = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Bounded a => a
maxBound 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 :: forall b.
(Ord b, Floating b) =>
Word8 -> Word8 -> Word8 -> Colour b
sRGB24 = 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 :: forall b. (Ord b, Floating b) => Colour b -> RGB b
toSRGB Colour b
c = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall {a}. (Ord a, Floating a) => a -> a
transferFunction (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 :: forall b a.
(RealFrac b, Floating b, Integral a, Bounded a) =>
Colour b -> RGB a
toSRGBBounded Colour b
c = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> a
f (forall b. (Ord b, Floating b) => Colour b -> RGB b
toSRGB Colour b
c)
 where
  f :: b -> a
f b
x' = forall a1 a. (RealFrac a1, Integral a, Bounded a) => a1 -> a
quantize (b
mforall a. Num a => a -> a -> a
*b
x')
  m :: b
m = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Bounded a => a
maxBound forall a. a -> a -> a
`asTypeOf` (b -> a
f 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 :: forall b. (RealFrac b, Floating b) => Colour b -> RGB Word8
toSRGB24 = 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 :: forall b. (RealFrac b, Floating b) => Colour b -> ShowS
sRGB24shows Colour b
c =
  (String
"#"forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. (Integral a, Show a) => a -> ShowS
showHex2 Word8
r' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. (Integral a, Show a) => a -> ShowS
showHex2 Word8
g' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. (Integral a, Show a) => a -> ShowS
showHex2 Word8
b'
 where
  RGB Word8
r' Word8
g' Word8
b' = forall b. (RealFrac b, Floating b) => Colour b -> RGB Word8
toSRGB24 Colour b
c
  showHex2 :: a -> ShowS
showHex2 a
x | a
x forall a. Ord a => a -> a -> Bool
<= a
0xf = (String
"0"forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {a}. (Integral a, Show a) => a -> ShowS
showHex a
x
             | Bool
otherwise = 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 :: forall b. (RealFrac b, Floating b) => Colour b -> String
sRGB24show Colour b
x = forall b. (RealFrac b, Floating b) => Colour b -> ShowS
sRGB24shows Colour b
x String
""

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

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

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