英文:
How to set a color.Color value?
问题
我正在使用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
}
}
}
我的变量color的类型是color.Color
,但是我无论如何都无法弄清楚如何初始化一个具有该类型的变量。我需要将其与RGBA为{221, 223, 226, 255}
的颜色进行比较。
当我在任何像素上使用fmt.Print(color)
时,你会得到一个输出,类似于{178 180 181 255}
。我该如何设置一个color.Color
值来进行比较呢?
英文:
I'm creating a bot of sorts with Go that takes a screenshot of my desktop, searches for a specific color RGBA, and when it finds a match it performs an action. Assuming I already have the screenshot, my program looks like this:
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
}
}
}
The type of my variable color is color.Color
, but I can't for the life of me figure out how to initialize a variable with that type. I need to compare it to a color with RGBA {221, 223, 226, 255}
.
When I fmt.Print(color)
at whatever pixel you get an output like {178 180 181 255}
. How can I set a color.Color
value to compare the two?
答案1
得分: 1
color.Color 只是一个接口,定义了如何派生出规范化的 R、G、B 和 A 通道值。这意味着对于任何给定的 color.Color 值,它可以包含多种不同的数据类型,每种类型具有不同的内部结构。为了比较不同的 color.Color 值的相等性,理论上可以使用 ==
,但它只会寻找相同的实现,而不是等价的表示。请看下面的示例:
import "image/color"
...
func colorEqual(c1, c2 color.Color) bool {
r1, g1, b1, a1 := c1.RGBA()
r2, g2, b2, a2 := c2.RGBA()
return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2
}
...
c := img.At(x, y)
if colorEqual(c, color.RGBA{178, 180, 181, 255}) {
// 做一些操作
}
英文:
color.Color is only an interface that defines how to derive normalized R, G, B and A channel values. This means that for any given color.Color value, there are multiple different data types that it could contain, each having a different internal structure. In order to compare equality of different color.Color values, you can theoretically use ==
, but it only looks for identical implementation, not equivalent representation. See this example:
import "image/color"
...
func colorEqual(c1, c2 color.Color) bool {
r1, g1, b1, a1 := c1.RGBA()
r2, g2, b2, a2 := c2.RGBA()
return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2
}
...
c := img.At(x, y)
if colorEqual(c, color.RGBA{178, 180, 181, 255}) {
// Do something
}
答案2
得分: 0
Color
是一个接口。你应该使用它的 RGBA
方法进行比较。它返回 红色、绿色、蓝色和透明度 值 - 所以你可以根据需要将它们与你预设的值进行比较。
英文:
Color
is an interface. You should use its RGBA
method for the comparison. It returns the red, green, blue and alpha values - so compare them to your preset values as you wish.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论