如何设置颜色。颜色值?

我正在用Go创建一个机器人,它截取我的桌面的屏幕截图,搜索特定颜色的RGBA,当它找到匹配项时,它会执行一个操作。


image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)

file, _ := os.Open("screenshot.png")

defer file.Close()


img, _, _ := image.Decode(file)


bounds := img.Bounds()

width := bounds.Max.X

height := bounds.Max.Y


for y := 0; y < height; y++ {

    for x := 0; x < width; x++ {

        color := img.At(x, y)


        if color == ? {

            // Do something

        }

    }

}

我的变量颜色的类型是,但是我无法弄清楚如何使用该类型初始化变量。我需要将其与RGBA的颜色进行比较。color.Color{221, 223, 226, 255}


当我在任何像素上你得到一个输出,如.如何设置一个值来比较两者?fmt.Print(color){178 180 181 255}color.Color


阿晨1998
浏览 149回答 2
2回答

繁花不似锦

颜色。颜色只是定义如何派生归一化 R、G、B 和 A 通道值的接口。这意味着对于任何给定的颜色。颜色值,它可以包含多个不同的数据类型,每个数据类型具有不同的内部结构。为了比较不同颜色的相等性。颜色值,理论上可以使用 ,但它只查找相同的实现,而不是等效的表示。请参阅此示例:==import "image/color"...func colorEqual(c1, c2 color.Color) bool {&nbsp; &nbsp; r1, g1, b1, a1 := c1.RGBA()&nbsp; &nbsp; r2, g2, b2, a2 := c2.RGBA()&nbsp; &nbsp; return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2}...&nbsp; &nbsp; &nbsp; &nbsp; c := img.At(x, y)&nbsp; &nbsp; &nbsp; &nbsp; if colorEqual(c, color.RGBA{178, 180, 181, 255}) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Do something&nbsp; &nbsp; &nbsp; &nbsp; }

潇湘沐

Color是一个接口。您应该使用其方法进行比较。它返回红色,绿色,蓝色和alpha值 - 因此请根据需要将它们与预设值进行比较。RGBA
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go