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

import Data.List (elemIndex, transpose)
import Data.Colour.Matrix
import Data.Colour.CIE.Chromaticity

-- |An RGB triple for an unspecified colour space.
data RGB a = RGB {forall a. RGB a -> a
channelRed :: !a
                 ,forall a. RGB a -> a
channelGreen :: !a
                 ,forall a. RGB a -> a
channelBlue :: !a
                 } deriving (RGB a -> RGB a -> Bool
(RGB a -> RGB a -> Bool) -> (RGB a -> RGB a -> Bool) -> Eq (RGB a)
forall a. Eq a => RGB a -> RGB a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => RGB a -> RGB a -> Bool
== :: RGB a -> RGB a -> Bool
$c/= :: forall a. Eq a => RGB a -> RGB a -> Bool
/= :: RGB a -> RGB a -> Bool
Eq, Int -> RGB a -> ShowS
[RGB a] -> ShowS
RGB a -> String
(Int -> RGB a -> ShowS)
-> (RGB a -> String) -> ([RGB a] -> ShowS) -> Show (RGB a)
forall a. Show a => Int -> RGB a -> ShowS
forall a. Show a => [RGB a] -> ShowS
forall a. Show a => RGB a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> RGB a -> ShowS
showsPrec :: Int -> RGB a -> ShowS
$cshow :: forall a. Show a => RGB a -> String
show :: RGB a -> String
$cshowList :: forall a. Show a => [RGB a] -> ShowS
showList :: [RGB a] -> ShowS
Show, ReadPrec [RGB a]
ReadPrec (RGB a)
Int -> ReadS (RGB a)
ReadS [RGB a]
(Int -> ReadS (RGB a))
-> ReadS [RGB a]
-> ReadPrec (RGB a)
-> ReadPrec [RGB a]
-> Read (RGB a)
forall a. Read a => ReadPrec [RGB a]
forall a. Read a => ReadPrec (RGB a)
forall a. Read a => Int -> ReadS (RGB a)
forall a. Read a => ReadS [RGB a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (RGB a)
readsPrec :: Int -> ReadS (RGB a)
$creadList :: forall a. Read a => ReadS [RGB a]
readList :: ReadS [RGB a]
$creadPrec :: forall a. Read a => ReadPrec (RGB a)
readPrec :: ReadPrec (RGB a)
$creadListPrec :: forall a. Read a => ReadPrec [RGB a]
readListPrec :: ReadPrec [RGB a]
Read)

instance Functor RGB where
 fmap :: forall a b. (a -> b) -> RGB a -> RGB b
fmap a -> b
f (RGB a
r a
g a
b) = b -> b -> b -> RGB b
forall a. a -> a -> a -> RGB a
RGB (a -> b
f a
r) (a -> b
f a
g) (a -> b
f a
b)

instance Applicative RGB where
 pure :: forall a. a -> RGB a
pure a
c = a -> a -> a -> RGB a
forall a. a -> a -> a -> RGB a
RGB a
c a
c a
c
 (RGB a -> b
fr a -> b
fg a -> b
fb) <*> :: forall a b. RGB (a -> b) -> RGB a -> RGB b
<*> (RGB a
r a
g a
b) = b -> b -> b -> RGB b
forall a. a -> a -> a -> RGB a
RGB (a -> b
fr a
r) (a -> b
fg a
g) (a -> b
fb a
b)

-- |Uncurries a function expecting three r, g, b parameters.
uncurryRGB :: (a -> a -> a -> b) -> RGB a -> b
uncurryRGB :: forall a b. (a -> a -> a -> b) -> RGB a -> b
uncurryRGB a -> a -> a -> b
f (RGB a
r a
g a
b) = a -> a -> a -> b
f a
r a
g a
b

-- |Curries a function expecting one RGB parameter.
curryRGB :: (RGB a -> b) -> a -> a -> a -> b
curryRGB :: forall a b. (RGB a -> b) -> a -> a -> a -> b
curryRGB RGB a -> b
f a
r a
g a
b = RGB a -> b
f (a -> a -> a -> RGB a
forall a. a -> a -> a -> RGB a
RGB a
r a
g a
b)

-- |An 'RGBGamut' is a 3-D colour &#8220;cube&#8221; that contains all the
-- colours that can be displayed by a RGB device.
-- The &#8220;cube&#8221; is normalized so that white has
-- 'Data.Colour.CIE.luminance' 1.
data RGBGamut = RGBGamut {RGBGamut -> RGB (Chromaticity Rational)
primaries :: !(RGB (Chromaticity Rational))
                         ,RGBGamut -> Chromaticity Rational
whitePoint :: !(Chromaticity Rational)
                         } deriving (RGBGamut -> RGBGamut -> Bool
(RGBGamut -> RGBGamut -> Bool)
-> (RGBGamut -> RGBGamut -> Bool) -> Eq RGBGamut
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RGBGamut -> RGBGamut -> Bool
== :: RGBGamut -> RGBGamut -> Bool
$c/= :: RGBGamut -> RGBGamut -> Bool
/= :: RGBGamut -> RGBGamut -> Bool
Eq)

instance Show RGBGamut where
  showsPrec :: Int -> RGBGamut -> ShowS
showsPrec Int
d RGBGamut
gamut = 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
"mkRGBGamut"
            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 -> RGB (Chromaticity Rational) -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec (Int
app_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (RGBGamut -> RGB (Chromaticity Rational)
primaries RGBGamut
gamut))
            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 -> Chromaticity Rational -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec (Int
app_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (RGBGamut -> Chromaticity Rational
whitePoint RGBGamut
gamut))

instance Read RGBGamut where
  readsPrec :: Int -> ReadS RGBGamut
readsPrec Int
d String
r = Bool -> ReadS RGBGamut -> ReadS RGBGamut
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 -> [(RGB (Chromaticity Rational) -> Chromaticity Rational -> RGBGamut
mkRGBGamut RGB (Chromaticity Rational)
p Chromaticity Rational
w,String
t)
                         |(String
"mkRGBGamut",String
s) <- ReadS String
lex String
r
                         ,(RGB (Chromaticity Rational)
p,String
s0) <- Int -> ReadS (RGB (Chromaticity Rational))
forall a. Read a => Int -> ReadS a
readsPrec (Int
app_precInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) String
s
                         ,(Chromaticity Rational
w,String
t)  <- Int -> ReadS (Chromaticity Rational)
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

-- |An RGB gamut is specified by three primary colours (red, green, and
-- blue) and a white point (often 'Data.Colour.CIE.Illuminant.d65').
mkRGBGamut :: RGB (Chromaticity Rational) -- ^ The three primaries
           -> Chromaticity Rational       -- ^ The white point
           -> RGBGamut
mkRGBGamut :: RGB (Chromaticity Rational) -> Chromaticity Rational -> RGBGamut
mkRGBGamut = RGB (Chromaticity Rational) -> Chromaticity Rational -> RGBGamut
RGBGamut

{- not for export -}

primaryMatrix :: (Fractional a) => (RGB (Chromaticity a)) -> [[a]]
primaryMatrix :: forall a. Fractional a => RGB (Chromaticity a) -> [[a]]
primaryMatrix RGB (Chromaticity a)
p =
  [[a
xr, a
xg, a
xb]
  ,[a
yr, a
yg, a
yb]
  ,[a
zr, a
zg, a
zb]]
 where
  RGB (a
xr, a
yr, a
zr)
      (a
xg, a
yg, a
zg)
      (a
xb, a
yb, a
zb) = (Chromaticity a -> (a, a, a))
-> RGB (Chromaticity a) -> RGB (a, a, a)
forall a b. (a -> b) -> RGB a -> RGB b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Chromaticity a -> (a, a, a)
forall a. Fractional a => Chromaticity a -> (a, a, a)
chromaCoords RGB (Chromaticity a)
p

rgb2xyz :: RGBGamut -> [[Rational]]
rgb2xyz :: RGBGamut -> [[Rational]]
rgb2xyz RGBGamut
space =
  [[Rational]] -> [[Rational]]
forall a. [[a]] -> [[a]]
transpose ((Rational -> [Rational] -> [Rational])
-> [Rational] -> [[Rational]] -> [[Rational]]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith ((Rational -> Rational) -> [Rational] -> [Rational]
forall a b. (a -> b) -> [a] -> [b]
map ((Rational -> Rational) -> [Rational] -> [Rational])
-> (Rational -> Rational -> Rational)
-> Rational
-> [Rational]
-> [Rational]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
(*)) [Rational]
as ([[Rational]] -> [[Rational]]
forall a. [[a]] -> [[a]]
transpose [[Rational]]
matrix))
 where
  (Rational
xn, Rational
yn, Rational
zn) = Chromaticity Rational -> (Rational, Rational, Rational)
forall a. Fractional a => Chromaticity a -> (a, a, a)
chromaCoords (RGBGamut -> Chromaticity Rational
whitePoint RGBGamut
space)
  matrix :: [[Rational]]
matrix = RGB (Chromaticity Rational) -> [[Rational]]
forall a. Fractional a => RGB (Chromaticity a) -> [[a]]
primaryMatrix (RGBGamut -> RGB (Chromaticity Rational)
primaries RGBGamut
space)
  as :: [Rational]
as = [[Rational]] -> [Rational] -> [Rational]
forall {b}. Num b => [[b]] -> [b] -> [b]
mult ([[Rational]] -> [[Rational]]
forall {a}. Fractional a => [[a]] -> [[a]]
inverse [[Rational]]
matrix) [Rational
xnRational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/Rational
yn, Rational
1, Rational
znRational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/Rational
yn]

xyz2rgb :: RGBGamut -> [[Rational]]
xyz2rgb :: RGBGamut -> [[Rational]]
xyz2rgb = [[Rational]] -> [[Rational]]
forall {a}. Fractional a => [[a]] -> [[a]]
inverse ([[Rational]] -> [[Rational]])
-> (RGBGamut -> [[Rational]]) -> RGBGamut -> [[Rational]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RGBGamut -> [[Rational]]
rgb2xyz

hslsv :: (Fractional a, Ord a) => RGB a -> (a,a,a,a,a)
hslsv :: forall a. (Fractional a, Ord a) => RGB a -> (a, a, a, a, a)
hslsv (RGB a
r a
g a
b) | a
mx a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
mn  = (a
0,a
0,a
mx,a
0 ,a
mx)
                  | Bool
otherwise = (a
h,a
s,a
l ,a
s0,a
mx)
 where
  mx :: a
mx = [a] -> a
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [a
r,a
g,a
b]
  mn :: a
mn = [a] -> a
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [a
r,a
g,a
b]
  l :: a
l = (a
mxa -> a -> a
forall a. Num a => a -> a -> a
+a
mn)a -> a -> a
forall a. Fractional a => a -> a -> a
/a
2
  s :: a
s | a
l a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0.5 = (a
mxa -> a -> a
forall a. Num a => a -> a -> a
-a
mn)a -> a -> a
forall a. Fractional a => a -> a -> a
/(a
mxa -> a -> a
forall a. Num a => a -> a -> a
+a
mn)
    | Bool
otherwise = (a
mxa -> a -> a
forall a. Num a => a -> a -> a
-a
mn)a -> a -> a
forall a. Fractional a => a -> a -> a
/(a
2a -> a -> a
forall a. Num a => a -> a -> a
-(a
mxa -> a -> a
forall a. Num a => a -> a -> a
+a
mn))
  s0 :: a
s0 = (a
mxa -> a -> a
forall a. Num a => a -> a -> a
-a
mn)a -> a -> a
forall a. Fractional a => a -> a -> a
/a
mx
  -- hue calcuation
  [a
x,a
y,a
z] = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
3 ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/=a
mx) [a
r,a
g,a
b,a
r,a
g]
  Just Int
o = a -> [a] -> Maybe Int
forall a. Eq a => a -> [a] -> Maybe Int
elemIndex a
mx [a
r,a
g,a
b]
  h0 :: a
h0 = a
60a -> a -> a
forall a. Num a => a -> a -> a
*(a
ya -> a -> a
forall a. Num a => a -> a -> a
-a
z)a -> a -> a
forall a. Fractional a => a -> a -> a
/(a
mxa -> a -> a
forall a. Num a => a -> a -> a
-a
mn) a -> a -> a
forall a. Num a => a -> a -> a
+ a
120a -> a -> a
forall a. Num a => a -> a -> a
*(Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
o)
  h :: a
h | a
h0 a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = a
h0 a -> a -> a
forall a. Num a => a -> a -> a
+ a
360
    | Bool
otherwise = a
h0

-- |The 'hue' coordinate of an 'RGB' value is in degrees. Its value is
-- always in the range 0-360.
hue :: (Fractional a, Ord a) => RGB a -> a
hue :: forall a. (Fractional a, Ord a) => RGB a -> a
hue RGB a
rgb = a
h
 where
  (a
h,a
_,a
_,a
_,a
_) = RGB a -> (a, a, a, a, a)
forall a. (Fractional a, Ord a) => RGB a -> (a, a, a, a, a)
hslsv RGB a
rgb

mod1 :: a -> a
mod1 a
x | a
pf a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 = a
pfa -> a -> a
forall a. Num a => a -> a -> a
+a
1
       | Bool
otherwise = a
pf
 where
  (Integer
_,a
pf) = a -> (Integer, a)
forall b. Integral b => a -> (b, a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction a
x