[转]各种颜色相互转换算法的C语言源代码
View Code
1 //RGB to CMYk 2 int* rgb2cmyk(int R,int G,int B) 3 { 4 int cmyk [4]; 5 6 cmyk[3]=(int)(min(min(255-R,255-G),255-B)/2.55);//cmykK 7 int MyR = (int)(R/2.55); 8 int Div = 100-cmyk[3]; 9 if (Div == 0)Div = 1; 10 cmyk[0] = ((100-MyR-cmyk[3])/Div)*100;//cmykC 11 int MyG = (int)(G/2.55); 12 cmyk[1] = ((100-MyG-cmyk[3])/Div)*100; 13 int MyB = (int)(B/2.55); 14 cmyk[2] = ((100-MyB-cmyk[3])/Div)*100; 15 16 return cmyk; 17 } 18 19 20 //CMYK to RGB 21 int* cmyk2rgb(int C,int M,int Y,int K) 22 { 23 int rgb[3]; 24 int *R=&rgb[0]; 25 int *G=&rgb[1]; 26 int *B=&rgb[2]; 27 28 float MyC = C/100; 29 float MyM = M/100; 30 float MyY = Y/100; 31 float MyK = K/100; 32 33 int *R = (int)((1-(MyC*(1-MyK)+MyK))*255); 34 int *G = (int)((1-(MyM*(1-MyK)+MyK))*255); 35 int *B = (int)((1-(MyY*(1-MyK)+MyK))*255); 36 37 if (*R<0) *R=0; 38 if (*G<0) *G=0; 39 if (*B<0) *B=0; 40 if (*R>255) *R=255; 41 if (*G>255) *G=255; 42 if (*B>255) *B=255; 43 44 return rgb; 45 } 46 47 48 //RGB to YUV 49 int* rgb2yuv(int R,int G,int B) 50 { 51 int yuv[3]; 52 53 yuv[0] = (int)(0.299*R+0.587*G+0.114*B); 54 yuv[1] = (int)(-0.147*R-0.289*G+0.437*B); 55 yuv[2] = (int)(0.615*R-0.515*G-0.1*B); 56 57 return yuv; 58 } 59 60 61 //YUV to RGB 62 int* yuv2rgb(int y,int u,int v) 63 { 64 int rgb[3]; 65 int *R=&rgb[0]; 66 int *G=&rgb[1]; 67 int *B=&rgb[2]; 68 69 *R = (int)(y+1.14*v); 70 *G = (int)(y-0.394*u-0.581*v); 71 *B = (int)(y+2.028*u); 72 73 if (*R<0) *R=0; 74 if (*G<0) *G=0; 75 if (*B<0) *B=0; 76 if (*R>255) *R=255; 77 if (*G>255) *G=255; 78 if (*B>255) *B=255; 79 80 return rgb; 81 } 82 83 84 //RGB to YIQ 85 int* rgb2yiq(int R,int G,int B) 86 { 87 int yiq[3]; 88 89 yiq[0] = (int)(0.299*R+0.587*G+0.114*B); 90 yiq[1] = (int)(0.596*R-0.274*G-0.322*B); 91 yiq[2] = (int)(0.212*R-0.523*G+0.311*B); 92 93 return yiq; 94 } 95 96 97 //YIQ to RGB 98 int* yiq2rgb(float y,float i,float q) 99 { 100 int rgb[3]; 101 int *R=&rgb[0]; 102 int *G=&rgb[1]; 103 int *B=&rgb[2]; 104 105 *R = (int)(y+0.956*i+0.621*q); 106 *G = (int)(y-0.272*i-0.647*q); 107 *B = (int)(y-1.105*i+1.702*q); 108 109 if (*R<0) *R=0; 110 if (*G<0) *G=0; 111 if (*B<0) *B=0; 112 if (*R>255) *R=255; 113 if (*G>255) *G=255; 114 if (*B>255) *B=255; 115 116 return rgb; 117 } 118 119 //RGB to YCbCr 120 int* rgb2ycbcr(int R, int G,int B) 121 { 122 int ycbcr[3]; 123 124 ycbcr[0] = (int)(0.299*R+0.587*G+0.114*B); 125 126 ycbcr[1] = (int)(-0.1687*R-0.3313*G+0.5*B+128); 127 if (ycbcr[1] > 255) ycbcr[1] = 255; 128 129 ycbcr[2] = (int)(0.5*R-0.4187*G-0.0813*B+128); 130 if (ycbcr[2] > 255) ycbcr[2] = 255; 131 132 return ycbcr; 133 } 134 135 136 //YCbCr to RGB 137 int* ycbcr2rgb(int Y,int Cb,int Cr) 138 { 139 int rgb[3]; 140 int *R=&rgb[0]; 141 int *G=&rgb[1]; 142 int *B=&rgb[2]; 143 144 *R = (int)(Y + 1.402 * (Cr -128)); 145 *G = (int)(Y - 0.34414 * (Cb - 128) - 0.71414 * (Cr - 128)); 146 *B = (int)(Y + 1.772 * (Cb -128)); 147 148 if (*R > 255) *R = 255; 149 if (*G > 255) *G = 255; 150 if (*B > 255) *B = 255; 151 152 if (*R < 0) *R =0; 153 if (*G < 0) *G =0; 154 if (*B < 0) *B =0; 155 156 return rgb; 157 }