-1

在我的代码中,我获取给定坐标的像素颜色,然后检查该颜色是否与另一种颜色匹配。它效果很好,现在我希望能够检查它是否在 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
4

2 回答 2

1

我不知道您使用的语言,但听起来您需要计算颜色值之间差异的绝对值。

伪代码:

if( Abs(color1 - color2) > 10.0 )
  // do something
于 2012-11-22T23:34:02.733 回答
0

我会创建一个这样的函数并用两种颜色调用它:

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
于 2012-11-22T23:43:39.327 回答