搜索特定的RGB值? [英] Search for a specific RGB value?

查看:58
本文介绍了搜索特定的RGB值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这个问题显而易见,我道歉 - 我是自学成才,不幸的是我不时遇到VB.NET的大量差距!



我有许多100的红色,绿色和蓝色值(每个0到255)具有相应的整数值。例如,CSV格式可以列出:



红色,绿色,蓝色,整数

125,54,99,5000

125,67,255,65

0,145,17,1256

54,65,99,11

............等等



我想做的是能够有效地搜索匹配的RGB值是否存在于这个清单。我想我需要创建一个接受R,G和B输入的函数,并输出列出的整数或类似'0'的条目,如果条目不存在。



我已经考虑过如何做到这一点,但我仍然不确定处理定义列表的最佳方法。



有人可以提供任何建议吗?



非常感谢!

I apologise if this question appears obvious - I am self taught and unfortunately from time to time run into big gaps in my knowledge of VB.NET!

I have many 100's of Red, Green and Blue values (0 to 255 each) that have a corresponding integer value. For example in CSV form they could be listed:

Red, Green, Blue, Integer
125, 54, 99, 5000
125, 67, 255, 65
0, 145, 17, 1256
54, 65, 99, 11
............ etc.

What I want to do is to be able to efficiently search whether the matching RGB values exist in this list. I guess I need to make a Function which accepts inputs of R, G and B and outputs the listed Integer or something like '0' if the entry does not exist.

I have thought about how to do this but am still unsure of the best way to handle the defined list.

Could anyone offer any suggestions please?

Many thanks!

推荐答案

我会这样做:



I would do something like that:

Public Class MyColor
    Public Property ID As Integer
    Public Property RGB As Color
End Class




Public Class Main

    Private _allMyColors As List(Of MyColor) = New List(Of MyColor)

    '... fill _allMyColors

    Public Function colorExists(a As Integer, g As Integer, b As Integer) As Integer
        Dim mc As MyColor = _allMyColors.FirstOrDefault(Function(c) c.RGB.A = a And c.RGB.G = g And c.RGB.B = b)
        If mc IsNot Nothing Then
            Return mc.ID
        Else
            Return 0
        End If
    End Function

End Class


虽然我没有测试过,但这应该有效,因为我没有CSV文件您的颜色。

您建立一个包含成员R,G,B和ID的类,确保它具有=和<>运营商。



然后你使用列表的功能为你做这种工作。



This should work though I haven't tested it since I don't have a CSV file of your colors.
You establish a class that contains members R, G, B and ID, ensuring it has = and <> operators.

Then you use the ability of Lists to do this kind of work for you.

Imports System.Math
Imports System.IO

Public Class Something
    Private ColorList As List(Of ExampleRGB) = New List(Of ExampleRGB)
    Private MatchedIDs As List(Of ExampleRGB) = New List(Of ExampleRGB)
    Private RGBConstraint As ExampleRGB

    Public Sub Main()
        ReadColors()
        SearchColors()

    End Sub

    Private Sub ReadColors()
        'Let's assume you have a CSV file with all you color objects
        Dim filename As String = "colors.csv"
        Dim StringColorArray() As String
        Dim ColorString As String

        'Load the CSV lines into an array, e.g. 10,20,30,100
        If File.Exists(filename) Then
            Dim tmpstream As StreamReader = File.OpenText(filename)

            'The Split method separates the individual lines into array members
            StringColorArray = tmpstream.ReadToEnd().Split({Environment.NewLine}, StringSplitOptions.None)

            Dim tmpExampleRGB As ExampleRGB

            'Pass each array member into the List object
            For Each ColorString In StringColorArray
                tmpExampleRGB = New ExampleRGB(ColorString)
                ColorList.Add(tmpExampleRGB)
            Next
        End If
    End Sub

    Private Sub SearchColors()
        'Establish the R, G and B values you are searching for
        RGBConstraint = New ExampleRGB("10,20,30,100")

        'Load the second List object with only those RGB values that have matching R, G and B elements

        'http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k%28System.Collections.Generic.List%601.FindAll%29;k%28TargetFrameworkMoniker-.NETFramework

        'The Predicate(Of T) is a delegate to a method that returns true if the object passed to it matches the conditions
        'defined in the delegate. The elements of the current List(Of T) are individually passed to the Predicate(Of T) delegate,
        'and the elements that match the conditions are saved in the returned List(Of T).

        'I.e. we tell thr FindAll method what function to use to get a true or false value for each member of the donor List
        'The FindAll method then sends each element of the donor list to this boolean fucntion and adds those which return true
        'to the target list
        MatchedIDs = ColorList.FindAll(AddressOf MatchIDs)
    End Sub

    Private Function MatchIDs(ByVal testExampleRGB As ExampleRGB) As Boolean
        'This function retuns True for all test RGB values that match exactly the R, G and B elements of the target
        'It is the Predicate(Of T) method discussed above
        If testExampleRGB = RGBConstraint Then
            Return True
        Else
            Return False
        End If
        
    End Function

End Class

Public Class ExampleRGB
    ' http://www.codeproject.com/Questions/650792/Search-for-a-specific-RGB-value

    Implements IEquatable(Of ExampleRGB)

    Private _R As Integer
    Private _G As Integer
    Private _B As Integer
    Private _id As Integer
    Private _colorString As String

    Sub New(ColorString As String)
        Dim items() As String
        items = ColorString.Split(New String() {","}, StringSplitOptions.None)
        Me.Red = Integer.Parse(items(0))
        Me.Green = Integer.Parse(items(1))
        Me.Blue = Integer.Parse(items(2))
        Me.ID = Integer.Parse(items(3))

    End Sub

    Sub New()
        Me.Red = 0
        Me.Green = 0
        Me.Blue = 0
        Me.ID = 0
    End Sub

    Public Property Red As Integer
        Get
            Return Me._R
        End Get
        Set(value As Integer)
            If Me._R <> value Then
                value =
                Me._R = value
            End If
        End Set
    End Property

    Public Property Green As Integer
        Get
            Return Me._G
        End Get
        Set(value As Integer)
            If Me._G <> value Then
                value = Min(value, 255)
                value = Max(0, value)
                Me._G = value
            End If
        End Set
    End Property

    Public Property Blue As Integer
        Get
            Return Me._B
        End Get
        Set(value As Integer)
            If Me._B <> value Then
                value = Min(value, 255)
                value = Max(0, value)
                Me._B = value
            End If
        End Set
    End Property

    Public Property ID As Integer
        Get
            Return Me._id
        End Get
        Set(value As Integer)
            If Me._id <> value Then
                'value = Min(value, 5000)
                'value = Max(0, value)
                Me._id = value
            End If
        End Set
    End Property

    Public Overloads Function Equals(ByVal testObject As ExampleRGB) As Boolean Implements IEquatable(Of MadBager.Scratch.ExampleRGB).Equals
        Return testObject = Me
    End Function

    Public Shared Operator =(ByVal object1 As ExampleRGB, ByVal object2 As ExampleRGB) As Boolean
        Return object1.Red = object2.Red And
        object1.Green = object2.Green And
        object1.Blue = object2.Blue
    End Operator

    Public Shared Operator <>(ByVal object1 As ExampleRGB, ByVal object2 As ExampleRGB) As Boolean
        Return Not object1 = object2
    End Operator

End Class


这篇关于搜索特定的RGB值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆