Skip to content

Commit

Permalink
Fix format decoding in 'renderToList' and output alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
nand authored and Brent Yorgey committed Nov 21, 2012
1 parent f40dd71 commit f89a434
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Diagrams/Backend/Cairo/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module Diagrams.Backend.Cairo.List where
import Control.Applicative ((<$>))
import Control.Exception (bracket)

import Data.Colour.SRGB (Colour, sRGB)
import Data.Colour
import Data.Colour.SRGB (sRGB)
import Data.Word (Word8)

import Diagrams.Prelude (Diagram, R2)
Expand All @@ -16,19 +17,19 @@ import Foreign.Marshal.Array (peekArray)
-- | Render to a regular list of Colour values.

renderToList :: (Ord a, Floating a) =>
Int -> Int -> Diagram Cairo R2 -> IO [[Colour a]]
Int -> Int -> Diagram Cairo R2 -> IO [[AlphaColour a]]
renderToList w h d =
f 0 <$> bracket (renderPtr w h d) free (peekArray $ w*h*4)
where
f :: (Ord a, Floating a) => Int -> [Word8] -> [[Colour a]]
f :: (Ord a, Floating a) => Int -> [Word8] -> [[AlphaColour a]]
f _ [] = []
f n xs | n >= w = [] : f 0 xs
f n (_:r:g:b:xs) = let c = sRGB (l r) (l g) (l b) in
case f (n+1) xs of
f n (g:b:r:a:xs) =
let l n = fromIntegral n / fromIntegral a
c = sRGB (l r) (l g) (l b) `withOpacity` (fromIntegral a / 255)

in case f (n+1) xs of
[] -> [[c]]
cs:ys -> (c:cs) : ys

f _ _ = error "renderToList: Internal format error"

l :: (Ord a, Floating a) => Word8 -> a
l n = fromIntegral n / fromIntegral (maxBound :: Word8)

0 comments on commit f89a434

Please sign in to comment.