我正在尝试使用 Haskell 的 hip 库制作图像。当我在 ghci 中运行以下代码时,它工作正常:
let grad_color = makeImageR VU (200, 200) (\(i, j) -> PixelRGB (fromIntegral i) (fromIntegral j) (fromIntegral (i + j))) / 400
writeImage "images/grad_color.png" grad_color
现在我想隔离构建图像的函数。我尝试过(我的尝试有点随机,因为我迷路了)::
fun :: (Int, Int) -> Pixel RGB Word8
fun (i, j) = PixelRGB (fromIntegral i) / 200 * (fromIntegral j) / 200 (fromIntegral (i + j))) / 400
但是这个函数无法编译。最终目标是将各个部分组合在一起:
gradColor :: (Int, Int) -> ????
gradColor (m, n) = makeImageR VU (m, n) fun
我知道有很多错误,但我再次迷失了签名。
好吧,我设法制作了一个示例。这比 200/400 的除法更复杂 ^^ 抱歉我问题中的括号。
module TestHip
(colorFun
, myImage
, gradientColor
, saveImage
) where
import Graphics.Image
( makeImageR, writeImage, RGB, Image, Pixel(PixelRGB), VU(..) )
colorFun :: (Int, Int) -> Pixel RGB Double
colorFun (i, j) =
PixelRGB (fromIntegral i) (fromIntegral j) (fromIntegral (i + j)) / 400
myImage :: ((Int, Int) -> Pixel RGB Double) -> (Int, Int) -> Image VU RGB Double
myImage thefun (m, n) = makeImageR VU (m, n) thefun
gradientColor :: Image VU RGB Double
gradientColor = myImage colorFun (400, 400)
saveImage :: IO ()
saveImage = writeImage "images/mygradient.png" gradientColor
运行`saveImage',你会得到这个图像: