Fork me on GitHub
打赏

Go指南练习_图像

https://tour.go-zh.org/methods/25

一、题目描述

还记得之前编写的图片生成器吗?我们再来编写另外一个,不过这次它将会返回一个 image.Image 的实现而非一个数据切片。

定义你自己的 Image 类型,实现必要的方法并调用 pic.ShowImage

Bounds 应当返回一个 image.Rectangle ,例如 image.Rect(0, 0, w, h)

ColorModel 应当返回 color.RGBAModel

At 应当返回一个颜色。上一个图片生成器的值 v 对应于此次的 color.RGBA{v, v, 255, 255}

 

二、题目分析

image 包定义了 Image 接口:

package image

type Image interface {
    ColorModel() color.Model  //颜色模式
    Bounds() Rectangle  //图片边界
    At(x, y int) color.Color  //图像上某个点的颜色
}

*注意:* Bounds 方法的返回值 Rectangle 实际上是一个 image.Rectangle,它在 image 包中声明。

了解官方的Image包的结构后,接下来就可以自己写方法。

 

三、Go代码

package main

import (
    "golang.org/x/tour/pic"
    "image/color"
    "image"
)

type Image struct{}  //新建一个Image结构体

func (i Image) ColorModel() color.Model{  //实现Image包中颜色模式的方法
    return color.RGBAModel
}

func (i Image) Bounds() image.Rectangle{  //实现Image包中生成图片边界的方法
    return image.Rect(0,0,200,200)
}

func (i Image) At(x,y int) color.Color{  //实现Image包中生成图像某个点的方法
    return color.RGBA{uint8(x),uint8(y),uint8(255),uint8(255)}
}

func main() {
    m := Image{}
    pic.ShowImage(m)  //调用
}

 

 

四、参考资料

https://linkscue.com/2018/02/28/go-tour-practice-image/

 

posted @ 2018-07-23 18:09  Zoctopus_Zhang  阅读(3196)  评论(0编辑  收藏  举报
// function btn_donateClick() { var DivPopup = document.getElementById('Div_popup'); var DivMasklayer = document.getElementById('div_masklayer'); DivMasklayer.style.display = 'block'; DivPopup.style.display = 'block'; var h = Div_popup.clientHeight; with (Div_popup.style) { marginTop = -h / 2 + 'px'; } } function MasklayerClick() { var masklayer = document.getElementById('div_masklayer'); var divImg = document.getElementById("Div_popup"); masklayer.style.display = "none"; divImg.style.display = "none"; } setTimeout( function () { document.getElementById('div_masklayer').onclick = MasklayerClick; document.getElementById('btn_donate').onclick = btn_donateClick; var a_gzw = document.getElementById("guanzhuwo"); a_gzw.href = "javascript:void(0);"; $("#guanzhuwo").attr("onclick","follow('33513f9f-ba13-e011-ac81-842b2b196315');"); }, 900);