Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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!
Posted

I would do something like that:

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

VB
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
 
Share this answer
 
Comments
codetowns 9-Sep-13 17:15pm    
That's great. (I didn't realise you could use a FirstODefault Method).

Many thanks 7681169 !!!
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.

VB
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
 
Share this answer
 
Comments
codetowns 10-Sep-13 3:41am    
Thanks so much for taking the time to look at it Mike-MadBadger. I'll sit down and see how your code works and incorporate it.

Cheers all!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900