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:
12
The above copyright notice and this permission notice shall be included in
13
all copies or substantial portions of the Software.
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
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
34
,mkRGBGamut, primaries, whitePoint
38
,linearTransferFunction, powerTransferFunction
39
,inverseTransferFunction
42
,mkRGBSpace ,gamut, transferFunction
50
import Data.Colour.CIE.Chromaticity
51
import Data.Colour.Matrix
52
import Data.Colour.RGB
53
import Data.Colour.SRGB.Linear
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
59
test x = 0 <= x && x <= 1
60
RGB r g b = fmap test (toRGBUsingGamut gamut c)
62
rtf :: (Fractional b, Real a) => [[a]] -> [[b]]
63
rtf = map (map realToFrac)
65
rgbUsingGamut :: (Fractional a) => RGBGamut -> a -> a -> a -> Colour a
66
rgbUsingGamut gamut r g b = rgb r0 g0 b0
68
matrix = rtf $ matrixMult (xyz2rgb sRGBGamut) (rgb2xyz gamut)
69
[r0,g0,b0] = mult matrix [r,g,b]
71
toRGBUsingGamut :: (Fractional a) => RGBGamut -> Colour a -> RGB a
72
toRGBUsingGamut gamut c = RGB r g b
74
RGB r0 g0 b0 = toRGB c
75
matrix = rtf $ matrixMult (xyz2rgb gamut) (rgb2xyz sRGBGamut)
76
[r,g,b] = mult matrix [r0,g0,b0]
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
84
-- > transfer . transferInverse === id === transferInverse . inverse
86
-- (or that this law holds up to floating point rounding errors).
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
95
, transferInverse :: a -> a
96
, transferGamma :: a }
98
-- |This is the identity 'TransferFunction'.
99
linearTransferFunction :: (Num a) => TransferFunction a
100
linearTransferFunction = TransferFunction id id 1
102
-- |This is the @(**gamma)@ 'TransferFunction'.
103
powerTransferFunction :: (Floating a) => a -> TransferFunction a
104
powerTransferFunction gamma =
105
TransferFunction (**gamma) (**(recip gamma)) gamma
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)
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))
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 }
124
-- |An RGBSpace is specified by an 'RGBGamut' and a 'TransferFunction'.
125
mkRGBSpace :: RGBGamut
126
-> TransferFunction a
128
mkRGBSpace = RGBSpace
130
-- |Produce a linear colour space from an 'RGBGamut'.
131
linearRGBSpace :: (Num a) => RGBGamut -> RGBSpace a
132
linearRGBSpace gamut = RGBSpace gamut mempty
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)
140
tinv = transferInverse (transferFunction space)
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)
146
t = transfer (transferFunction space)