Images with Haskell and GD
2008-12-03
So I'm in the middle of knocking up a website, and I need some rounded corner images, what better language to turn to than Haskell?
We're going to use the Graphics.GD bindings to the GD library written by Bjorn Bringert to do all the work:
> import Graphics.GD > import Numeric (showHex, readHex) > import System.Environment > > > main = do > [prefix, strSize,fg,bg] <- getArgs > let size = read strSize > img <- newImage (size,size) > doAt img fg bg size (prefix++"_tl.png") (size-1,size-1) > doAt img fg bg size (prefix++"_tr.png") (0,size-1) > doAt img fg bg size (prefix++"_bl.png") (size-1,0) > doAt img fg bg size (prefix++"_br.png") (0,0) > > doAt img fg bg size name pos = do > fillImage (torgb bg) img > drawFilledEllipse pos (size*2-1,size*2-1) (torgb fg) img > savePngFile name img > > torgb s = > let > [r,g,b] = fromHexStr s > in > rgb r g b > > > fromHexStr :: String -> [Int] > fromHexStr (a:b:tl) = (fromHex [a,b]):(fromHexStr tl) > fromHexStr [] = [] > fromHex :: String -> Int > fromHex s = > case readHex s of > [(val,"")] -> fromIntegral (val) > other -> error $! show other >
Usage is simple: "./makeCorners l 20 3D7AB8 FFFFFF" will make 4 images with quadrants of a circle in them, coloured #3D7AB8 on a white background. And that's it!
