Hashing strings to Color in C# -


i don't know if hashing right word this, want convert string hex or argb color semi randomly.

i've used string.gethascode function, results skew towards greenness:

string[] list = { "test string", "something else", "test hooray" };  foreach (string k in list) {     string x = k.toupper().gethashcode().tostring("x8");     console.writeline("#" + x.substring(0,6)); } 

i'd ideally strings begin similar prefixes have different colors. example, "test string" , "test hooray" should different because both begin "test."

i'm not worried absolute solution, want enough. list[] @ most have 10 elements in @ single time, , of time 2, 3 or 4. means color has distinct 2 or 3 other colors.

i'm generating visual list, color references name, name should map same color.

edit: sample output:

#66bd44 #7ec83e #95e4fe 

colors: http://www.colorcombos.com/combotester.html?color0=66bd44&color1=7ec83e&color2=95e4fe&color3=000316

create md5 hash of string , take first 3 bytes red, green , blue components respectively.

the following demo produces reasonable distribution of colors.

var words = ("she sells sea shells on sea shore sea " +              "shells sells sea shells no more.").split(' '); var md5 = md5.create(); var box = new listbox     {         dock = dockstyle.fill,         drawmode = drawmode.ownerdrawfixed     }; box.items.addrange(words); box.drawitem += (sender, e) =>     {         var value = (string) box.items[e.index];         var hash = md5.computehash(encoding.utf8.getbytes(value));         var color = color.fromargb(hash[0], hash[1], hash[2]);         using (var backbrush = new solidbrush(color))         using (var forebrush = new solidbrush(e.forecolor))         {             e.graphics.fillrectangle(backbrush, e.bounds);             e.graphics.drawstring(value, e.font, forebrush, e.bounds);         }         e.drawfocusrectangle();     }; new form {controls = {box}}.showdialog(); 

screenshot


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -