user3037472
user3037472

Reputation: 15

Converting RGB value into integers

I'm making an app with a color dropper tool on it using g.CopyFromScreen(screenpoint, Point.Empty, Bmp2.Size) (the dropper tool works currently), once I have the dropper values I want to convert the RBG values into individual integers.

The values that i'm converting are in this format Color [A=255, R=240, G=240, B=240] which needs to be in four different integers

My code is giving me odd results and I'm lost now

My code:

    Dim text1Conv As String

    text1Conv = TextBox1.Text
    Dim myChars() As Char = text1Conv.ToCharArray()

For Each ch As Char In myChars
        If Char.IsDigit(ch) And Not ch = " " And Not ch = "," And Not count > 2 Then
            color1Conv = color1Conv + ch
            TextBox2.Text = TextBox2.Text + color1Conv 'test result
            count = count + 1
        ElseIf Char.IsDigit(ch) And Not ch = " " And Not ch = "," And count < 2 And Not count > 5 Then
            color2Conv = color2Conv + ch
            TextBox2.Text = TextBox2.Text + color2Conv 'test result
            count = count + 1
        ElseIf Char.IsDigit(ch) And Not ch = " " And Not ch = "," And count < 5 And Not count > 8 Then
            color3Conv = color3Conv + ch
            TextBox2.Text = TextBox2.Text + color3Conv 'test result
            count = count + 1
        ElseIf Char.IsDigit(ch) And Not ch = " " And Not ch = "," And count < 8 And Not count > 11 Then
            color4Conv = color4Conv + ch
            TextBox2.Text = TextBox2.Text + color4Conv 'test result
            count = count + 1
        End If

    Next

results: 225 255 118 112 122 results: 225 255 116 772 721

probably an easy one but I can't see it

Upvotes: 0

Views: 1211

Answers (2)

Victor Zakharov
Victor Zakharov

Reputation: 26424

You can use regular expressions:

Imports System.Text.RegularExpressions

Dim input As String = "Color [A=255, R=240, G=240, B=240]"
Dim re As New Regex("Color \[A=(\d+), R=(\d+), G=(\d+), B=(\d+)\]")
Dim m As Match = re.Match(input)
Dim integer1 As Integer = Convert.ToInt32(m.Groups(1).Value) '255
Dim integer2 As Integer = Convert.ToInt32(m.Groups(2).Value) '240
Dim integer3 As Integer = Convert.ToInt32(m.Groups(3).Value) '240
Dim integer4 As Integer = Convert.ToInt32(m.Groups(4).Value) '240

Upvotes: 0

Derek
Derek

Reputation: 8763

Using regular expressions: I used "[A=255, R=241, G=24, B=2]" as a test string and split it into four integers.

Dim a as Integer, r as Integer, g as Integer, b as Integer
Dim s as String = "[A=255, R=241, G=24, B=2]"
Dim mc as MatchCollection = System.Text.RegularExpressions.Regex.Matches( s, "(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+", RegexOptions.None )  
Integer.TryParse( mc(0).Groups(1).Value, a )
Integer.TryParse( mc(0).Groups(2).Value, r )
Integer.TryParse( mc(0).Groups(3).Value, g )
Integer.TryParse( mc(0).Groups(4).Value, b )

NOTE: it will have no problems with numbers being 1, 2, or any number of digits long.

Upvotes: 1

Related Questions