Hashing strings to Color in C#
Asked Answered
L

10

22

I don't know if hashing is the right word for this, but I want to convert a string into a hex or argb color semi randomly.

I've used the string.GetHasCode function, but the 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 like strings that begin with similar prefixes to have widely different colors. For example, "Test String" and "Test Hooray" should be completely different because they both begin with "Test."

I'm not really worried about an absolute solution, I just want something that is good enough. The list[] will at most have like 10 elements in it at a single time, and most of the time only 2, 3 or 4. This means that a color only has to be distinct from 2 or 3 other colors.

I'm basically generating a visual list, where the color references the name, but the name should ALWAYS map to the same color.

Edit: Sample Output:

#66BD44
#7EC83E
#95E4FE

Colors: http://www.colorcombos.com/combotester.html?color0=66BD44&color1=7EC83E&color2=95E4FE&color3=000316

Ligan answered 8/10, 2010 at 1:25 Comment(1)
compute the hash value however you want, but use it as a hue instead of an rgb value, then convert hsl to rgb using the same S and L for all the conversions, then you won't get such ugly colours.Aylmar
C
30

Create an MD5 hash of the string and take the first three bytes as the red, green and blue components respectively.

The following demo produces a reasonable distribution of colors.

var words = ("She sells sea shells on the sea shore but the sea " +
             "shells she sells are 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

Crewel answered 8/10, 2010 at 2:22 Comment(3)
Thanks! Pictures say a thousand words and the new SO integration with imgur makes it that little bit easier to insert screenshots.Crewel
Is there something in this code that's ensuring a reasonable colour value/lightness? Why are there no really dark or really light colours?Protagoras
@Protagoras Basic statistics. You have three independent channels each with a uniform distribution; the overall brightness distribution curve will be akin to a 3d256 such as anydice.com/program/df1c (ignoring that each channel contributes a different weight to the perceived brightness, of course).Zulazulch
I
3

The following Wang/Jenkins-based rehashing code can help improve bit distribution:

        private static int ReHash(int srcHash)
        {
            unchecked
            {
                uint h = (uint)srcHash;
                h += (h << 15) ^ 0xffffcd7d;
                h ^= (h >> 10);
                h += (h << 3);
                h ^= (h >> 6);
                h += (h << 2) + (h << 14);
                return (int)(h ^ (h >> 16));
            }
        }

However, I the colours may still be distributed visually less well. The RGB system splits up the colours in a somewhat skewed way, and our eyes don't process colours quite as RGB codes would have one suspect.

Islam answered 8/10, 2010 at 1:58 Comment(2)
Then compute a hue convert it to rgb instead.. has the added bonus that you don't get colors that are too light to read against the background.Aylmar
@Mark, that brings its own issues too, but would probably be the way to go really. That said, if Nathan's MD5-based method worked well enough, then hopefully this would too with less overhead.Islam
H
1

Doesn't string just use object's GetHashCode(), which returns the memory address from the reference? That would explain why all of your codes gravitate towards a specific spectrum. It would also mean that the color is not deterministic between runs, as the memory locations would be different.

What you should probably do instead is use one of the cryptographic hashing functions, like SHA1, and then select a (the same) random 3-byte section from each string.

EDIT System.String.GetHashCode() loops over the string, and performs mathematical operations on the characters contained in the string (I'm not currently alert enough to go through the math...). This allows the hash code to always be the same, but it also explains why "Test String" and "Test Hooray" produce very similar colors - it's not designed to be secure, just repeatable.

Hardshell answered 8/10, 2010 at 1:28 Comment(3)
System.String does not use the Object.GetHashCode() implementation. It determines the hashcode as a combination of characters in the string, otherwise 2 strings at different memory locations wouldn't generate the same hashcode.Tumid
I just got done finding it in Reflector. I'm going to update my post now.Hardshell
String.GetHashCode() is designed to provide a good trade-off between uniformity of distribution over the space of Int32, repeatability for the same sequences of characters, and speed of generation.Tumid
F
1

I don't think your ideal solution is consistent, because the only way I see to make sure strings with similar prefixes are far apart in the color space is to have a mapping that depends on existing hashed values, which means it won't be consistent.

Consider the edge case where you have 2^24 strings that start with "Test "...

Interesting question, though. How green is the mapping right now? (I can't think of a reason why it should be so).

Fornof answered 8/10, 2010 at 1:33 Comment(1)
There are only so many rgb values, so some level of repetition is expected, but if I could get out of the green spectrum that would help. I added a link to the colors so you can see the greens.Ligan
W
1

I would use an MD5-Hash:

http://msdn.microsoft.com/de-de/library/system.security.cryptography.md5.aspx

Changing one char from the original sequence completely "shuffles" the md5-sequence. For "md5-to-color"-mapping just cut some bits.

Worsted answered 8/10, 2010 at 1:36 Comment(0)
H
1

For some value N (e.g. N=10 or N=100), use a hashing algorithm that hashes your population into N buckets (e.g. GetHashCode() modulo 10). Use that value as an index into a table of N colors. Then select N distinct colors for your index table.

Heraclea answered 8/10, 2010 at 1:43 Comment(0)
S
0

Simple solution: loop over your string, adding up ascii values (or unicode if you like) of individual characters. You'll end up with some integer; % it to a desirable number of bits (say, 6) and split the result into 3 components (r, g, b). Multiply or bit-shift as needed to make sure you cover the full range of the RGB color space.

Swirly answered 8/10, 2010 at 1:32 Comment(0)
T
0

I think your implementation skews toward greenness because the gamut you are using skews toward greenness.

Tumid answered 8/10, 2010 at 1:36 Comment(0)
A
0

Edit: Darn..over my character limit.. anyway, wrote this in PHP, but you can easily translate it to C#. It takes a string and returns a hue to use as your color..you can decide on the saturation and lightness yourself (I suggest keeping those consistent). These numbers aren't random, they're evenly distributed across an English dictionary ('a' has a larger range because more words start with 'a', likewise, not many words start with 'x' so only a few hues are assigned to it)

<?php
// first letter
$s2h1['a'] = 0;
$s2h1['b'] = 24.0006119483;
$s2h1['c'] = 45.2658150386;
$s2h1['d'] = 80.9852367475;
$s2h1['e'] = 96.5807389276;
$s2h1['f'] = 108.9573931;
$s2h1['g'] = 126.34284403;
$s2h1['h'] = 150.8544328;
$s2h1['i'] = 165.473877457;
$s2h1['j'] = 174.374665341;
$s2h1['k'] = 179.306968561;
$s2h1['l'] = 183.679339096;
$s2h1['m'] = 197.864300467;
$s2h1['n'] = 218.875545016;
$s2h1['o'] = 227.351028838;
$s2h1['p'] = 235.91218542;
$s2h1['q'] = 264.621739463;
$s2h1['r'] = 266.081236136;
$s2h1['s'] = 280.621127515;
$s2h1['t'] = 319.660368699;
$s2h1['u'] = 337.783217318;
$s2h1['v'] = 341.427369387;
$s2h1['w'] = 347.335730131;
$s2h1['x'] = 357.00757286;
$s2h1['y'] = 357.509370458;
$s2h1['z'] = 358.901552819;
$s2h1['{'] = 360;
// second letter
$s2h2['a']['a'] = 0;
$s2h2['a']['b'] = 0.143807848237;
$s2h2['a']['c'] = 1.15046278589;
$s2h2['a']['d'] = 3.16377266121;
$s2h2['a']['e'] = 4.3142354471;
$s2h2['a']['f'] = 4.69364338713;
$s2h2['a']['g'] = 5.14342538056;
$s2h2['a']['h'] = 5.81962824141;
$s2h2['a']['i'] = 5.85634513884;
$s2h2['a']['j'] = 6.45299472195;
$s2h2['a']['k'] = 6.47747265356;
$s2h2['a']['l'] = 6.55702593131;
$s2h2['a']['m'] = 9.49743746653;
$s2h2['a']['n'] = 11.2904459573;
$s2h2['a']['o'] = 15.0080318213;
$s2h2['a']['p'] = 15.0478084602;
$s2h2['a']['q'] = 16.109538744;
$s2h2['a']['r'] = 16.2472271093;
$s2h2['a']['s'] = 19.4385374436;
$s2h2['a']['t'] = 20.9286315306;
$s2h2['a']['u'] = 22.1800657844;
$s2h2['a']['v'] = 23.4009026237;
$s2h2['a']['w'] = 23.6518014228;
$s2h2['a']['x'] = 23.700757286;
$s2h2['a']['y'] = 23.8017287539;
$s2h2['a']['z'] = 23.8568041001;
$s2h2['b']['a'] = 0;
$s2h2['b']['b'] = 5.03021494684;
$s2h2['b']['c'] = 5.0455136541;
$s2h2['b']['d'] = 5.0455136541;
$s2h2['b']['e'] = 5.051633137;
$s2h2['b']['f'] = 8.01040312094;
$s2h2['b']['g'] = 8.01040312094;
$s2h2['b']['h'] = 8.01040312094;
$s2h2['b']['i'] = 8.05323950126;
$s2h2['b']['j'] = 10.0390117035;
$s2h2['b']['k'] = 10.0451311864;
$s2h2['b']['l'] = 10.0481909279;
$s2h2['b']['m'] = 12.5938958158;
$s2h2['b']['n'] = 12.6091945231;
$s2h2['b']['o'] = 12.6122542645;
$s2h2['b']['p'] = 15.6689359749;
$s2h2['b']['q'] = 15.6811749407;
$s2h2['b']['r'] = 15.6811749407;
$s2h2['b']['s'] = 18.7133787195;
$s2h2['b']['t'] = 18.7225579439;
$s2h2['b']['u'] = 18.7256176853;
$s2h2['b']['v'] = 21.1183355007;
$s2h2['b']['w'] = 21.1244549836;
$s2h2['b']['x'] = 21.1366939494;
$s2h2['b']['y'] = 21.1366939494;
$s2h2['b']['z'] = 21.2652030903;
$s2h2['c']['a'] = 0;
$s2h2['c']['b'] = 8.33779545628;
$s2h2['c']['c'] = 8.34391493919;
$s2h2['c']['d'] = 8.35003442209;
$s2h2['c']['e'] = 8.39899028532;
$s2h2['c']['f'] = 10.2409546393;
$s2h2['c']['g'] = 10.2531936051;
$s2h2['c']['h'] = 10.259313088;
$s2h2['c']['i'] = 15.724011321;
$s2h2['c']['j'] = 16.9295494531;
$s2h2['c']['k'] = 16.9326091945;
$s2h2['c']['l'] = 16.9326091945;
$s2h2['c']['m'] = 19.4171192534;
$s2h2['c']['n'] = 19.4293582192;
$s2h2['c']['o'] = 19.4905530483;
$s2h2['c']['p'] = 30.4444274459;
$s2h2['c']['q'] = 30.4627858946;
$s2h2['c']['r'] = 30.4627858946;
$s2h2['c']['s'] = 33.1094622504;
$s2h2['c']['t'] = 33.1247609577;
$s2h2['c']['u'] = 33.1645375966;
$s2h2['c']['v'] = 34.6454524593;
$s2h2['c']['w'] = 34.6515719422;
$s2h2['c']['x'] = 34.6576914251;
$s2h2['c']['y'] = 34.6576914251;
$s2h2['c']['z'] = 35.6674061042;
$s2h2['d']['a'] = 0;
$s2h2['d']['b'] = 2.00107090951;
$s2h2['d']['c'] = 2.01025013386;
$s2h2['d']['d'] = 2.01942935822;
$s2h2['d']['e'] = 2.03472806548;
$s2h2['d']['f'] = 6.36732196129;
$s2h2['d']['g'] = 6.37038170275;
$s2h2['d']['h'] = 6.3734414442;
$s2h2['d']['i'] = 6.43463627323;
$s2h2['d']['j'] = 10.4245391264;
$s2h2['d']['k'] = 10.4520767995;
$s2h2['d']['l'] = 10.4612560239;
$s2h2['d']['m'] = 10.4673755068;
$s2h2['d']['n'] = 10.4979729213;
$s2h2['d']['o'] = 10.5224508529;
$s2h2['d']['p'] = 12.6459114205;
$s2h2['d']['q'] = 12.6612101277;
$s2h2['d']['r'] = 12.6612101277;
$s2h2['d']['s'] = 14.1757821464;
$s2h2['d']['t'] = 14.1849613708;
$s2h2['d']['u'] = 14.1910808537;
$s2h2['d']['v'] = 15.1701981183;
$s2h2['d']['w'] = 15.1763176012;
$s2h2['d']['x'] = 15.3384838981;
$s2h2['d']['y'] = 15.3384838981;
$s2h2['d']['z'] = 15.5924424386;
$s2h2['e']['a'] = 0;
$s2h2['e']['b'] = 0.81083148474;
$s2h2['e']['c'] = 0.902623728295;
$s2h2['e']['d'] = 1.44113822382;
$s2h2['e']['e'] = 2.02860858257;
$s2h2['e']['f'] = 2.06532548;
$s2h2['e']['g'] = 2.18771513807;
$s2h2['e']['h'] = 2.45391264438;
$s2h2['e']['i'] = 2.46615161019;
$s2h2['e']['j'] = 2.63443739004;
$s2h2['e']['k'] = 2.65891532166;
$s2h2['e']['l'] = 2.66809454601;
$s2h2['e']['m'] = 4.40908743211;
$s2h2['e']['n'] = 5.12506693184;
$s2h2['e']['o'] = 6.61822076035;
$s2h2['e']['p'] = 6.66717662358;
$s2h2['e']['q'] = 7.37397689895;
$s2h2['e']['r'] = 7.67077181978;
$s2h2['e']['s'] = 8.43876692419;
$s2h2['e']['t'] = 8.97116193682;
$s2h2['e']['u'] = 9.34445039394;
$s2h2['e']['v'] = 10.3725235218;
$s2h2['e']['w'] = 10.6968561157;
$s2h2['e']['x'] = 10.7182743058;
$s2h2['e']['y'] = 12.134934598;
$s2h2['e']['z'] = 12.3460567582;
$s2h2['f']['a'] = 0;
$s2h2['f']['b'] = 5.7125372906;
$s2h2['f']['c'] = 5.7186567735;
$s2h2['f']['d'] = 5.72477625641;
$s2h2['f']['e'] = 5.73395548076;
$s2h2['f']['f'] = 7.17815344603;
$s2h2['f']['g'] = 7.17815344603;
$s2h2['f']['h'] = 7.17815344603;
$s2h2['f']['i'] = 7.18427292894;
$s2h2['f']['j'] = 9.9380402356;
$s2h2['f']['k'] = 9.94109997705;
$s2h2['f']['l'] = 9.94109997705;
$s2h2['f']['m'] = 11.884035799;
$s2h2['f']['n'] = 11.8901552819;
$s2h2['f']['o'] = 11.8932150233;
$s2h2['f']['p'] = 14.0258548153;
$s2h2['f']['q'] = 14.0319742982;
$s2h2['f']['r'] = 14.0319742982;
$s2h2['f']['s'] = 16.3543180601;
$s2h2['f']['t'] = 16.360437543;
$s2h2['f']['u'] = 16.3787959917;
$s2h2['f']['v'] = 17.3640327392;
$s2h2['f']['w'] = 17.3640327392;
$s2h2['f']['x'] = 17.3670924807;
$s2h2['f']['y'] = 17.3670924807;
$s2h2['f']['z'] = 17.3854509294;
$s2h2['g']['a'] = 0;
$s2h2['g']['b'] = 2.41719574696;
$s2h2['g']['c'] = 2.42637497131;
$s2h2['g']['d'] = 2.43555419567;
$s2h2['g']['e'] = 2.44473342003;
$s2h2['g']['f'] = 15.5587852826;
$s2h2['g']['g'] = 15.5587852826;
$s2h2['g']['h'] = 15.5587852826;
$s2h2['g']['i'] = 15.6566970091;
$s2h2['g']['j'] = 16.5929778934;
$s2h2['g']['k'] = 16.5960376348;
$s2h2['g']['l'] = 16.5960376348;
$s2h2['g']['m'] = 17.6302302455;
$s2h2['g']['n'] = 17.6363497284;
$s2h2['g']['o'] = 17.7250822306;
$s2h2['g']['p'] = 19.5456283944;
$s2h2['g']['q'] = 19.5609271017;
$s2h2['g']['r'] = 19.5609271017;
$s2h2['g']['s'] = 22.9235829572;
$s2h2['g']['t'] = 22.9327621816;
$s2h2['g']['u'] = 22.9327621816;
$s2h2['g']['v'] = 24.1811366939;
$s2h2['g']['w'] = 24.1811366939;
$s2h2['g']['x'] = 24.1903159183;
$s2h2['g']['y'] = 24.1903159183;
$s2h2['g']['z'] = 24.5115887707;
$s2h2['h']['a'] = 0;
$s2h2['h']['b'] = 3.60437543028;
$s2h2['h']['c'] = 3.60743517173;
$s2h2['h']['d'] = 3.61355465463;
$s2h2['h']['e'] = 3.62273387899;
$s2h2['h']['f'] = 7.0465845636;
$s2h2['h']['g'] = 7.05270404651;
$s2h2['h']['h'] = 7.05576378796;
$s2h2['h']['i'] = 7.05882352941;
$s2h2['h']['j'] = 8.45712537291;
$s2h2['h']['k'] = 8.45712537291;
$s2h2['h']['l'] = 8.45712537291;
$s2h2['h']['m'] = 8.46018511436;
$s2h2['h']['n'] = 8.47548382162;
$s2h2['h']['o'] = 8.48160330452;
$s2h2['h']['p'] = 11.7402279507;
$s2h2['h']['q'] = 11.7432876922;
$s2h2['h']['r'] = 11.7463474336;
$s2h2['h']['s'] = 11.7616461409;
$s2h2['h']['t'] = 11.789183814;
$s2h2['h']['u'] = 11.7983630383;
$s2h2['h']['v'] = 12.8417348734;
$s2h2['h']['w'] = 12.8417348734;
$s2h2['h']['x'] = 12.8447946149;
$s2h2['h']['y'] = 12.8447946149;
$s2h2['h']['z'] = 14.6163849155;
$s2h2['i']['a'] = 0;
$s2h2['i']['b'] = 0.100971467911;
$s2h2['i']['c'] = 0.171345521303;
$s2h2['i']['d'] = 0.559932685688;
$s2h2['i']['e'] = 0.847548382162;
$s2h2['i']['f'] = 0.856727606517;
$s2h2['i']['g'] = 0.859787347969;
$s2h2['i']['h'] = 1.01889390347;
$s2h2['i']['i'] = 1.01889390347;
$s2h2['i']['j'] = 1.06173028379;
$s2h2['i']['k'] = 1.0739692496;
$s2h2['i']['l'] = 1.08314847395;
$s2h2['i']['m'] = 1.37994339478;
$s2h2['i']['n'] = 2.3131645376;
$s2h2['i']['o'] = 7.03740533925;
$s2h2['i']['p'] = 7.2056911191;
$s2h2['i']['q'] = 7.28524439685;
$s2h2['i']['r'] = 7.29136387975;
$s2h2['i']['s'] = 7.94002906754;
$s2h2['i']['t'] = 8.5336189092;
$s2h2['i']['u'] = 8.68966572325;
$s2h2['i']['v'] = 8.6927254647;
$s2h2['i']['w'] = 8.81511512277;
$s2h2['i']['x'] = 8.82735408858;
$s2h2['i']['y'] = 8.87325021036;
$s2h2['i']['z'] = 8.87936969326;
$s2h2['j']['a'] = 0;
$s2h2['j']['b'] = 1.59106555496;
$s2h2['j']['c'] = 1.59106555496;
$s2h2['j']['d'] = 1.59106555496;
$s2h2['j']['e'] = 1.59412529641;
$s2h2['j']['f'] = 2.28868660598;
$s2h2['j']['g'] = 2.29174634743;
$s2h2['j']['h'] = 2.29174634743;
$s2h2['j']['i'] = 2.29480608889;
$s2h2['j']['j'] = 2.49980876616;
$s2h2['j']['k'] = 2.49980876616;
$s2h2['j']['l'] = 2.49980876616;
$s2h2['j']['m'] = 2.49980876616;
$s2h2['j']['n'] = 2.49980876616;
$s2h2['j']['o'] = 2.50592824906;
$s2h2['j']['p'] = 3.92870802417;
$s2h2['j']['q'] = 3.92870802417;
$s2h2['j']['r'] = 3.92870802417;
$s2h2['j']['s'] = 3.93482750708;
$s2h2['j']['t'] = 3.93482750708;
$s2h2['j']['u'] = 3.93482750708;
$s2h2['j']['v'] = 4.92006425457;
$s2h2['j']['w'] = 4.92312399602;
$s2h2['j']['x'] = 4.92312399602;
$s2h2['j']['y'] = 4.92312399602;
$s2h2['j']['z'] = 4.93230322038;
$s2h2['k']['a'] = 0;
$s2h2['k']['b'] = 0.991356230399;
$s2h2['k']['c'] = 1.00053545475;
$s2h2['k']['d'] = 1.00359519621;
$s2h2['k']['e'] = 1.00359519621;
$s2h2['k']['f'] = 1.75017211046;
$s2h2['k']['g'] = 1.75017211046;
$s2h2['k']['h'] = 1.75629159336;
$s2h2['k']['i'] = 1.89703970015;
$s2h2['k']['j'] = 2.94959075958;
$s2h2['k']['k'] = 2.94959075958;
$s2h2['k']['l'] = 2.95265050103;
$s2h2['k']['m'] = 3.0689206762;
$s2h2['k']['n'] = 3.07504015911;
$s2h2['k']['o'] = 3.45444809914;
$s2h2['k']['p'] = 3.83997552207;
$s2h2['k']['q'] = 3.84609500497;
$s2h2['k']['r'] = 3.84609500497;
$s2h2['k']['s'] = 3.98684311176;
$s2h2['k']['t'] = 3.99296259466;
$s2h2['k']['u'] = 3.99602233611;
$s2h2['k']['v'] = 4.24692113516;
$s2h2['k']['w'] = 4.25610035952;
$s2h2['k']['x'] = 4.3142354471;
$s2h2['k']['y'] = 4.3142354471;
$s2h2['k']['z'] = 4.37237053469;
$s2h2['l']['a'] = 0;
$s2h2['l']['b'] = 3.72676508835;
$s2h2['l']['c'] = 3.73594431271;
$s2h2['l']['d'] = 3.74206379561;
$s2h2['l']['e'] = 3.75124301996;
$s2h2['l']['f'] = 6.60904153599;
$s2h2['l']['g'] = 6.61210127744;
$s2h2['l']['h'] = 6.61822076035;
$s2h2['l']['i'] = 6.63045972615;
$s2h2['l']['j'] = 9.86460644076;
$s2h2['l']['k'] = 9.86766618221;
$s2h2['l']['l'] = 9.86766618221;
$s2h2['l']['m'] = 9.90744282108;
$s2h2['l']['n'] = 9.91050256253;
$s2h2['l']['o'] = 9.91050256253;
$s2h2['l']['p'] = 12.5541191769;
$s2h2['l']['q'] = 12.5602386598;
$s2h2['l']['r'] = 12.5602386598;
$s2h2['l']['s'] = 12.5632984013;
$s2h2['l']['t'] = 12.5663581427;
$s2h2['l']['u'] = 12.5755373671;
$s2h2['l']['v'] = 13.5454754073;
$s2h2['l']['w'] = 13.5454754073;
$s2h2['l']['x'] = 13.5485351488;
$s2h2['l']['y'] = 13.5607741146;
$s2h2['l']['z'] = 14.1849613708;
$s2h2['m']['a'] = 0;
$s2h2['m']['b'] = 7.08024171957;
$s2h2['m']['c'] = 7.10165990974;
$s2h2['m']['d'] = 7.17203396313;
$s2h2['m']['e'] = 7.18427292894;
$s2h2['m']['f'] = 10.9844718121;
$s2h2['m']['g'] = 10.9936510365;
$s2h2['m']['h'] = 10.9967107779;
$s2h2['m']['i'] = 11.0028302608;
$s2h2['m']['j'] = 14.2553354242;
$s2h2['m']['k'] = 14.2553354242;
$s2h2['m']['l'] = 14.2583951656;
$s2h2['m']['m'] = 14.26757439;
$s2h2['m']['n'] = 14.2767536143;
$s2h2['m']['o'] = 14.2981718045;
$s2h2['m']['p'] = 18.107549912;
$s2h2['m']['q'] = 18.1197888778;
$s2h2['m']['r'] = 18.1197888778;
$s2h2['m']['s'] = 18.1595655167;
$s2h2['m']['t'] = 18.1901629312;
$s2h2['m']['u'] = 18.2238200872;
$s2h2['m']['v'] = 20.2126520309;
$s2h2['m']['w'] = 20.2187715138;
$s2h2['m']['x'] = 20.2248909967;
$s2h2['m']['y'] = 20.2279507382;
$s2h2['m']['z'] = 21.0112445498;
$s2h2['n']['a'] = 0;
$s2h2['n']['b'] = 1.80218771514;
$s2h2['n']['c'] = 1.81136693949;
$s2h2['n']['d'] = 1.8174864224;
$s2h2['n']['e'] = 1.82666564675;
$s2h2['n']['f'] = 4.31117570565;
$s2h2['n']['g'] = 4.31117570565;
$s2h2['n']['h'] = 4.34789260308;
$s2h2['n']['i'] = 4.35095234453;
$s2h2['n']['j'] = 5.44022030138;
$s2h2['n']['k'] = 5.44939952574;
$s2h2['n']['l'] = 5.44939952574;
$s2h2['n']['m'] = 5.45551900864;
$s2h2['n']['n'] = 5.46163849155;
$s2h2['n']['o'] = 5.4708177159;
$s2h2['n']['p'] = 7.46576914251;
$s2h2['n']['q'] = 7.47494836686;
$s2h2['n']['r'] = 7.47494836686;
$s2h2['n']['s'] = 7.49942629848;
$s2h2['n']['t'] = 7.52084448864;
$s2h2['n']['u'] = 7.52696397154;
$s2h2['n']['v'] = 8.27048114434;
$s2h2['n']['w'] = 8.2735408858;
$s2h2['n']['x'] = 8.28272011015;
$s2h2['n']['y'] = 8.28272011015;
$s2h2['n']['z'] = 8.47548382162;
$s2h2['o']['a'] = 0;
$s2h2['o']['b'] = 0.146867589689;
$s2h2['o']['c'] = 0.581350875851;
$s2h2['o']['d'] = 1.00971467911;
$s2h2['o']['e'] = 1.19635890767;
$s2h2['o']['f'] = 1.3218083072;
$s2h2['o']['g'] = 1.51763176012;
$s2h2['o']['h'] = 1.55434865754;
$s2h2['o']['i'] = 1.59106555496;
$s2h2['o']['j'] = 1.78994874933;
$s2h2['o']['k'] = 1.79912797369;
$s2h2['o']['l'] = 1.86338254418;
$s2h2['o']['m'] = 2.54570488794;
$s2h2['o']['n'] = 2.71705040924;
$s2h2['o']['o'] = 3.17907136847;
$s2h2['o']['p'] = 3.23720645605;
$s2h2['o']['q'] = 3.98378337031;
$s2h2['o']['r'] = 3.98378337031;
$s2h2['o']['s'] = 6.44381549759;
$s2h2['o']['t'] = 6.93031438843;
$s2h2['o']['u'] = 7.15367551442;
$s2h2['o']['v'] = 7.52696397154;
$s2h2['o']['w'] = 8.06853820852;
$s2h2['o']['x'] = 8.11749407175;
$s2h2['o']['y'] = 8.42958769984;
$s2h2['o']['z'] = 8.49690201178;
$s2h2['p']['a'] = 0;
$s2h2['p']['b'] = 5.18014227798;
$s2h2['p']['c'] = 5.18932150233;
$s2h2['p']['d'] = 5.20156046814;
$s2h2['p']['e'] = 5.21379943395;
$s2h2['p']['f'] = 9.11802952651;
$s2h2['p']['g'] = 9.13638797522;
$s2h2['p']['h'] = 9.13638797522;
$s2h2['p']['i'] = 11.3638797522;
$s2h2['p']['j'] = 14.1207068003;
$s2h2['p']['k'] = 14.1237665417;
$s2h2['p']['l'] = 14.1329457661;
$s2h2['p']['m'] = 16.2533465922;
$s2h2['p']['n'] = 16.2594660751;
$s2h2['p']['o'] = 16.3359596114;
$s2h2['p']['p'] = 20.6960911803;
$s2h2['p']['q'] = 20.7052704047;
$s2h2['p']['r'] = 20.7052704047;
$s2h2['p']['s'] = 25.5916775033;
$s2h2['p']['t'] = 26.2434024325;
$s2h2['p']['u'] = 26.5065401974;
$s2h2['p']['v'] = 28.149621357;
$s2h2['p']['w'] = 28.1557408399;
$s2h2['p']['x'] = 28.1588005814;
$s2h2['p']['y'] = 28.1618603228;
$s2h2['p']['z'] = 28.7095540427;
$s2h2['q']['a'] = 0;
$s2h2['q']['b'] = 0.0611948290369;
$s2h2['q']['c'] = 0.0611948290369;
$s2h2['q']['d'] = 0.0642545704888;
$s2h2['q']['e'] = 0.0642545704888;
$s2h2['q']['f'] = 0.0703740533925;
$s2h2['q']['g'] = 0.0703740533925;
$s2h2['q']['h'] = 0.0703740533925;
$s2h2['q']['i'] = 0.0703740533925;
$s2h2['q']['j'] = 0.107090950815;
$s2h2['q']['k'] = 0.107090950815;
$s2h2['q']['l'] = 0.107090950815;
$s2h2['q']['m'] = 0.107090950815;
$s2h2['q']['n'] = 0.107090950815;
$s2h2['q']['o'] = 0.107090950815;
$s2h2['q']['p'] = 0.110150692267;
$s2h2['q']['q'] = 0.110150692267;
$s2h2['q']['r'] = 0.110150692267;
$s2h2['q']['s'] = 0.110150692267;
$s2h2['q']['t'] = 0.110150692267;
$s2h2['q']['u'] = 0.110150692267;
$s2h2['q']['v'] = 1.45643693108;
$s2h2['q']['w'] = 1.45643693108;
$s2h2['q']['x'] = 1.45949667253;
$s2h2['q']['y'] = 1.45949667253;
$s2h2['q']['z'] = 1.45949667253;
$s2h2['r']['a'] = 0;
$s2h2['r']['b'] = 2.75988678957;
$s2h2['r']['c'] = 2.76906601392;
$s2h2['r']['d'] = 2.77212575537;
$s2h2['r']['e'] = 2.77212575537;
$s2h2['r']['f'] = 8.24294347128;
$s2h2['r']['g'] = 8.24906295418;
$s2h2['r']['h'] = 8.25212269563;
$s2h2['r']['i'] = 8.95892297101;
$s2h2['r']['j'] = 10.4765547311;
$s2h2['r']['k'] = 10.4765547311;
$s2h2['r']['l'] = 10.4765547311;
$s2h2['r']['m'] = 10.4765547311;
$s2h2['r']['n'] = 10.4765547311;
$s2h2['r']['o'] = 10.4887936969;
$s2h2['r']['p'] = 13.279277901;
$s2h2['r']['q'] = 13.2853973839;
$s2h2['r']['r'] = 13.2853973839;
$s2h2['r']['s'] = 13.2853973839;
$s2h2['r']['t'] = 13.2853973839;
$s2h2['r']['u'] = 13.2884571254;
$s2h2['r']['v'] = 14.4695173258;
$s2h2['r']['w'] = 14.4725770672;
$s2h2['r']['x'] = 14.484816033;
$s2h2['r']['y'] = 14.484816033;
$s2h2['r']['z'] = 14.5398913792;
$s2h2['s']['a'] = 0;
$s2h2['s']['b'] = 4.55901476325;
$s2h2['s']['c'] = 4.57125372906;
$s2h2['s']['d'] = 7.2883041383;
$s2h2['s']['e'] = 7.29136387975;
$s2h2['s']['f'] = 11.911573472;
$s2h2['s']['g'] = 11.9176929549;
$s2h2['s']['h'] = 11.9299319208;
$s2h2['s']['i'] = 14.6745200031;
$s2h2['s']['j'] = 17.7250822306;
$s2h2['s']['k'] = 17.728141972;
$s2h2['s']['l'] = 18.364568194;
$s2h2['s']['m'] = 19.3467452;
$s2h2['s']['n'] = 19.9464545246;
$s2h2['s']['o'] = 20.6379560927;
$s2h2['s']['p'] = 23.6212040083;
$s2h2['s']['q'] = 27.0266962442;
$s2h2['s']['r'] = 27.4214028915;
$s2h2['s']['s'] = 27.4428210816;
$s2h2['s']['t'] = 27.4642392718;
$s2h2['s']['u'] = 33.2043142354;
$s2h2['s']['v'] = 36.9647364798;
$s2h2['s']['w'] = 36.9922741528;
$s2h2['s']['x'] = 38.1213187486;
$s2h2['s']['y'] = 38.1213187486;
$s2h2['s']['z'] = 39.0208827354;
$s2h2['t']['a'] = 0;
$s2h2['t']['b'] = 2.78742446263;
$s2h2['t']['c'] = 2.79660368699;
$s2h2['t']['d'] = 2.8210816186;
$s2h2['t']['e'] = 2.82414136006;
$s2h2['t']['f'] = 5.5473112522;
$s2h2['t']['g'] = 5.5473112522;
$s2h2['t']['h'] = 5.5473112522;
$s2h2['t']['i'] = 8.23376424692;
$s2h2['t']['j'] = 9.73303755833;
$s2h2['t']['k'] = 9.73915704123;
$s2h2['t']['l'] = 9.74221678268;
$s2h2['t']['m'] = 9.75139600704;
$s2h2['t']['n'] = 9.75751548994;
$s2h2['t']['o'] = 9.7666947143;
$s2h2['t']['p'] = 11.9238124378;
$s2h2['t']['q'] = 11.9268721793;
$s2h2['t']['r'] = 11.9268721793;
$s2h2['t']['s'] = 15.9014763253;
$s2h2['t']['t'] = 15.9840893445;
$s2h2['t']['u'] = 15.9871490859;
$s2h2['t']['v'] = 17.2661210128;
$s2h2['t']['w'] = 17.315076876;
$s2h2['t']['x'] = 17.728141972;
$s2h2['t']['y'] = 17.7312017135;
$s2h2['t']['z'] = 18.107549912;
$s2h2['u']['a'] = 0;
$s2h2['u']['b'] = 0.0734337948443;
$s2h2['u']['c'] = 0.0917922435554;
$s2h2['u']['d'] = 0.0948519850073;
$s2h2['u']['e'] = 0.107090950815;
$s2h2['u']['f'] = 0.107090950815;
$s2h2['u']['g'] = 0.113210433718;
$s2h2['u']['h'] = 0.149927331141;
$s2h2['u']['i'] = 0.156046814044;
$s2h2['u']['j'] = 0.171345521303;
$s2h2['u']['k'] = 0.171345521303;
$s2h2['u']['l'] = 0.19888319437;
$s2h2['u']['m'] = 0.462020959229;
$s2h2['u']['n'] = 0.59664958311;
$s2h2['u']['o'] = 2.3254035034;
$s2h2['u']['p'] = 2.3254035034;
$s2h2['u']['q'] = 2.63137764859;
$s2h2['u']['r'] = 2.63137764859;
$s2h2['u']['s'] = 3.15459343685;
$s2h2['u']['t'] = 3.40855197736;
$s2h2['u']['u'] = 3.5645987914;
$s2h2['u']['v'] = 3.57989749866;
$s2h2['u']['w'] = 3.61049491318;
$s2h2['u']['x'] = 3.61049491318;
$s2h2['u']['y'] = 3.62273387899;
$s2h2['u']['z'] = 3.62579362044;
$s2h2['v']['a'] = 0;
$s2h2['v']['b'] = 1.33098753155;
$s2h2['v']['c'] = 1.33098753155;
$s2h2['v']['d'] = 1.33404727301;
$s2h2['v']['e'] = 1.34016675591;
$s2h2['v']['f'] = 3.2280272317;
$s2h2['v']['g'] = 3.23108697315;
$s2h2['v']['h'] = 3.23108697315;
$s2h2['v']['i'] = 3.2341467146;
$s2h2['v']['j'] = 5.15566434636;
$s2h2['v']['k'] = 5.15566434636;
$s2h2['v']['l'] = 5.15566434636;
$s2h2['v']['m'] = 5.20462020959;
$s2h2['v']['n'] = 5.20462020959;
$s2h2['v']['o'] = 5.20462020959;
$s2h2['v']['p'] = 5.7951503098;
$s2h2['v']['q'] = 5.7951503098;
$s2h2['v']['r'] = 5.7951503098;
$s2h2['v']['s'] = 5.79821005125;
$s2h2['v']['t'] = 5.79821005125;
$s2h2['v']['u'] = 5.8012697927;
$s2h2['v']['v'] = 5.90224126061;
$s2h2['v']['w'] = 5.90224126061;
$s2h2['v']['x'] = 5.90224126061;
$s2h2['v']['y'] = 5.90530100207;
$s2h2['v']['z'] = 5.90836074352;
$s2h2['w']['a'] = 0;
$s2h2['w']['b'] = 2.46615161019;
$s2h2['w']['c'] = 2.478390576;
$s2h2['w']['d'] = 2.478390576;
$s2h2['w']['e'] = 2.478390576;
$s2h2['w']['f'] = 3.82161707336;
$s2h2['w']['g'] = 3.82161707336;
$s2h2['w']['h'] = 3.82161707336;
$s2h2['w']['i'] = 5.36678650654;
$s2h2['w']['j'] = 7.73808613172;
$s2h2['w']['k'] = 7.73808613172;
$s2h2['w']['l'] = 7.73808613172;
$s2h2['w']['m'] = 7.74114587317;
$s2h2['w']['n'] = 7.74726535608;
$s2h2['w']['o'] = 7.75032509753;
$s2h2['w']['p'] = 9.1945230628;
$s2h2['w']['q'] = 9.19758280425;
$s2h2['w']['r'] = 9.19758280425;
$s2h2['w']['s'] = 9.53415436396;
$s2h2['w']['t'] = 9.53721410541;
$s2h2['w']['u'] = 9.54639332976;
$s2h2['w']['v'] = 9.57699074428;
$s2h2['w']['w'] = 9.58005048573;
$s2h2['w']['x'] = 9.58311022719;
$s2h2['w']['y'] = 9.58311022719;
$s2h2['w']['z'] = 9.67184272929;
$s2h2['x']['a'] = 0;
$s2h2['x']['b'] = 0.140748106785;
$s2h2['x']['c'] = 0.140748106785;
$s2h2['x']['d'] = 0.143807848237;
$s2h2['x']['e'] = 0.143807848237;
$s2h2['x']['f'] = 0.293735179377;
$s2h2['x']['g'] = 0.293735179377;
$s2h2['x']['h'] = 0.293735179377;
$s2h2['x']['i'] = 0.296794920829;
$s2h2['x']['j'] = 0.351870266962;
$s2h2['x']['k'] = 0.351870266962;
$s2h2['x']['l'] = 0.351870266962;
$s2h2['x']['m'] = 0.354930008414;
$s2h2['x']['n'] = 0.357989749866;
$s2h2['x']['o'] = 0.357989749866;
$s2h2['x']['p'] = 0.36410923277;
$s2h2['x']['q'] = 0.36410923277;
$s2h2['x']['r'] = 0.36410923277;
$s2h2['x']['s'] = 0.36410923277;
$s2h2['x']['t'] = 0.36410923277;
$s2h2['x']['u'] = 0.370228715674;
$s2h2['x']['v'] = 0.370228715674;
$s2h2['x']['w'] = 0.382467681481;
$s2h2['x']['x'] = 0.382467681481;
$s2h2['x']['y'] = 0.422244320355;
$s2h2['x']['z'] = 0.501797598103;
$s2h2['y']['a'] = 0;
$s2h2['y']['b'] = 0.3304520768;
$s2h2['y']['c'] = 0.336571559703;
$s2h2['y']['d'] = 0.336571559703;
$s2h2['y']['e'] = 0.336571559703;
$s2h2['y']['f'] = 0.976057523139;
$s2h2['y']['g'] = 0.976057523139;
$s2h2['y']['h'] = 0.982177006043;
$s2h2['y']['i'] = 0.988296488947;
$s2h2['y']['j'] = 1.02501338637;
$s2h2['y']['k'] = 1.02501338637;
$s2h2['y']['l'] = 1.02501338637;
$s2h2['y']['m'] = 1.03113286927;
$s2h2['y']['n'] = 1.03419261072;
$s2h2['y']['o'] = 1.03419261072;
$s2h2['y']['p'] = 1.24225502945;
$s2h2['y']['q'] = 1.2453147709;
$s2h2['y']['r'] = 1.24837451235;
$s2h2['y']['s'] = 1.25143425381;
$s2h2['y']['t'] = 1.25143425381;
$s2h2['y']['u'] = 1.26061347816;
$s2h2['y']['v'] = 1.38912261914;
$s2h2['y']['w'] = 1.39218236059;
$s2h2['y']['x'] = 1.39218236059;
$s2h2['y']['y'] = 1.39218236059;
$s2h2['y']['z'] = 1.39218236059;
$s2h2['z']['a'] = 0;
$s2h2['z']['b'] = 0.217241643081;
$s2h2['z']['c'] = 0.223361125985;
$s2h2['z']['d'] = 0.223361125985;
$s2h2['z']['e'] = 0.226420867437;
$s2h2['z']['f'] = 0.449781993422;
$s2h2['z']['g'] = 0.449781993422;
$s2h2['z']['h'] = 0.449781993422;
$s2h2['z']['i'] = 0.468140442133;
$s2h2['z']['j'] = 0.731278206992;
$s2h2['z']['k'] = 0.731278206992;
$s2h2['z']['l'] = 0.731278206992;
$s2h2['z']['m'] = 0.734337948443;
$s2h2['z']['n'] = 0.734337948443;
$s2h2['z']['o'] = 0.737397689895;
$s2h2['z']['p'] = 0.954639332976;
$s2h2['z']['q'] = 0.954639332976;
$s2h2['z']['r'] = 0.954639332976;
$s2h2['z']['s'] = 0.957699074428;
$s2h2['z']['t'] = 0.96075881588;
$s2h2['z']['u'] = 0.96075881588;
$s2h2['z']['v'] = 0.991356230399;
$s2h2['z']['w'] = 0.991356230399;
$s2h2['z']['x'] = 1.00359519621;
$s2h2['z']['y'] = 1.00359519621;
$s2h2['z']['z'] = 1.09844718121;
$s2h2['z']['{'] = 1.09844718121;

function str2hue($str) 
{
    global $s2h1,$s2h2;
    $str = preg_replace("`[^a-z]+`","",strtolower($str));
    $len = strlen($str);
    $hue = 0;
    if($len>0) {
        $hue += $s2h1[$str[0]];
        if($len>1) {
            $hue += $s2h2[$str[0]][$str[1]];
            $max = $s2h2[$str[0]][chr(ord($str[1])+1)] - $s2h2[$str[0]][$str[1]];
            for($i=2; $i<$len; $i++) {
                $mult = ord($str[$i])-ord('a');
                $hue += $max/pow(26,$len-$i)*$mult;
            }
        }
    }
    return $hue;
}
?>

Edit2: Missed that bit about you wanting words with similar beginning to have different colors... this does the exact opposite. I wrote this code so that when displaying an alphabetically sorted list of items, if there's a jump between colors, it means there's a jump between letters.

Aylmar answered 8/10, 2010 at 2:22 Comment(0)
A
0

Here is something a little more generalizable:

//Generate a six digit hex color from a string
private string StringToHexColor(string s)
{
    var md5 = MD5.Create();
    var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(s));
    var color = Color.FromArgb(hash[0], hash[1], hash[2]);
    Color colorNoAlpha = Color.FromArgb(0, color.R, color.G, color.B);
    return colorNoAlpha.ToArgb().ToString("X6");
}
Arraign answered 21/7 at 20:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.