我結束了使用HSB到RGB轉換算法發現在http://www.adafruit.com/blog/2012/03/14/constant-brightness-hsb-to-rgb-algorithm/,我採用了inital(長)版本。也許這可以進一步優化,但爲了我的目的,這是完美的!
由於hsb2rgb方法是在C和我需要C#,我在這裏分享我的版本:
private byte[] hsb2rgb(int index, byte sat, byte bright)
{
int r_temp, g_temp, b_temp;
byte index_mod;
byte inverse_sat = (byte)(sat^255);
index = index % 768;
index_mod = (byte)(index % 256);
if (index < 256)
{
r_temp = index_mod^255;
g_temp = index_mod;
b_temp = 0;
}
else if (index < 512)
{
r_temp = 0;
g_temp = index_mod^255;
b_temp = index_mod;
}
else if (index < 768)
{
r_temp = index_mod;
g_temp = 0;
b_temp = index_mod^255;
}
else
{
r_temp = 0;
g_temp = 0;
b_temp = 0;
}
r_temp = ((r_temp * sat)/255) + inverse_sat;
g_temp = ((g_temp * sat)/255) + inverse_sat;
b_temp = ((b_temp * sat)/255) + inverse_sat;
r_temp = (r_temp * bright)/255;
g_temp = (g_temp * bright)/255;
b_temp = (b_temp * bright)/255;
byte[] color = new byte[3];
color[0] = (byte)r_temp;
color[1] = (byte)g_temp;
color[2] = (byte)b_temp;
return color;
}
要調用它的基礎上在原崗位掛鉤的代碼,我需要做一些小修改:
private byte[] SmoothColors1(int maxIterationCount, ref Complex z, int iteration)
{
double smoothcolor = iteration + 1 - Math.Log(Math.Log(z.Magnitude))/Math.Log(2);
byte[] color = hsb2rgb((int)(10 * smoothcolor), (byte)(255 * 0.6f), (byte)(255 * 1.0f));
if (iteration >= maxIterationCount)
{
// Make sure the core is black
color[0] = 0;
color[1] = 0;
color[2] = 0;
}
return color;
}