Results 1 to 5 of 5

Thread: Get a random color-code depending on string?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Denmark
    Posts
    291

    Get a random color-code depending on string?

    Depending on a specific string, how would I retrieve a random color-code?

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Get a random color-code depending on string?

    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.
    Code:
    Dim WhiteColor As Color = Color.FromArgb(255, 255, 255)
    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.

    Also, your signature is way too big, please adjust the fonts.

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Get a random color-code depending on string?

    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?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Denmark
    Posts
    291

    Re: Get a random color-code depending on string?

    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!

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Get a random color-code depending on string?

    Glad you got it working.

    Still adjust the font sizes on your signature though please.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width