-1

How do I calculate a random color, thats going to be more or less unique to a specific string? Its true that RGB format of (0-255, 0-255, 0-255) will only allow maximum of 16581375 unique colors, while strings are indefinite.

The string can be random like hello123 or bye456.

My goal is to show a control (expander) in a almost unique color by text of the header. Because on other list in GUI there is a reference to that expander, so i want to help the user to find that entry quicker.

So I need to get a SolidColorBrush for background.

Example: The header of one expander is hello123 and the following expander header is bye456. So I want to set the background of each header to a (nearly) unique color.

Suplanus
  • 1,523
  • 16
  • 30

3 Answers3

1

You can set the background of your controls that uses SolidColorBrush like this:

using System.Windows.Media;

new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFDFD991"));
Imac
  • 379
  • 5
  • 11
  • I have edited the question, because the input string is a random string like hello123 and bye456. – Suplanus Jan 13 '17 at 12:52
  • What do you mean? Can you post exactly what you want? The objects and values that you are trying to use would be helpful. – Imac Jan 13 '17 at 12:54
  • I have self answered the question, dont understand why its been dow voted. Found no duplicate. Hope, now you understand the question. – Suplanus Jan 13 '17 at 12:57
  • It still unclear what is this string hash that you are trying to convert in color. Your question is too simple, without datails, probably that's why ppl downvoted it. – Imac Jan 13 '17 at 13:02
  • The string can be everythink like hello123... I convert a hash of it and of this hash a color and of the color a solidcolorbrush :) – Suplanus Jan 13 '17 at 13:05
  • 1
    How are you supposed to extract a color has from a string like this? It makes no sense. – Imac Jan 13 '17 at 13:10
  • 3
    What I assume he means is to calculate a random color, thats going to be more or less unique to a specific string, and he is looking for an algorithm for that. While it is possible, one has to bear in mind that RGB format of (0-255, 0-255, 0-255) will only allow maximum of 16581375 unique colors, while strings are indefinite. Still, I cannot see a point where one would need this, and OP needs to update his question with details. – Kamil Solecki Jan 13 '17 at 13:37
  • Thats right Kamil. Thanks, I have edited the question. – Suplanus Jan 13 '17 at 20:46
1

Could do something like this to get your color code

"#" + Convert.ToString("hello123".GetHashCode(), 16)

"hello123" returns #12c09349

Tyler Lee
  • 2,736
  • 13
  • 24
-3

With this method you get a SolidColorBrush which can be used as Background:

private static SolidColorBrush GetSolidColorBrushFromString(string value)
{
   // Get Color from string
   var md5 = MD5.Create();
   var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(value));
   var color = Color.FromRgb(hash[0], hash[1], hash[2]);
   var brush = new SolidColorBrush(color);
   brush.Opacity = 0.2; // Readability in UI
   return brush;
}

Usage:

var brushBackground = GetSolidColorBrushFromString(expander.Name);
expander.Background = brushBackground;
Suplanus
  • 1,523
  • 16
  • 30