在我的代码中,我得到给定坐标的像素颜色,然后检查该颜色是否与另一种颜色匹配。它工作得很好,现在我希望能够检查它是否在 10 种左右的颜色阴影或一定数量的阴影范围内匹配。我不知道该怎么做。这是代码:
Public Function GetPixelColor(ByVal x As Integer, ByVal y As Integer) As Color
Dim sz As New Size(1, 1)
Dim c As Color
Using bmp As New Bitmap(1, 1)
Using g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(New Point(x, y), Point.Empty, sz)
c = bmp.GetPixel(0, 0)
End Using
End Using
Return c
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim fb As Color = GetPixelColor(TextBox1.Text, TextBox2.Text)
If fb.ToArgb() = TextBox3.Text Then
MessageBox.Show("Rock on dude")
End If
End Sub
我不知道
你使用的语言,但听起来你需要计算颜色值之间差异的绝对值。
伪代码:
if( Abs(color1 - color2) > 10.0 )
// do something
我会创建一个这样的函数,并用两种颜色调用它:
Private Function CompareColors(ByVal Color1 As Color, ByVal Color2 As Color) As Boolean
If Color1.ToArgb = Color2.ToArgb Then
'perfect match
Return True
ElseIf Asc(Color1.R) > Asc(Color2.R) - 10 AndAlso Asc(Color1.R) < Asc(Color2.R) + 10 Then
' red is wrong
Return False
ElseIf Asc(Color1.G) > Asc(Color2.G) - 10 AndAlso Asc(Color1.G) < Asc(Color2.G) + 10 Then
' green is wrong
Return False
ElseIf Asc(Color1.B) > Asc(Color2.B) - 10 AndAlso Asc(Color1.B) < Asc(Color2.B) + 10 Then
' blue is wrong
Return False
ElseIf Asc(Color1.A) > Asc(Color2.A) - 10 AndAlso Asc(Color1.A) < Asc(Color2.A) + 10 Then
' alpha is wrong
Return False
Else
Return True
End If
End Function