I'd like to form an algorithmic connections between a particular string and and a particular color, in such that each unique string is always the same color.
My particular use case is for a kiosk where each user should immediately notice if someone else is logged in, so my strings are employee first names. Reinforcing this visually by assigning each user a different color. And yes I know i could just build a list or add a property to the user object but this is more fun. Here's my current approach:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
float r = 0;
float g = 0;
float b = 0;
var name = value as string;
if (name.IsNullOrEmpty()) return new SolidColorBrush(Colors.Black);
var pos = 0;
foreach (var letter in name)
{
var index = letter % 32;
if (pos == 0) r = (float)index / AlphabetLength;
if (pos == 1) g = (float)index / AlphabetLength;
b = (float)index / AlphabetLength;
pos++;
}
return new SolidColorBrush(Color.FromScRgb(1f, r, g, b));
}
private const int AlphabetLength = 26;
Kind of came up with this quickly so there are a bunch of weaknesses. What do you think?
String.IsNullOrEmpty
isn't an extension method. Are you sure this code can be compiled? \$\endgroup\$