我有一个名为 Color 的类,它实现了以下覆盖:
public override Int32 GetHashCode()
{
unchecked
{
Int32 hash = 23;
hash = (hash * 37) + m_Changes.GetHashCode();
hash = (hash * 37) + m_Blue;
hash = (hash * 37) + m_Green;
hash = (hash * 37) + m_Random;
hash = (hash * 37) + m_RandomBlue;
hash = (hash * 37) + m_RandomGreen;
hash = (hash * 37) + m_RandomRed;
hash = (hash * 37) + m_Red;
return hash;
}
}
我正在尝试缓存结果以减少计算:
public static Color Average(Color left, Color right, Double weight)
{
Color value;
Int32 key = left.GetHashCode() ^ right.GetHashCode() ^ weight.GetHashCode();
if (!s_Averages.TryGetValue(key, out value))
{
Double complement = 100.0 - weight;
Int32 red = (Int32)(((left.Red * complement) + (right.Red * weight)) / 100.0);
Int32 green = (Int32)(((left.Green * complement) + (right.Green * weight)) / 100.0);
Int32 blue = (Int32)(((left.Blue * complement) + (right.Blue * weight)) / 100.0);
Int32 random = (Int32)(((left.Random * complement) + (right.Random * weight)) / 100.0);
Int32 randomRed = (Int32)(((left.RandomRed * complement) + (right.RandomRed * weight)) / 100.0);
Int32 randomGreen = (Int32)(((left.RandomGreen * complement) + (right.RandomGreen * weight)) / 100.0);
Int32 randomBlue = (Int32)(((left.RandomBlue * complement) + (right.RandomBlue * weight)) / 100.0);
value = new Color(red, green, blue, randomRed, randomGreen, randomBlue, random, (left.Changes || right.Changes));
s_Averages.Add(key, value);
}
return value;
}
结果不好,因为我在屏幕上绘制平均颜色时得到了错误的像素。如果我将该方法恢复为无缓存版本,一切正常:
public static Color Average(Color left, Color right, Double weight)
{
Double complement = 100.0 - weight;
Int32 red = (Int32)(((left.Red * complement) + (right.Red * weight)) / 100.0);
Int32 green = (Int32)(((left.Green * complement) + (right.Green * weight)) / 100.0);
Int32 blue = (Int32)(((left.Blue * complement) + (right.Blue * weight)) / 100.0);
Int32 random = (Int32)(((left.Random * complement) + (right.Random * weight)) / 100.0);
Int32 randomRed = (Int32)(((left.RandomRed * complement) + (right.RandomRed * weight)) / 100.0);
Int32 randomGreen = (Int32)(((left.RandomGreen * complement) + (right.RandomGreen * weight)) / 100.0);
Int32 randomBlue = (Int32)(((left.RandomBlue * complement) + (right.RandomBlue * weight)) / 100.0);
return (new Color(red, green, blue, randomRed, randomGreen, randomBlue, random, (left.Changes || right.Changes)));
}
这只能意味着我使用 GetHashCode 生成的密钥不是唯一的。如何管理这种缓存,为我的颜色平均值获取唯一键?