Depending on a specific string, how would I retrieve a random color-code?
Printable View
Depending on a specific string, how would I retrieve a random color-code?
You can create a color by using the FromARGB method of the color structure.
This would create a color with red, green, and blue values of 255, which is of course white.
So you could basically just grab 3 random numbers between the values of 0 (black) and 255 (white) and use those random values to create a random color.Code:Dim WhiteColor As Color = Color.FromArgb(255, 255, 255)
Also, your signature is way too big, please adjust the fonts.
Kleinma told you how to create a random color (you could include the alpha channel if you want), but I don't see how 'a specific string' comes into play here. What kind of string? How does the string determine the color? If the string determines the color, how is it random? Or is the string random?
Nevermind, I solved the problem.
Here's my code. It's for VB.NET WPF:
Dim MD5Engine As New Security.Cryptography.MD5CryptoServiceProvider
Private BrushConverter As New BrushConverter
Function GetMD5Hash(ByVal strToHash As String) As String
Dim BytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
BytesToHash = MD5Engine.ComputeHash(BytesToHash)
Dim Result As String = Nothing
For Each B As Byte In BytesToHash
Result &= B.ToString("x2")
Next
Return Result
End Function
Public Function GetColorCodeFromProcessName(ByVal ProcessName As String) As Brush
Dim FinalHexColor As String = GetMD5Hash(ProcessName).Substring(0, 6)
Return BrushConverter.ConvertFrom("#FF" & FinalHexColor)
End Function
As you can see, it returns a random color based on a target string name. However, if the same target string name is entered again, the same color is returned. Brilliant!
Glad you got it working. ;)
Still adjust the font sizes on your signature though please.