1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
{-
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
import Data.Colour.Matrix
import Data.Colour.CIE.Chromaticity
import Control.Applicative
-- |An RGB triple for an unspecified colour space.
data RGB a = RGB {channelRed :: !a
,channelGreen :: !a
,channelBlue :: !a
} deriving (Eq, Show, Read)
instance Functor RGB where
fmap f (RGB r g b) = RGB (f r) (f g) (f b)
instance Applicative RGB where
pure c = RGB c c c
(RGB fr fg fb) <*> (RGB r g b) = RGB (fr r) (fg g) (fb b)
-- |Uncurries a function expecting three r, g, b parameters.
uncurryRGB :: (a -> a -> a -> b) -> RGB a -> b
uncurryRGB f (RGB r g b) = f r g b
-- |Curries a function expecting one RGB parameter.
curryRGB :: (RGB a -> b) -> a -> a -> a -> b
curryRGB f r g b = f (RGB r g b)
-- |An 'RGBGamut' is a 3-D colour “cube” that contains all the
-- colours that can be displayed by a RGB device.
-- The “cube” is normalized so that white has
-- 'Data.Colour.CIE.luminance' 1.
data RGBGamut = RGBGamut {primaries :: !(RGB (Chromaticity Rational))
,whitePoint :: !(Chromaticity Rational)
} deriving (Eq)
instance Show RGBGamut where
showsPrec d gamut = showParen (d > app_prec) showStr
where
showStr = showString "mkRGBGamut"
. showString " " . (showsPrec (app_prec+1) (primaries gamut))
. showString " " . (showsPrec (app_prec+1) (whitePoint gamut))
instance Read RGBGamut where
readsPrec d r = readParen (d > app_prec)
(\r -> [(mkRGBGamut p w,t)
|("mkRGBGamut",s) <- lex r
,(p,s0) <- readsPrec (app_prec+1) s
,(w,t) <- readsPrec (app_prec+1) s0]) 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 = RGBGamut
{- not for export -}
primaryMatrix :: (Fractional a) => (RGB (Chromaticity a)) -> [[a]]
primaryMatrix p =
[[xr, xg, xb]
,[yr, yg, yb]
,[zr, zg, zb]]
where
RGB (xr, yr, zr)
(xg, yg, zg)
(xb, yb, zb) = fmap chromaCoords p
rgb2xyz :: RGBGamut -> [[Rational]]
rgb2xyz space =
transpose (zipWith (map . (*)) as (transpose matrix))
where
(xn, yn, zn) = chromaCoords (whitePoint space)
matrix = primaryMatrix (primaries space)
as = mult (inverse matrix) [xn/yn, 1, zn/yn]
xyz2rgb :: RGBGamut -> [[Rational]]
xyz2rgb = inverse . rgb2xyz
hslsv :: (Fractional a, Ord a) => RGB a -> (a,a,a,a,a)
hslsv (RGB r g b) | mx == mn = (0,0,mx,0 ,mx)
| otherwise = (h,s,l ,s0,mx)
where
mx = maximum [r,g,b]
mn = minimum [r,g,b]
l = (mx+mn)/2
s | l <= 0.5 = (mx-mn)/(mx+mn)
| otherwise = (mx-mn)/(2-(mx+mn))
s0 = (mx-mn)/mx
-- hue calcuation
[x,y,z] = take 3 $ dropWhile (/=mx) [r,g,b,r,g]
Just o = elemIndex mx [r,g,b]
h0 = 60*(y-z)/(mx-mn) + 120*(fromIntegral o)
h | h0 < 0 = h0 + 360
| otherwise = 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 rgb = h
where
(h,_,_,_,_) = hslsv rgb
mod1 x | pf < 0 = pf+1
| otherwise = pf
where
(_,pf) = properFraction x
|