Results 1 to 12 of 12

Thread: Who wants to have some fun?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Who wants to have some fun?

    So, I am trying to make a hash function that, given a string will return a unique color. Not a random color, but the same color everytime. I am just hoping to make it hash oddly enough that the colors will be varied. I am using it in a bar graph to color the different bars. With what I have now I am getting shades of red only. Anybody got any ideas to make this hash better?

    VB Code:
    1. Private Function ColorHash(ByVal strHashThis As String) As System.Drawing.Color
    2.         Dim x, y, z As Integer
    3.  
    4.  
    5. '122 is ascii value of 'Z'
    6. '57 is number of characters from 'a' to 'Z'
    7.         x = ((122 - Asc(strHashThis.Chars(0))) * 5) + Asc(strHashThis.Chars(strHashThis.Length - 1))
    8.         x = ((122 - Asc(strHashThis.Chars(strHashThis.Length - 1))) * 5) + Asc(strHashThis.Chars(0))
    9.         z = (strHashThis.Length Mod 57) * 5
    10.  
    11.         Return Color.FromArgb(x Mod 255, y Mod 255, z Mod 255)
    12.     End Function
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Who wants to have some fun?

    VB Code:
    1. y = ((122 - Asc(strHashThis.Chars(strHashThis.Length - 1))) * 5) + Asc(strHashThis.Chars(0))
    Ooops, I changed the second line to y instead of x. Still, though, I'm getting a lot of ugly greens and some strange reds, but not the rainbow of colors I was hoping for. I know I need to research how the number affect the outcome. I probably need to limit the range of my factors greatly, perhaps by modding them by a smaller number. I guess what I would really like is more traditional colors, if possible.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Who wants to have some fun?

    By the way, here are some samples of the strings I am sending to the function:
    Art Dept
    ASSEMBLE
    Assembly
    Fabrication
    Finish
    Manual
    Laster
    OP-Other
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Who wants to have some fun?

    What? Nobody wants to have some fun? You are all a bunch of party poopers.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  5. #5
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Who wants to have some fun?

    A good way to do that would be to make a back-loop function (I have one written, but it's at home) and have it cycle through all the letters in the string to find the character value and multiple/add the character PLACE value to it. Then, add them all together and get the first 3 numbers, the next 3 numbers starting from the second number, and so on for the third set.

    Then, with the back-loop function (a variation of Mod), add the remainder of thenumber Mod 255 and get the color value.

    Hard to explain, but when I get home I'll write a sample for you and see if it works. I'll comment it accordingly, of course.

    In any case, it won't generate a random color per-say. If you type in "testing" for the hash string, it will return the same color every time. But if you type in "testign", it will generate a completely different color.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Who wants to have some fun?

    Thats cool. I want it to generate the same color everytime based on what string it is sent.
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  7. #7
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Who wants to have some fun?

    That's what this will do. Didn't have time to do it when I got home, but I can tonight(hopefully, unless I get dragged out to the bar against my will when I'm sick :sad: ).
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: Who wants to have some fun?

    This is what I ended up doing. I seems to work well enough.
    VB Code:
    1. Private Function WCEfficiencyColorHash(ByVal strHashThis As String) As System.Drawing.Color
    2.         Dim x, y, z, Total As Integer
    3.         x = 0
    4.         For i As Integer = 0 To strHashThis.Length - 1
    5.             x += Asc(strHashThis.Chars(i))
    6.         Next
    7.         y = Math.Abs((strHashThis.Length * x) - Asc(strHashThis.Chars(0)))
    8.         z = x * y
    9.  
    10.         Return Color.FromArgb(x Mod 255, y Mod 255, z Mod 255)
    VB.Net 2008
    .Net Framework 2.0

    "Must you breathe? 'Cause I need heaven..."

  9. #9
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Who wants to have some fun?

    You can use this code (I used a picture box to change the color of, and a textbox to enter the string/color name):

    VB Code:
    1. Public Function colorRoundValueRGB(ByVal number As Long) As Long
    2.  
    3.         'Set boundries
    4.         If number < 1 Then Return 1
    5.         If number <= 255 Then Return 255
    6.  
    7.         'Find remaining placement value
    8.         Dim remain As Long = number - (255 * (Math.Floor(number / 255)))
    9.  
    10.         If remain < 1 Then remain = 1
    11.  
    12.         Return remain
    13.  
    14.     End Function
    15.  
    16.     Public Function colorRandomFromString(ByVal str As String) As Color
    17.  
    18.         Dim lng As Long = 1
    19.         Dim r, g, b As Integer
    20.  
    21.         For stp As Integer = 1 To str.Length
    22.             Try
    23.                 lng *= CInt(AscW(Mid(str, stp, 1)))
    24.             Catch ex As Exception
    25.                 If ex.Message.Contains("overflow") Then
    26.                     lng = 1
    27.                 End If
    28.             End Try
    29.         Next
    30.  
    31.         If lng < 99999 Then
    32.             lng += ((99999 - lng) ^ 2)
    33.         End If
    34.  
    35.         r = colorRoundValueRGB(CInt(Mid(CStr(lng), 1, 3)))
    36.         g = colorRoundValueRGB(CInt(Mid(CStr(lng), 2, 3)))
    37.         b = colorRoundValueRGB(CInt(Mid(CStr(lng), 3, 3)))
    38.  
    39.         Return Color.FromArgb(r, g, b)
    40.  
    41.     End Function

    Works for me, anyway. Still gets a bit of red and green, but you get a bunch of other colors, too. And it stays the same each time. Funny, this actually made me create a "moduleColor.vb" module. I've been looking for something interesting to try out while waiting for a cooperative project I'm working on to take off. This did it!
    Last edited by BrendanDavis; Feb 10th, 2007 at 01:06 AM.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  10. #10
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Who wants to have some fun?

    And you can use as large or as small of a string as you wish, doesn't really matter. Hence the try / catch.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  11. #11
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: Who wants to have some fun?

    Pretty interesting fact: I put this code in a timer:

    VB Code:
    1. PictureBox1.BackColor = colorRandomFromString(CStr(integerRandomBetween(0, 99999)))

    ...to generate basically convert a random number between 0 and 99999 to a string, and then generate a color based on that string. It creates a neat little flashy picturebox, lol.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  12. #12
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: Who wants to have some fun?

    This seems to work pretty good. As with all hash functions, there are collisions (two different things producing the same hash. You'd have to have a hash that contains exactly the same amount of data (or more) as the original string to prevent all collisions.) The fact that I manually set the color to opaque increases the likelihood of a collision.

    VB Code:
    1. Public Function ColorFromString(ByVal [String] As String) As Color
    2.         Return Color.FromArgb(255, Color.FromArgb([String].GetHashCode))
    3.     End Function

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