module Graphics.Image.ColorSpace.CMYK (
CMYK(..), CMYKA(..), Pixel(..),
ToCMYK(..), ToCMYKA(..)
) where
import Prelude hiding (map)
import Graphics.Image.Interface
import Data.Typeable (Typeable)
import qualified Data.Monoid as M (mappend)
import qualified Data.Colour as C
import qualified Data.Colour.Names as C
data CMYK = CyanCMYK
| MagCMYK
| YelCMYK
| KeyCMYK
deriving (Eq, Enum, Typeable)
data CMYKA = CyanCMYKA
| MagCMYKA
| YelCMYKA
| KeyCMYKA
| AlphaCMYKA
deriving (Eq, Enum, Typeable)
class ColorSpace cs => ToCMYK cs where
toPixelCMYK :: Pixel cs Double -> Pixel CMYK Double
toImageCMYK :: (Array arr cs Double, Array arr CMYK Double) =>
Image arr cs Double
-> Image arr CMYK Double
toImageCMYK = map toPixelCMYK
class (ToCMYK (Opaque cs), Alpha cs) => ToCMYKA cs where
toPixelCMYKA :: Pixel cs Double -> Pixel CMYKA Double
toPixelCMYKA px = addAlpha (getAlpha px) (toPixelCMYK (dropAlpha px))
toImageCMYKA :: (Array arr cs Double, Array arr CMYKA Double) =>
Image arr cs Double
-> Image arr CMYKA Double
toImageCMYKA = map toPixelCMYKA
instance ColorSpace CMYK where
type PixelElt CMYK e = (e, e, e, e)
data Pixel CMYK e = PixelCMYK !e !e !e !e deriving Eq
fromChannel !e = PixelCMYK e e e e
fromElt !(c, m, y, k) = PixelCMYK c m y k
toElt (PixelCMYK c m y k) = (c, m, y, k)
getPxCh (PixelCMYK c _ _ _) CyanCMYK = c
getPxCh (PixelCMYK _ m _ _) MagCMYK = m
getPxCh (PixelCMYK _ _ y _) YelCMYK = y
getPxCh (PixelCMYK _ _ _ k) KeyCMYK = k
chOp !f (PixelCMYK c m y k) =
PixelCMYK (f CyanCMYK c) (f MagCMYK m) (f YelCMYK y) (f KeyCMYK k)
pxOp !f (PixelCMYK c m y k) = PixelCMYK (f c) (f m) (f y) (f k)
chApp (PixelCMYK fc fm fy fk) (PixelCMYK c m y k) = PixelCMYK (fc c) (fm m) (fy y) (fk k)
pxFoldMap f (PixelCMYK c m y k) = f c `M.mappend` f m `M.mappend` f y `M.mappend` f k
csColour CyanCMYK = C.opaque C.cyan
csColour MagCMYK = C.opaque C.magenta
csColour YelCMYK = C.opaque C.yellow
csColour KeyCMYK = C.opaque C.black
instance ColorSpace CMYKA where
type PixelElt CMYKA e = (e, e, e, e, e)
data Pixel CMYKA e = PixelCMYKA !e !e !e !e !e deriving Eq
fromChannel !e = PixelCMYKA e e e e e
fromElt (c, m, y, k, a) = PixelCMYKA c m y k a
toElt (PixelCMYKA c m y k a) = (c, m, y, k, a)
getPxCh (PixelCMYKA c _ _ _ _) CyanCMYKA = c
getPxCh (PixelCMYKA _ m _ _ _) MagCMYKA = m
getPxCh (PixelCMYKA _ _ y _ _) YelCMYKA = y
getPxCh (PixelCMYKA _ _ _ k _) KeyCMYKA = k
getPxCh (PixelCMYKA _ _ _ _ a) AlphaCMYKA = a
chOp !f (PixelCMYKA c m y k a) =
PixelCMYKA (f CyanCMYKA c) (f MagCMYKA m) (f YelCMYKA y) (f KeyCMYKA k) (f AlphaCMYKA a)
pxOp !f (PixelCMYKA c m y k a) = PixelCMYKA (f c) (f m) (f y) (f k) (f a)
chApp (PixelCMYKA fc fm fy fk fa) (PixelCMYKA c m y k a) =
PixelCMYKA (fc c) (fm m) (fy y) (fk k) (fa a)
pxFoldMap f (PixelCMYKA c m y k a) =
f c `M.mappend` f m `M.mappend` f y `M.mappend` f k `M.mappend` f a
csColour AlphaCMYKA = C.opaque C.grey
csColour ch = csColour $ opaque ch
instance Alpha CMYKA where
type Opaque CMYKA = CMYK
getAlpha (PixelCMYKA _ _ _ _ a) = a
addAlpha !a (PixelCMYK c m y k) = PixelCMYKA c m y k a
dropAlpha (PixelCMYKA c m y k _) = PixelCMYK c m y k
opaque CyanCMYKA = CyanCMYK
opaque MagCMYKA = MagCMYK
opaque YelCMYKA = YelCMYK
opaque KeyCMYKA = KeyCMYK
opaque AlphaCMYKA = error "Data.Image.ColorSpace.CMYK (Alpha.opaque)"
instance Show CMYK where
show CyanCMYK = "Cyan"
show MagCMYK = "Magenta"
show YelCMYK = "Yellow"
show KeyCMYK = "Black"
instance Show CMYKA where
show AlphaCMYKA = "Alpha"
show ch = show $ opaque ch
instance Show e => Show (Pixel CMYK e) where
show (PixelCMYK c m y k) = "<CMYK:("++show c++"|"++show m++"|"++show y++"|"++show k++")>"
instance Show e => Show (Pixel CMYKA e) where
show (PixelCMYKA c m y k a) =
"<CMYKA:("++show c++"|"++show m++"|"++show y++"|"++show k++"|"++show a++")>"