{-
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.
-}-- |Specifies 'Colour's in accordance with the sRGB standard.moduleData.Colour.SRGB(Colour,RGB(..),sRGB24,sRGBBounded,sRGB,toSRGB24,toSRGBBounded,toSRGB,sRGB24shows,sRGB24show,sRGB24reads,sRGB24read,sRGBSpace)whereimportData.WordimportNumericimportData.Colour.Internal(quantize)importData.Colour.SRGB.LinearimportData.Colour.RGBSpacehiding(transferFunction){- Non-linear colour space -}{- the sRGB transfer function approximates a gamma of about 1/2.2 -}transferFunctionlin|lin==1=1|lin<=0.0031308=12.92*lin|otherwise=(1+a)*lin**(1/2.4)-awherea=0.055invTransferFunctionnonLin|nonLin==1=1|nonLin<=0.04045=nonLin/12.92|otherwise=((nonLin+a)/(1+a))**2.4wherea=0.055-- |Construct a colour from an sRGB specification.-- Input components are expected to be in the range [0..1].sRGB::(Ordb,Floatingb)=>b->b->b->ColourbsRGB=curryRGB(uncurryRGBrgb.fmapinvTransferFunction)-- |Construct a colour from an sRGB specification.-- Input components are expected to be in the range [0..'maxBound'].sRGBBounded::(Ordb,Floatingb,Integrala,Boundeda)=>a->a->a->ColourbsRGBBoundedr'g'b'=uncurryRGBsRGB(fmapf(RGBr'g'b'))wherefx'=(fromIntegralx'/m)m=fromIntegral$maxBound`asTypeOf`r'-- |Construct a colour from a 24-bit (three 8-bit words) sRGB-- specification.sRGB24::(Ordb,Floatingb)=>Word8->Word8->Word8->ColourbsRGB24=sRGBBounded-- |Return the sRGB colour components in the range [0..1].toSRGB::(Ordb,Floatingb)=>Colourb->RGBbtoSRGBc=fmaptransferFunction(toRGBc){- Results are clamped and quantized -}-- |Return the approximate sRGB colour components in the range-- [0..'maxBound'].-- Out of range values are clamped.toSRGBBounded::(RealFracb,Floatingb,Integrala,Boundeda)=>Colourb->RGBatoSRGBBoundedc=fmapf(toSRGBc)wherefx'=quantize(m*x')m=fromIntegral$maxBound`asTypeOf`(fundefined)-- |Return the approximate 24-bit sRGB colour components as three 8-bit-- components.-- Out of range values are clamped.toSRGB24::(RealFracb,Floatingb)=>Colourb->RGBWord8toSRGB24=toSRGBBounded-- |Show a colour in hexadecimal form, e.g. \"#00aaff\"sRGB24shows::(RealFracb,Floatingb)=>Colourb->ShowSsRGB24showsc=("#"++).showHex2r'.showHex2g'.showHex2b'whereRGBr'g'b'=toSRGB24cshowHex2x|x<=0xf=("0"++).showHexx|otherwise=showHexx-- |Show a colour in hexadecimal form, e.g. \"#00aaff\"sRGB24show::(RealFracb,Floatingb)=>Colourb->StringsRGB24showx=sRGB24showsx""-- |Read a colour in hexadecimal form, e.g. \"#00aaff\" or \"00aaff\"sRGB24reads::(Ordb,Floatingb)=>ReadS(Colourb)sRGB24reads""=[]sRGB24readsx=[(sRGB24abc,c0)|(a,a0)<-readPairx',(b,b0)<-readPaira0,(c,c0)<-readPairb0]wherex'|headx=='#'=tailx|otherwise=xreadPair[]=[]readPair[_]=[]readPaira=[(x,a1)|(x,"")<-readHexa0]where(a0,a1)=splitAt2a-- |Read a colour in hexadecimal form, e.g. \"#00aaff\" or \"00aaff\"sRGB24read::(Ordb,Floatingb)=>String->(Colourb)sRGB24readx|lengthrx/=1||not(null(snd(headrx)))=error"Data.Colour.SRGB.sRGB24read: no parse"|otherwise=fst(headrx)whererx=sRGB24readsx-- |The sRGB colour spacesRGBSpace::(Orda,Floatinga)=>RGBSpaceasRGBSpace=mkRGBSpacesRGBGamuttransferwheretransfer=TransferFunctiontransferFunctioninvTransferFunction(recip2.2)