{-
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.
-}-- |An 'RGBSpace' is characterized by 'Chromaticity' for red, green, and-- blue, the 'Chromaticity' of the white point, and it's-- 'TransferFunction'.moduleData.Colour.RGBSpace(Colour-- *RGB Tuple,RGB(..),uncurryRGB,curryRGB-- *RGB Gamut,RGBGamut,mkRGBGamut,primaries,whitePoint,inGamut-- *RGB Space,TransferFunction(..),linearTransferFunction,powerTransferFunction,inverseTransferFunction,RGBSpace(),mkRGBSpace,gamut,transferFunction,linearRGBSpace,rgbUsingSpace,toRGBUsingSpace)whereimportData.MonoidimportData.Colour.CIE.ChromaticityimportData.Colour.MatriximportData.Colour.RGBimportData.Colour.SRGB.Linear-- |Returns 'True' if the given colour lies inside the given gamut.inGamut::(Orda,Fractionala)=>RGBGamut->Coloura->BoolinGamutgamutc=r&&g&&bwheretestx=0<=x&&x<=1RGBrgb=fmaptest(toRGBUsingGamutgamutc)rtf::(Fractionalb,Reala)=>[[a]]->[[b]]rtf=map(maprealToFrac)rgbUsingGamut::(Fractionala)=>RGBGamut->a->a->a->ColourargbUsingGamutgamutrgb=rgbr0g0b0wherematrix=rtf$matrixMult(xyz2rgbsRGBGamut)(rgb2xyzgamut)[r0,g0,b0]=multmatrix[r,g,b]toRGBUsingGamut::(Fractionala)=>RGBGamut->Coloura->RGBatoRGBUsingGamutgamutc=RGBrgbwhereRGBr0g0b0=toRGBcmatrix=rtf$matrixMult(xyz2rgbgamut)(rgb2xyzsRGBGamut)[r,g,b]=multmatrix[r0,g0,b0]-- |A 'transfer' function is a function that typically translates linear-- colour space coordinates into non-linear coordinates.-- The 'transferInverse' function reverses this by translating non-linear-- colour space coordinates into linear coordinates.-- It is required that---- > transfer . transferInverse === id === transferInverse . inverse---- (or that this law holds up to floating point rounding errors).---- We also require that 'transfer' is approximately @(**transferGamma)@-- (and hence 'transferInverse' is approximately-- @(**(recip transferGamma))@).-- The value 'transferGamma' is for informational purposes only, so there-- is no bound on how good this approximation needs to be.dataTransferFunctiona=TransferFunction{transfer::a->a,transferInverse::a->a,transferGamma::a}-- |This is the identity 'TransferFunction'.linearTransferFunction::(Numa)=>TransferFunctionalinearTransferFunction=TransferFunctionidid1-- |This is the @(**gamma)@ 'TransferFunction'.powerTransferFunction::(Floatinga)=>a->TransferFunctionapowerTransferFunctiongamma=TransferFunction(**gamma)(**(recipgamma))gamma-- |This reverses a 'TransferFunction'.inverseTransferFunction::(Fractionala)=>TransferFunctiona->TransferFunctionainverseTransferFunction(TransferFunctionforrevg)=TransferFunctionrevfor(recipg)instance(Numa)=>Monoid(TransferFunctiona)wheremempty=linearTransferFunction(TransferFunctionf0f1f)`mappend`(TransferFunctiong0g1g)=(TransferFunction(f0.g0)(g1.f1)(f*g))-- |An 'RGBSpace' is a colour coordinate system for colours laying-- 'inGamut' of 'gamut'.-- Linear coordinates are passed through a 'transferFunction' to-- produce non-linear 'RGB' values.dataRGBSpacea=RGBSpace{gamut::RGBGamut,transferFunction::TransferFunctiona}-- |An RGBSpace is specified by an 'RGBGamut' and a 'TransferFunction'.mkRGBSpace::RGBGamut->TransferFunctiona->RGBSpaceamkRGBSpace=RGBSpace-- |Produce a linear colour space from an 'RGBGamut'.linearRGBSpace::(Numa)=>RGBGamut->RGBSpacealinearRGBSpacegamut=RGBSpacegamutmempty-- |Create a 'Colour' from red, green, and blue coordinates given in a-- general 'RGBSpace'.rgbUsingSpace::(Fractionala)=>RGBSpacea->a->a->a->ColourargbUsingSpacespace=curryRGB(uncurryRGB(rgbUsingGamut(gamutspace)).fmaptinv)wheretinv=transferInverse(transferFunctionspace)-- |Return the coordinates of a given 'Colour' for a general 'RGBSpace'.toRGBUsingSpace::(Fractionala)=>RGBSpacea->Coloura->RGBatoRGBUsingSpacespacec=fmapt(toRGBUsingGamut(gamutspace)c)wheret=transfer(transferFunctionspace)