如果您使用 C# 并想要 HSL 颜色
这是我根据Zeno Zeng 编写的库编写的课程,并在此答案中提到。
HSLColor 类是 Rich Newman 编写的,在这里定义
public class ColorHash
{
// you can pass in a string hashing function of you choice
public ColorHash(Func<string, int> hashFunction)
{
this.hashFunction = hashFunction;
}
// or use the default string.GetHashCode()
public ColorHash()
{
this.hashFunction = (string s) => { return s.GetHashCode(); };
}
Func<string, int> hashFunction = null;
static float[] defaultValues = new float[] { 0.35F, 0.5F, 0.65F };
// HSLColor class is defined at https://richnewman.wordpress.com/about/code-listings-and-diagrams/hslcolor-class/
public HSLColor HSL(string s) {
return HSL(s, defaultValues, defaultValues);
}
// HSL function ported from https://github.com/zenozeng/color-hash/
public HSLColor HSL(string s, float[] saturationValues, float[] lightnessValues)
{
double hue;
double saturation;
double luminosity;
int hash = Math.Abs(this.hashFunction(s));
hue = hash % 359;
hash = (int)Math.Ceiling((double)hash / 360);
saturation = saturationValues[hash % saturationValues.Length];
hash = (int)Math.Ceiling((double)hash / saturationValues.Length);
luminosity = lightnessValues[hash % lightnessValues.Length];
return new HSLColor(hue, saturation, luminosity);
}
}