~ubuntu-branches/ubuntu/oneiric/haskell-colour/oneiric

« back to all changes in this revision

Viewing changes to Data/Colour/RGBSpace.hs

  • Committer: Bazaar Package Importer
  • Author(s): Marco Túlio Gontijo e Silva
  • Date: 2010-05-30 16:02:29 UTC
  • Revision ID: james.westby@ubuntu.com-20100530160229-42f17ki2m313haz3
Tags: upstream-2.3.1
Import upstream version 2.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{-
 
2
Copyright (c) 2008
 
3
Russell O'Connor
 
4
 
 
5
Permission is hereby granted, free of charge, to any person obtaining a copy
 
6
of this software and associated documentation files (the "Software"), to deal
 
7
in the Software without restriction, including without limitation the rights
 
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
copies of the Software, and to permit persons to whom the Software is
 
10
furnished to do so, subject to the following conditions:
 
11
 
 
12
The above copyright notice and this permission notice shall be included in
 
13
all copies or substantial portions of the Software.
 
14
 
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
18
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
19
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
20
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
21
THE SOFTWARE.
 
22
-}
 
23
-- |An 'RGBSpace' is characterized by 'Chromaticity' for red, green, and
 
24
-- blue, the 'Chromaticity' of the white point, and it's
 
25
-- 'TransferFunction'.
 
26
module Data.Colour.RGBSpace
 
27
 (Colour
 
28
  -- *RGB Tuple
 
29
 ,RGB(..)
 
30
 ,uncurryRGB, curryRGB
 
31
 
 
32
 -- *RGB Gamut
 
33
 ,RGBGamut
 
34
 ,mkRGBGamut, primaries, whitePoint
 
35
 ,inGamut
 
36
 -- *RGB Space
 
37
 ,TransferFunction(..)
 
38
 ,linearTransferFunction, powerTransferFunction
 
39
 ,inverseTransferFunction
 
40
 
 
41
 ,RGBSpace()
 
42
 ,mkRGBSpace ,gamut, transferFunction
 
43
 ,linearRGBSpace
 
44
 ,rgbUsingSpace
 
45
 ,toRGBUsingSpace
 
46
 )
 
47
where
 
48
 
 
49
import Data.Monoid
 
50
import Data.Colour.CIE.Chromaticity
 
51
import Data.Colour.Matrix
 
52
import Data.Colour.RGB
 
53
import Data.Colour.SRGB.Linear
 
54
 
 
55
-- |Returns 'True' if the given colour lies inside the given gamut.
 
56
inGamut :: (Ord a, Fractional a) => RGBGamut -> Colour a -> Bool
 
57
inGamut gamut c = r && g && b
 
58
 where
 
59
  test x = 0 <= x && x <= 1
 
60
  RGB r g b = fmap test (toRGBUsingGamut gamut c)
 
61
 
 
62
rtf :: (Fractional b, Real a) => [[a]] -> [[b]]
 
63
rtf = map (map realToFrac)
 
64
 
 
65
rgbUsingGamut :: (Fractional a) => RGBGamut -> a -> a -> a -> Colour a
 
66
rgbUsingGamut gamut r g b = rgb r0 g0 b0
 
67
 where
 
68
  matrix = rtf $ matrixMult (xyz2rgb sRGBGamut) (rgb2xyz gamut)
 
69
  [r0,g0,b0] = mult matrix [r,g,b]
 
70
 
 
71
toRGBUsingGamut :: (Fractional a) => RGBGamut -> Colour a -> RGB a
 
72
toRGBUsingGamut gamut c = RGB r g b
 
73
 where
 
74
  RGB r0 g0 b0 = toRGB c
 
75
  matrix = rtf $ matrixMult (xyz2rgb gamut) (rgb2xyz sRGBGamut)
 
76
  [r,g,b] = mult matrix [r0,g0,b0]
 
77
 
 
78
-- |A 'transfer' function is a function that typically translates linear
 
79
-- colour space coordinates into non-linear coordinates.
 
80
-- The 'transferInverse' function reverses this by translating non-linear
 
81
-- colour space coordinates into linear coordinates.
 
82
-- It is required that
 
83
--
 
84
-- > transfer . transferInverse === id === transferInverse . inverse
 
85
--
 
86
-- (or that this law holds up to floating point rounding errors).
 
87
--
 
88
-- We also require that 'transfer' is approximately @(**transferGamma)@
 
89
-- (and hence 'transferInverse' is approximately
 
90
-- @(**(recip transferGamma))@).
 
91
-- The value 'transferGamma' is for informational purposes only, so there
 
92
-- is no bound on how good this approximation needs to be.
 
93
data TransferFunction a = TransferFunction
 
94
                          { transfer :: a -> a
 
95
                          , transferInverse :: a -> a
 
96
                          , transferGamma :: a }
 
97
 
 
98
-- |This is the identity 'TransferFunction'.
 
99
linearTransferFunction :: (Num a) => TransferFunction a
 
100
linearTransferFunction = TransferFunction id id 1
 
101
 
 
102
-- |This is the @(**gamma)@ 'TransferFunction'.
 
103
powerTransferFunction :: (Floating a) => a -> TransferFunction a
 
104
powerTransferFunction gamma =
 
105
  TransferFunction (**gamma) (**(recip gamma)) gamma
 
106
 
 
107
-- |This reverses a 'TransferFunction'.
 
108
inverseTransferFunction :: (Fractional a) => TransferFunction a -> TransferFunction a
 
109
inverseTransferFunction (TransferFunction for rev g) =
 
110
  TransferFunction rev for (recip g)
 
111
 
 
112
instance (Num a) => Monoid (TransferFunction a) where
 
113
 mempty = linearTransferFunction
 
114
 (TransferFunction f0 f1 f) `mappend` (TransferFunction g0 g1 g) =
 
115
   (TransferFunction (f0 . g0) (g1 . f1) (f*g))
 
116
 
 
117
-- |An 'RGBSpace' is a colour coordinate system for colours laying
 
118
-- 'inGamut' of 'gamut'.
 
119
-- Linear coordinates are passed through a 'transferFunction' to
 
120
-- produce non-linear 'RGB' values.
 
121
data RGBSpace a = RGBSpace { gamut :: RGBGamut,
 
122
                             transferFunction :: TransferFunction a }
 
123
 
 
124
-- |An RGBSpace is specified by an 'RGBGamut' and a 'TransferFunction'.
 
125
mkRGBSpace :: RGBGamut
 
126
           -> TransferFunction a
 
127
           -> RGBSpace a
 
128
mkRGBSpace = RGBSpace
 
129
 
 
130
-- |Produce a linear colour space from an 'RGBGamut'.
 
131
linearRGBSpace :: (Num a) => RGBGamut -> RGBSpace a
 
132
linearRGBSpace gamut = RGBSpace gamut mempty
 
133
 
 
134
-- |Create a 'Colour' from red, green, and blue coordinates given in a
 
135
-- general 'RGBSpace'.
 
136
rgbUsingSpace :: (Fractional a) => RGBSpace a -> a -> a -> a -> Colour a
 
137
rgbUsingSpace space = 
 
138
  curryRGB (uncurryRGB (rgbUsingGamut (gamut space)) . fmap tinv)
 
139
 where
 
140
  tinv = transferInverse (transferFunction space)
 
141
 
 
142
-- |Return the coordinates of a given 'Colour' for a general 'RGBSpace'.
 
143
toRGBUsingSpace :: (Fractional a) => RGBSpace a -> Colour a -> RGB a
 
144
toRGBUsingSpace space c = fmap t (toRGBUsingGamut (gamut space) c)
 
145
 where
 
146
  t = transfer (transferFunction space)