Download or view hashColor.frink in plain text format
/** This file contains methods to take a string and return a consistent color
that corresponds to that string. The string does not say anything about the
color; the color is returned from a hash code of the string. This is
basically a lazy way to obtain "random" colors that remain consistent across
runs of a program.
To obtain a different color scheme, you can append another value
consistently to each string. For example, appending " 16" to each string
can be found experimentally to give a good color scheme for your strings.
*/
/** This function turns a string into a repeatable color as a Frink color
object. */
hashColor[str] :=
{
[r,g,b] = hashColorToIntRGB[str]
return new color[r/255, g/255, b/255]
}
/** This function turns a string ito a repeatable color from 0 to 255 as
integers [r,g,b] */
hashColorToIntRGB[str] :=
{
hash = right[messageDigest[str, "MD5"], 6]
r = parseInt[substrLen[hash, 0, 2], 16]
g = parseInt[substrLen[hash, 2, 2], 16]
b = parseInt[substrLen[hash, 4, 2], 16]
return [r,g,b]
}
/** This function turns a string into a repeatable color as a hexadecimal string
like 'FF10CC". You may want to preface this with a pound sign # to make
this an HTML color.
*/
hashColorToHex[str] :=
{
[r,g,b] = hashColorToIntRGB[str]
return padLeft[hex[r], 2, "0"] + padLeft[hex[g], 2, "0"] + padLeft[hex[b], 2, "0"]
}
/**
g = new graphics
g.backgroundColor[hashColor["mars"]]
g.show[]
*/
Download or view hashColor.frink in plain text format
This is a program written in the programming language Frink.
For more information, view the Frink
Documentation or see More Sample Frink Programs.
Alan Eliasen, eliasen@mindspring.com