Exercise: Images

发布时间 2023-08-05 23:44:40作者: 熋霸

练习说明里面讲得很清楚了,实现前一节中Image的三个接口即可,这个Exercise明白Go的接口实现即可完成。主要代码如下:

 1 type Image struct{}
 2 
 3 func (Image) ColorModel() color.Model {
 4     return color.RGBAModel
 5 }
 6 
 7 func (Image) Bounds() image.Rectangle {
 8     return image.Rect(0, 0, 256, 256)
 9 }
10 
11 func (Image) At(x, y int) color.Color {
12     return color.RGBA{uint8(x*y), uint8(x*y), 255, 255}
13 }

两个地方说明下:

  1. 之前那个Exercise是显示tour/pic下的这张图片,他的尺寸是256x256,所以这里Rect设置为256大小;
  2. 同样之前那个Exercise中有三个变换,这里选了其中一个。