Reputation: 55
I have three labels and one button. I want to randomize the background color for label1 and label2, on the condition that not come the same color in label1 and label2, and with the click of a button I want to get label3 background color, which is a mixed color between label1 color and label2 color. In my code i have colorlist with some colors. I want to randomize colors that are included in my colorlist only thanks for your help
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Create a List
Dim colorList As New List(Of SolidBrush)
' Add colors to it
'red
colorList.Add(New SolidBrush(Color.FromArgb(100, 255, 0, 0)))
'white
colorList.Add(New SolidBrush(Color.FromArgb(100, 255, 255, 255)))
'Blue
colorList.Add(New SolidBrush(Color.FromArgb(100, 0, 0, 255)))
'Yellow
colorList.Add(New SolidBrush(Color.FromArgb(100, 244, 255, 16)))
'Green
colorList.Add(New SolidBrush(Color.FromArgb(100, 0, 255, 0)))
'Pink
colorList.Add(New SolidBrush(Color.FromArgb(100, 255, 16, 22)))
'Brown
colorList.Add(New SolidBrush(Color.FromArgb(100, 120, 37, 37)))
Dim rnd = New Random()
' Get a random item from the list between 0 and list count
Dim randomColour = colorList(rnd.Next(0, colorList.Count))
Dim randomColour1 = colorList(rnd.Next(0, colorList.Count))
' Assign the color to the label
Label1.BackColor = randomColour.Color
Label1.Text = randomColour.Color.Name.ToString
Label2.BackColor = randomColour1.Color
Label3.BackColor = (Color.FromArgb(Label1.BackColor.ToArgb + Label2.BackColor.ToArgb))
End Sub
Upvotes: 0
Views: 471
Reputation: 15091
You should not create a new Random class every time you click. Just make a class level variable. Mixing the colors takes each component of the color (R,G, B) and averages the value. Then creates a new color from the averages. The rest is pretty self explanatory.
You only need a List(Of Color)
Private rnd As New Random()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim colorList As New List(Of Color)
' Add colors to it
'red
colorList.Add(Color.FromArgb(255, 0, 0))
'white
colorList.Add(Color.FromArgb(255, 255, 255))
'Blue
colorList.Add(Color.FromArgb(0, 0, 255))
'Yellow
colorList.Add(Color.FromArgb(244, 255, 16))
'Green
colorList.Add(Color.FromArgb(0, 255, 0))
'Pink
colorList.Add(Color.FromArgb(255, 16, 22))
'Brown
colorList.Add(Color.FromArgb(120, 37, 37))
' Get a random item from the list between 0 and list count
Dim randomColour = colorList(rnd.Next(0, colorList.Count))
Dim randomColour1 = colorList(rnd.Next(0, colorList.Count))
' Get the name of the color to display in the label
Dim ColorName1 As String = GetColorName(randomColour.Name)
Dim ColorName2 As String = GetColorName(randomColour1.Name)
'Set the text color depending on the back color
If ColorName1 = "White" OrElse ColorName1 = "Yellow" Then
Label1.ForeColor = Color.Black
Else
Label1.ForeColor = Color.White
End If
If ColorName2 = "White" OrElse ColorName2 = "Yellow" Then
Label2.ForeColor = Color.Black
Else
Label2.ForeColor = Color.White
End If
'Set the back color and text of the labels.
Label1.BackColor = randomColour
Label1.Text = ColorName1
Label2.BackColor = randomColour1
Label2.Text = ColorName2
Label3.BackColor = MixColors(randomColour, randomColour1)
End Sub
Private Function MixColors(Color1 As Color, Color2 As Color) As Color
Dim r As Byte = CByte((Color1.R * 0.5) + (Color2.R * 0.5))
Dim g As Byte = CByte((Color1.G * 0.5) + (Color2.G * 0.5))
Dim b As Byte = CByte((Color1.B * 0.5) + (Color2.B * 0.5))
Return Color.FromArgb(r, g, b)
End Function
Private Function GetColorName(name As String) As String
Select Case name
Case "ffff0000"
Return "Red"
Case "ffffffff"
Return "White"
Case "ff0000ff"
Return "Blue"
Case "fff4ff10"
Return "Yellow"
Case "ff00ff00"
Return "Green"
Case "ffff1016"
Return "Pink"
Case "ff782525"
Return "Brown"
Case Else
Return "No match"
End Select
End Function
Upvotes: 1