I define a Pixel
data with different size like this.
data Pixel = RGB8 Word8 Word8 Word8
| RGBA8 Word8 Word8 Word8 Word8
| RGB16 Word16 Word16 Word16
| RGBA16 Word16 Word16 Word16 Word16
| Empty
deriving (Eq)
and Num
instance
instance Num Pixel where
x + Empty = x
Empty + x = x
(RGB8 ru gu bu) + (RGB8 rv gv bv) = RGB8 (ru + rv) (gu + gv) (bu + bv)
(RGBA8 ru gu bu au) + (RGBA8 rv gv bv av) = RGBA8 (ru + rv) (gu + gv) (bu + bv) (au + av)
(RGB16 ru gu bu) + (RGB16 rv gv bv) = RGB16 (ru + rv) (gu + gv) (bu + bv)
(RGBA16 ru gu bu au) + (RGBA16 rv gv bv av) = RGBA16 (ru + rv) (gu + gv) (bu + bv) (au + av)
x - Empty = x
Empty - x = x
(RGB8 ru gu bu) - (RGB8 rv gv bv) = RGB8 (ru - rv) (gu - gv) (bu - bv)
(RGB16 ru gu bu) - (RGB16 rv gv bv) = RGB16 (ru - rv) (gu - gv) (bu - bv)
(RGBA8 ru gu bu au) - (RGBA8 rv gv bv av) = RGBA8 (ru - rv) (gu - gv) (bu - bv) (au - av)
(RGBA16 ru gu bu au) - (RGBA16 rv gv bv av) = RGBA16 (ru - rv) (gu - gv) (bu - bv) (au - av)
_ * _ = error "Not support multiply"
abs = id
signum _ = error "Not support signum"
fromInteger _ = error "Not support fromInteger"
It's very ugly and bored. I think Haskell is a Elegant language, so can I have a better style to write this?