2012-11-22 11 views
-1

在我的代码中,我得到给定坐标的像素颜色,然后检查颜色是否与另一种颜色匹配。它很好用,现在我希望能够检查它是否在10色左右的颜色或一定数量的色调内匹配。我不知道如何做到这一点。下面是代码:试图获得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 
+0

阴影中混合有黑色的颜色,以降低亮度,这是你是什​​么意思? – Aesthete

+0

根本没有。我的意思是色彩的深浅。 – user1632018

+0

好吧,你说的是_hue_,而不是阴影。因为阴影是黑色的混合物,而颜色不变。 – Aesthete

回答

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 
+0

嘿,多数民众赞成在完美,真棒感谢。 – user1632018

+0

这个解决方案可能是一个瓶颈,因为它被称为*对单个像素*。最初的“完美搭配”测试只会降低颜色不同的情况下的情况。此外,您正在计算颜色容差的立方体,而不是更常见的球体。而且你计算每个颜色对的差值两次,而不是仅使用一次绝对值。 – Borodin

+0

鲍罗廷,你是对的,我不会在检查大量像素的东西中使用它。但它确实展示了Color对象的一些有用部分。与往常一样,如果您有更好的解决方案,请随时发布。在数学上这不是很好。 –

1

我不知道你正在使用的语言,但它听起来像是你需要计算的颜色值之间的差值的绝对值。

伪代码:

if(Abs(color1 - color2) > 10.0) 
    // do something 
+0

我正在使用vb.net – user1632018