没有Math.Cos的HSI到RGB()

时间:2014-07-31 17:34:47

标签: colors converter rgb

问题就是这么说。我正在寻找一种算法将HSI(不是HSL而不是HSV)转换为RGB,假设所有的H,S和I都是> 0和< 1。

我希望我可以产生类似:http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV的东西但是对于HSI没有使用余弦函数。我不太确定是否有可能。有没有办法计算HSI的C和m(如维基百科HSV和HSL文章所述)?

感谢。

仅供参考,我正在使用此函数将RGB转换为HSI(所以我试图反过来):

 public static void toHSI(byte R, byte G, byte B, out double H, out double S, out double I)
    {
        byte MAX, MIN;
        if (R > G) { if (R > B) { MAX = R; MIN = (G < B ? G : B); } else { MAX = B; MIN = (G < R ? G : R); } }
        else { if (G > B) { MAX = G; MIN = (R < B ? R : B); } else { MAX = B; MIN = (G < R ? G : R); } }

        I = (double)(R + G + B) / 765;

        if (I == 0) { H = S = 0; } // achromatic
        else
        {
            double DIF = (double)(MAX - MIN);
            S = 1 - (double)MIN / (255 * I);
            if (MAX == R) { H = (double)(G - B) / DIF + (G < B ? 6 : 0); }
            else if (MAX == G) { H = (double)(B - R) / DIF + 2; }
            else { H = (double)(R - G) / DIF + 4; }
            H /= 6;
        }
    }

1 个答案:

答案 0 :(得分:0)

你可以尝试这个我不确定它是否有用,但你需要将函数中的值视为浮点数。 Hue是0-360之间的数字。饱和度为0.00 -1.00,介于两者之间。还使用了Intensity aka Value,也是0.00 -1.00的值以及介于两者之间的任何值。函数的结果是一个数组的三个值,然后可以用于值为0-255的rgb颜色空间。

///I use this

// the function result will be the values of the array rgb[3] and will be the  rgb values 0-255

///float H is values 0-360 because there are 360 degrees of color in hsi colorspace
///float S is 0.00 - 1.00 and aything in between
///float I is 0.00 - 1.00 and aything in between
///The input to our function is going to be hsi_to_rgb (Hue, Saturation, Intensity(brightness))

int rgb[3]; ///number of channels rgb = 3

void hsi_to_rgb(float H, float S, float I) {
  int r, g, b;
  if (H > 360) {
    H = H - 360;
  }
  H = fmod(H, 360); // cycle H around to 0-360 degrees
  H = 3.14159 * H / (float)180; // Convert to radians.
  S = S > 0 ? (S < 1 ? S : 1) : 0; // clamp S and I to interval [0,1]
  I = I > 0 ? (I < 1 ? I : 1) : 0;
  if (H < 2.09439) {
    r = 255 * I / 3 * (1 + S * cos(H) / cos(1.047196667 - H));
    g = 255 * I / 3 * (1 + S * (1 - cos(H) / cos(1.047196667 - H)));
    b = 255 * I / 3 * (1 - S);
  } else if (H < 4.188787) {
    H = H - 2.09439;
    g = 255 * I / 3 * (1 + S * cos(H) / cos(1.047196667 - H));
    b = 255 * I / 3 * (1 + S * (1 - cos(H) / cos(1.047196667 - H)));
    r = 255 * I / 3 * (1 - S);
  } else {
    H = H - 4.188787;
    b = 255 * I / 3 * (1 + S * cos(H) / cos(1.047196667 - H));
    r = 255 * I / 3 * (1 + S * (1 - cos(H) / cos(1.047196667 - H)));
    g = 255 * I / 3 * (1 - S);
  }
  //set the output to the array
  rgb[0] = r;
  rgb[1] = g;
  rgb[2] = b;

}