[Haskell-cafe] Review request for my encoding function

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Sat Jan 8 11:32:48 CET 2011


On 8 January 2011 19:58, C K Kashyap <ckkashyap at gmail.com> wrote:
>> The more "Haskellian" approach would be to use a dedicated datatype to
>> specify the number of bits, not to have a partial function on Int.
>> Possibly even encode the RGB triple such that it specifies the number
>> of bits rather than separating each value.
>
> Did you mean something like this?
>
> data BitsPerPixel = Bpp8 | Bpp16 | Bpp32
>
> encode :: Color -> BitsPerPixel -> Int-> Int-> Int-> Int-> Int-> Int
> -> ByteString
> encode (r,g,b) bitsPerPixel redMax greenMax blueMax redShift
> greenShift blueShift = runPut $ do
>        case bitsPerPixel of
>                Bpp8    -> putWord8 z8
>                Bpp16   -> putWord16be z16
>                Bpp32   -> putWord32be z32

Yes, that's a start.  However, unless you specifically  need the RGB
values to be separate, I think something along these lines would be
even better:

data Color = C8 Word8 Word8 Word8
           | C16 Word16 Word16 Word16
           | C32 Word32 Word32 Word32

Or: why not just use Russell O'Connor's colour [1] package?

[1]: http://hackage.haskell.org/package/colour

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
IvanMiljenovic.wordpress.com



More information about the Haskell-Cafe mailing list