Review request for my encoding function

Hi, I've written a function to encode a color value of type (Int,Int,Int) into 8,16 or 32 byte ByteString depending on the value of bits per pixel. This is for my VNC server implementation. I'd appreciate some feedback on the Haskellism of the implementation. import Data.Bits import Data.ByteString.Lazy import Data.Binary.Put import Data.Word type Red = Int type Green = Int type Blue = Int type Color = (Red,Green,Blue) encode :: Color -> Int-> Int-> Int-> Int-> Int-> Int-> Int -> ByteString encode (r,g,b) bitsPerPixel redMax greenMax blueMax redShift greenShift blueShift = runPut $ do case bitsPerPixel of 8 -> putWord8 z8 16 -> putWord16be z16 32 -> putWord32be z32 where z8 = (fromIntegral $ nr + ng + nb) :: Word8 z16 = (fromIntegral $ nr + ng + nb) :: Word16 z32 = (fromIntegral $ nr + ng + nb) :: Word32 nr = scale r redMax redShift ng = scale g greenMax greenShift nb = scale b blueMax blueShift scale c cm cs = (c * cm `div` 255) `shift` cs Regards, Kashyap

On 8 January 2011 16:27, C K Kashyap
Hi, I've written a function to encode a color value of type (Int,Int,Int) into 8,16 or 32 byte ByteString depending on the value of bits per pixel. This is for my VNC server implementation. I'd appreciate some feedback on the Haskellism of the implementation.
import Data.Bits import Data.ByteString.Lazy import Data.Binary.Put import Data.Word
type Red = Int type Green = Int type Blue = Int type Color = (Red,Green,Blue)
encode :: Color -> Int-> Int-> Int-> Int-> Int-> Int-> Int -> ByteString encode (r,g,b) bitsPerPixel redMax greenMax blueMax redShift greenShift blueShift = runPut $ do case bitsPerPixel of 8 -> putWord8 z8 16 -> putWord16be z16 32 -> putWord32be z32 where z8 = (fromIntegral $ nr + ng + nb) :: Word8 z16 = (fromIntegral $ nr + ng + nb) :: Word16 z32 = (fromIntegral $ nr + ng + nb) :: Word32 nr = scale r redMax redShift ng = scale g greenMax greenShift nb = scale b blueMax blueShift scale c cm cs = (c * cm `div` 255) `shift` cs
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. -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com

Thanks Ivan,
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 Regards, Kashyap

On 8 January 2011 19:58, C K Kashyap
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@gmail.com IvanMiljenovic.wordpress.com

data Color = C8 Word8 Word8 Word8 | C16 Word16 Word16 Word16 | C32 Word32 Word32 Word32
Thank you very much Ivan ... I've been working on a VNC based platform independent graphics system for haskell. The idea is that one could write graphics logic in haskell that would get rendered into a bytestring that would get displayed via vnc. I have a partially working system here - https://github.com/ckkashyap/Chitra I'd love to get some feedback on that as well. Regards, Kashyap
participants (2)
-
C K Kashyap
-
Ivan Lazar Miljenovic