-----------------------------------------------------------------------------
-- |
-- Module      :  Graphics.UI.SDL.Color
-- Copyright   :  (c) David Himmelstrup 2005
-- License     :  BSD-like
--
-- Maintainer  :  lemmih@gmail.com
-- Stability   :  provisional
-- Portability :  portable
--
-----------------------------------------------------------------------------
module Graphics.UI.SDL.Color
    (Color (..)
    ,Pixel (..)
    ) where

import Foreign (castPtr,pokeArray)
import Data.Word (Word8,Word32)
import Foreign.Storable (Storable(..))

data Color
    = Color
    { Color -> Word8
colorRed, Color -> Word8
colorGreen, Color -> Word8
colorBlue :: Word8 }

instance Storable Color where
    sizeOf :: Color -> Int
sizeOf = Int -> Color -> Int
forall a b. a -> b -> a
const 4
    alignment :: Color -> Int
alignment = Int -> Color -> Int
forall a b. a -> b -> a
const 1
    peek :: Ptr Color -> IO Color
peek ptr :: Ptr Color
ptr
        = do Word8
r <- Ptr Color -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Color
ptr 0
             Word8
g <- Ptr Color -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Color
ptr 1
             Word8
b <- Ptr Color -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr Color
ptr 2
             Color -> IO Color
forall (m :: * -> *) a. Monad m => a -> m a
return (Word8 -> Word8 -> Word8 -> Color
Color Word8
r Word8
g Word8
b)
    poke :: Ptr Color -> Color -> IO ()
poke ptr :: Ptr Color
ptr (Color r :: Word8
r g :: Word8
g b :: Word8
b) = Ptr Word8 -> [Word8] -> IO ()
forall a. Storable a => Ptr a -> [a] -> IO ()
pokeArray (Ptr Color -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr Color
ptr) [Word8
r,Word8
g,Word8
b,0]

newtype Pixel = Pixel Word32 deriving (Int -> Pixel -> ShowS
[Pixel] -> ShowS
Pixel -> String
(Int -> Pixel -> ShowS)
-> (Pixel -> String) -> ([Pixel] -> ShowS) -> Show Pixel
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Pixel] -> ShowS
$cshowList :: [Pixel] -> ShowS
show :: Pixel -> String
$cshow :: Pixel -> String
showsPrec :: Int -> Pixel -> ShowS
$cshowsPrec :: Int -> Pixel -> ShowS
Show,Pixel -> Pixel -> Bool
(Pixel -> Pixel -> Bool) -> (Pixel -> Pixel -> Bool) -> Eq Pixel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Pixel -> Pixel -> Bool
$c/= :: Pixel -> Pixel -> Bool
== :: Pixel -> Pixel -> Bool
$c== :: Pixel -> Pixel -> Bool
Eq,Eq Pixel
Eq Pixel =>
(Pixel -> Pixel -> Ordering)
-> (Pixel -> Pixel -> Bool)
-> (Pixel -> Pixel -> Bool)
-> (Pixel -> Pixel -> Bool)
-> (Pixel -> Pixel -> Bool)
-> (Pixel -> Pixel -> Pixel)
-> (Pixel -> Pixel -> Pixel)
-> Ord Pixel
Pixel -> Pixel -> Bool
Pixel -> Pixel -> Ordering
Pixel -> Pixel -> Pixel
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Pixel -> Pixel -> Pixel
$cmin :: Pixel -> Pixel -> Pixel
max :: Pixel -> Pixel -> Pixel
$cmax :: Pixel -> Pixel -> Pixel
>= :: Pixel -> Pixel -> Bool
$c>= :: Pixel -> Pixel -> Bool
> :: Pixel -> Pixel -> Bool
$c> :: Pixel -> Pixel -> Bool
<= :: Pixel -> Pixel -> Bool
$c<= :: Pixel -> Pixel -> Bool
< :: Pixel -> Pixel -> Bool
$c< :: Pixel -> Pixel -> Bool
compare :: Pixel -> Pixel -> Ordering
$ccompare :: Pixel -> Pixel -> Ordering
$cp1Ord :: Eq Pixel
Ord)

instance Storable Pixel where
    sizeOf :: Pixel -> Int
sizeOf    (Pixel v :: Word32
v) = Word32 -> Int
forall a. Storable a => a -> Int
sizeOf Word32
v
    alignment :: Pixel -> Int
alignment (Pixel v :: Word32
v) = Word32 -> Int
forall a. Storable a => a -> Int
alignment Word32
v
    peek :: Ptr Pixel -> IO Pixel
peek p :: Ptr Pixel
p
        = do Word32
v <- Ptr Word32 -> IO Word32
forall a. Storable a => Ptr a -> IO a
peek (Ptr Pixel -> Ptr Word32
forall a b. Ptr a -> Ptr b
castPtr Ptr Pixel
p)
             Pixel -> IO Pixel
forall (m :: * -> *) a. Monad m => a -> m a
return (Pixel -> IO Pixel) -> Pixel -> IO Pixel
forall a b. (a -> b) -> a -> b
$ Word32 -> Pixel
Pixel Word32
v
    poke :: Ptr Pixel -> Pixel -> IO ()
poke p :: Ptr Pixel
p (Pixel v :: Word32
v) = Ptr Word32 -> Word32 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr Pixel -> Ptr Word32
forall a b. Ptr a -> Ptr b
castPtr Ptr Pixel
p) Word32
v