{-
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.
-}
module Data.Colour.CIE.Chromaticity where

data Chromaticity a = Chroma !a !a deriving (Chromaticity a -> Chromaticity a -> Bool
(Chromaticity a -> Chromaticity a -> Bool)
-> (Chromaticity a -> Chromaticity a -> Bool)
-> Eq (Chromaticity a)
forall a. Eq a => Chromaticity a -> Chromaticity a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Chromaticity a -> Chromaticity a -> Bool
== :: Chromaticity a -> Chromaticity a -> Bool
$c/= :: forall a. Eq a => Chromaticity a -> Chromaticity a -> Bool
/= :: Chromaticity a -> Chromaticity a -> Bool
Eq)

-- |Constructs 'Chromaticity' from the CIE little /x/, little /y/
-- coordinates for the 2° standard (colourimetric) observer.
mkChromaticity :: (Fractional a) => a -> a -> Chromaticity a
mkChromaticity :: forall a. Fractional a => a -> a -> Chromaticity a
mkChromaticity = a -> a -> Chromaticity a
forall a. a -> a -> Chromaticity a
Chroma

-- |Returns the CIE little /x/, little /y/, little /z/ coordinates
-- for the 2° standard (colourimetric) observer.
chromaCoords :: (Fractional a) => Chromaticity a -> (a, a, a)
chromaCoords :: forall a. Fractional a => Chromaticity a -> (a, a, a)
chromaCoords (Chroma a
x a
y) = (a
x, a
y, a
1 a -> a -> a
forall a. Num a => a -> a -> a
- a
x a -> a -> a
forall a. Num a => a -> a -> a
- a
y)

-- |Returns the CIE little /x/ coordinate
-- for the 2° standard (colourimetric) observer.
chromaX :: (Fractional a) => Chromaticity a -> a
chromaX :: forall a. Fractional a => Chromaticity a -> a
chromaX (Chroma a
x a
_y) = a
x

-- |Returns the CIE little /y/ coordinate
-- for the 2° standard (colourimetric) observer.
chromaY :: (Fractional a) => Chromaticity a -> a
chromaY :: forall a. Fractional a => Chromaticity a -> a
chromaY (Chroma a
_x a
y) = a
y

-- |Returns the CIE little /z/ coordinate
-- for the 2° standard (colourimetric) observer.
chromaZ :: (Fractional a) => Chromaticity a -> a
chromaZ :: forall a. Fractional a => Chromaticity a -> a
chromaZ (Chroma a
x a
y) = a
1 a -> a -> a
forall a. Num a => a -> a -> a
- a
x a -> a -> a
forall a. Num a => a -> a -> a
- a
y

-- |Change the type used to represent the chromaticity coordinates.
chromaConvert :: (Fractional b, Real a) => Chromaticity a -> Chromaticity b
chromaConvert :: forall b a.
(Fractional b, Real a) =>
Chromaticity a -> Chromaticity b
chromaConvert (Chroma a
x a
y) = b -> b -> Chromaticity b
forall a. a -> a -> Chromaticity a
Chroma (a -> b
forall a b. (Real a, Fractional b) => a -> b
realToFrac a
x) (a -> b
forall a b. (Real a, Fractional b) => a -> b
realToFrac a
y)

instance (Fractional a, Show a) => Show (Chromaticity a) where
  showsPrec :: Int -> Chromaticity a -> ShowS
showsPrec Int
d Chromaticity 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
"mkChromaticity " 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
x)
            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
y)
    (a
x,a
y,a
z) = Chromaticity a -> (a, a, a)
forall a. Fractional a => Chromaticity a -> (a, a, a)
chromaCoords Chromaticity a
c

instance (Fractional a, Read a) => Read (Chromaticity a) where
  readsPrec :: Int -> ReadS (Chromaticity a)
readsPrec Int
d String
r = Bool -> ReadS (Chromaticity a) -> ReadS (Chromaticity 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 -> Chromaticity a
forall a. Fractional a => a -> a -> Chromaticity a
mkChromaticity a
x a
y,String
t)
                         |(String
"mkChromaticity",String
s) <- ReadS String
lex String
r
                         ,(a
x,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
y,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
s0]) String
r

--------------------------------------------------------------------------
-- not for export
--------------------------------------------------------------------------

app_prec :: Int
app_prec = Int
10

infix_prec :: Int
infix_prec = Int
9 Int -> Int -> Int
forall a. a -> a -> a
`asTypeOf` Int
app_prec