{-
Copyright (c) 2008, 2009
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.
-}
-- |Datatypes for representing the human perception of colour.
-- Includes common operations for blending and compositing colours.
-- The most common way of creating colours is either by name
-- (see "Data.Colour.Names") or by giving an sRGB triple
-- (see "Data.Colour.SRGB").
--
-- Methods of specifying Colours can be found in
--
-- - "Data.Colour.SRGB"
--
-- - "Data.Colour.SRGB.Linear"
--
-- - "Data.Colour.CIE"
--
-- Colours can be specified in a generic 'Data.Colour.RGBSpace.RGBSpace'
-- by using
--
-- - "Data.Colour.RGBSpace"

--TODO
-- - "Data.Colour.HDTV"
--
-- - "Data.Colour.SDTV"

module Data.Colour
 (
-- *Interfacing with Other Libraries\' Colour Spaces
--
-- |Executive summary: Always use "Data.Colour.SRGB" when interfacing with
-- other libraries.
-- Use 'Data.Colour.SRGB.toSRGB24' \/ 'Data.Colour.SRGB.sRGB24' when
-- interfacing with libraries wanting 'Data.Word.Word8' per channel.
-- Use 'Data.Colour.SRGB.toSRGB' \/ 'Data.Colour.SRGB.sRGB' when
-- interfacing with libraries wanting 'Double' or 'Float' per channel.
--
-- Interfacing with the colour for other libraries, such as cairo
-- (<http://www.haskell.org/gtk2hs/archives/category/cairo/>) and OpenGL
-- (<http://hackage.haskell.org/cgi-bin/hackage-scripts/package/OpenGL>),
-- can be a challenge because these libraries often do not use colour spaces
-- in a consistent way.
-- The problem is that these libraries work in a device dependent colour
-- space and give no indication what the colour space is.
-- For most devices this colours space is implicitly the non-linear sRGB
-- space.
-- However, to make matters worse, these libraries also do their
-- compositing and blending in the device colour space.
-- Blending and compositing ought to be done in a linear colour space,
-- but since the device space is typically non-linear sRGB, these libraries
-- typically produce colour blends that are too dark.
--
-- (Note that "Data.Colour" is a device /independent/ colour space, and
-- produces correct blends.
-- e.g. compare @toSRGB (blend 0.5 lime red)@ with @RGB 0.5 0.5 0@)
--
-- Because these other colour libraries can only blend in device colour
-- spaces, they are fundamentally broken and there is no \"right\" way
-- to interface with them.
-- For most libraries, the best one can do is assume they are working
-- with an sRGB colour space and doing incorrect blends.
-- In these cases use "Data.Colour.SRGB" to convert to and from the
-- colour coordinates.  This is the best advice for interfacing with cairo.
--
-- When using OpenGL, the choice is less clear.
-- Again, OpenGL usually does blending in the device colour space.
-- However, because blending is an important part of proper shading, one
-- may want to consider that OpenGL is working in a linear colour space,
-- and the resulting rasters are improperly displayed.
-- This is born out by the fact that OpenGL extensions that support
-- sRGB do so by converting sRGB input\/output to linear colour coordinates
-- for processing by OpenGL.
--
-- The best way to use OpenGL, is to use proper sRGB surfaces for textures
-- and rendering.
-- These surfaces will automatically convert to and from OpenGL's linear
-- colour space.
-- In this case, use "Data.Colour.SRGB.Linear" to interface OpenGL's linear
-- colour space.
--
-- If not using proper surfaces with OpenGL, then you have a choice between
-- having OpenGL do improper blending or improper display
-- If you are using OpenGL for 3D shading, I recommend using
-- "Data.Colour.SRGB.Linear" (thus choosing improper OpenGL display).
-- If you are not using OpenGL for 3D shading, I recommend using
-- "Data.Colour.SRGB" (thus choosing improper OpenGL blending).

-- *Colour type
  Colour
 ,colourConvert
 ,black

 ,AlphaColour
 ,opaque, withOpacity
 ,transparent
 ,alphaColourConvert
 ,alphaChannel

 -- *Colour operations
 -- |These operations allow combine and modify existing colours
 ,AffineSpace(..), blend

 ,ColourOps(..)
 ,dissolve, atop
 )
where

import Data.Char
import Data.Colour.Internal
import qualified Data.Colour.SRGB.Linear
import Data.Colour.CIE.Chromaticity (app_prec, infix_prec)

instance (Fractional a, Show a) => Show (Colour a) where
  showsPrec :: Int -> Colour a -> ShowS
showsPrec Int
d Colour a
c = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
app_prec) ShowS
showStr
   where
    showStr :: ShowS
showStr = String -> ShowS
showString String
linearConstructorQualifiedName
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec (Int
app_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) a
r)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec (Int
app_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) a
g)
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec (Int
app_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) a
b)
    Data.Colour.SRGB.Linear.RGB a
r a
g a
b = Colour a -> RGB a
forall a. Fractional a => Colour a -> RGB a
Data.Colour.SRGB.Linear.toRGB Colour a
c

instance (Fractional a, Read a) => Read (Colour a) where
  readsPrec :: Int -> ReadS (Colour a)
readsPrec Int
d String
r = Bool -> ReadS (Colour a) -> ReadS (Colour a)
forall a. Bool -> ReadS a -> ReadS a
readParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
app_prec)
                  (\String
r -> [(a -> a -> a -> Colour a
forall a. Fractional a => a -> a -> a -> Colour a
Data.Colour.SRGB.Linear.rgb a
r0 a
g0 a
b0,String
t)
                         |(String
name,String
s) <- String -> [(String, String)]
mylex String
r
                         ,String
name String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String
linearConstructorName
                                      ,String
linearConstructorQualifiedName]
                         ,(a
r0,String
s0) <- Int -> ReadS a
forall a. Read a => Int -> ReadS a
readsPrec (Int
app_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) String
s
                         ,(a
g0,String
s1) <- Int -> ReadS a
forall a. Read a => Int -> ReadS a
readsPrec (Int
app_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) String
s0
                         ,(a
b0,String
t)  <- Int -> ReadS a
forall a. Read a => Int -> ReadS a
readsPrec (Int
app_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) String
s1]) String
r
   where
    mylex :: String -> [(String, String)]
mylex = (String, String) -> [(String, String)]
forall (m :: * -> *) a. Monad m => a -> m a
return
          ((String, String) -> [(String, String)])
-> (String -> (String, String)) -> String -> [(String, String)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (\Char
c -> Char -> Bool
isAlphaNum Char
c Bool -> Bool -> Bool
|| Char
c Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
"._'")
          (String -> (String, String)) -> ShowS -> String -> (String, String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace

linearConstructorQualifiedName :: String
linearConstructorQualifiedName = String
"Data.Colour.SRGB.Linear.rgb"
linearConstructorName :: String
linearConstructorName = String
"rgb"

instance (Fractional a, Show a, Eq a) => Show (AlphaColour a) where
  showsPrec :: Int -> AlphaColour a -> ShowS
showsPrec Int
d AlphaColour a
ac | a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 = String -> ShowS
showString String
"transparent"
                 | Bool
otherwise = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
infix_prec) ShowS
showStr
   where
    showStr :: ShowS
showStr = Int -> Colour a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec (Int
infix_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Colour a
c
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" `withOpacity` "
            ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec (Int
infix_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) a
a
    a :: a
a = AlphaColour a -> a
forall a. AlphaColour a -> a
alphaChannel AlphaColour a
ac
    c :: Colour a
c = AlphaColour a -> Colour a
forall a. Fractional a => AlphaColour a -> Colour a
colourChannel AlphaColour a
ac

instance (Fractional a, Read a) => Read (AlphaColour a) where
  readsPrec :: Int -> ReadS (AlphaColour a)
readsPrec Int
d String
r = [(AlphaColour a
forall a. Num a => AlphaColour a
transparent,String
s)|(String
"transparent",String
s) <- String -> [(String, String)]
lex String
r]
               [(AlphaColour a, String)]
-> [(AlphaColour a, String)] -> [(AlphaColour a, String)]
forall a. [a] -> [a] -> [a]
++ Bool -> ReadS (AlphaColour a) -> ReadS (AlphaColour a)
forall a. Bool -> ReadS a -> ReadS a
readParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
infix_prec)
                  (\String
r -> [(Colour a
c Colour a -> a -> AlphaColour a
forall a. Num a => Colour a -> a -> AlphaColour a
`withOpacity` a
o,String
s)
                         |(Colour a
c,String
r0) <- Int -> ReadS (Colour a)
forall a. Read a => Int -> ReadS a
readsPrec (Int
infix_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) String
r
                         ,(String
"`",String
r1) <- String -> [(String, String)]
lex String
r0
                         ,(String
"withOpacity",String
r2) <- String -> [(String, String)]
lex String
r1
                         ,(String
"`",String
r3) <- String -> [(String, String)]
lex String
r2
                         ,(a
o,String
s)  <- Int -> ReadS a
forall a. Read a => Int -> ReadS a
readsPrec (Int
infix_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) String
r3]) String
r