ColorHelper.h
Go to the documentation of this file.00001 00013 #ifndef __ColorHelper_H__ 00014 #define __ColorHelper_H__ 00015 00016 #include "StandardLibrary.h" 00017 00021 __forceinline unsigned short AlphaBlend16( 00022 unsigned short color1, 00023 unsigned short color2, 00024 unsigned char alpha 00025 ) 00026 { 00027 // These checks are needed to avoid the 16-bit related artefacts we get otherwise 00028 if (alpha<3) 00029 { 00030 return color1; 00031 } 00032 if (alpha>252) 00033 { 00034 return color2; 00035 } 00036 00037 00038 unsigned char invAlpha=255-alpha; 00039 return 00040 ((unsigned short)( 00041 ((((((unsigned int)color1 & 0xf800)>>11)*invAlpha)+(((unsigned int)color2 & 0xf800)>>11)*alpha)>>8) <<11 | 00042 ((((((unsigned int)color1 & 0x07e0)>>5 )*invAlpha)+(((unsigned int)color2 & 0x07e0)>>5 )*alpha)>>8) <<5 | 00043 ((((((unsigned int)color1 & 0x001f) )*invAlpha)+(((unsigned int)color2 & 0x001f) )*alpha)>>8) 00044 )); 00045 } 00046 00048 __forceinline unsigned int AlphaBlend32( 00049 unsigned int color1, 00050 unsigned int color2, 00051 unsigned char alpha 00052 ) 00053 { 00054 unsigned char invAlpha=255-alpha; 00055 return 00056 (((((color1 & 0x00ff0000)>>16)*invAlpha)+((color2 & 0xff0000)>>16)*alpha)>>8)<<16 | 00057 (((((color1 & 0x0000ff00)>>8 )*invAlpha)+((color2 & 0x00ff00)>>8 )*alpha)>>8)<<8 | 00058 (((((color1 & 0x000000ff) )*invAlpha)+((color2 & 0x0000ff) )*alpha)>>8) | 00059 (0xff000000); 00060 } 00061 00063 __forceinline unsigned short RGBModulate16( 00064 unsigned short color1, 00065 unsigned short color2 00066 ) 00067 { 00068 return 00069 ((unsigned short)( 00070 ((((((unsigned int)color1 & 0xf800)>>11)*(((unsigned int)color2 & 0xf800)>>11)))>>5) <<11 | 00071 ((((((unsigned int)color1 & 0x07e0)>>5 )*(((unsigned int)color2 & 0x07e0)>>5 )))>>6) <<5 | 00072 ((((((unsigned int)color1 & 0x001f) )*(((unsigned int)color2 & 0x001f) )))>>5) 00073 )); 00074 } 00075 00076 __forceinline unsigned short RGBAdd16( 00077 unsigned short color1, 00078 unsigned short color2 00079 ) 00080 { 00081 #define MIN(x,y) (x<y?x:y) 00082 return 00083 ((unsigned short)( 00084 MIN((((unsigned int)color1 & 0xf800)>>11)+(((unsigned int)color2 & 0xf800)>>11),0x1f) <<11 | 00085 MIN((((unsigned int)color1 & 0x07e0)>>5 )+(((unsigned int)color2 & 0x07e0)>>5 ),0x3f) <<5 | 00086 MIN((((unsigned int)color1 & 0x001f) )+(((unsigned int)color2 & 0x001f) ),0x1f) 00087 )); 00088 } 00089 00090 __forceinline unsigned short RGBSubtract16( 00091 unsigned short color1, 00092 unsigned short color2 00093 ) 00094 { 00095 #define MAX(x,y) (x>y?x:y) 00096 return 00097 ((unsigned short)( 00098 MAX((((int)color1 & 0xf800)>>11)-(((int)color2 & 0xf800)>>11),0) <<11 | 00099 MAX((((int)color1 & 0x07e0)>>5 )-(((int)color2 & 0x07e0)>>5 ),0) <<5 | 00100 MAX((((int)color1 & 0x001f) )-(((int)color2 & 0x001f) ),0) 00101 )); 00102 } 00103 00104 __forceinline unsigned short RGB16( 00105 unsigned char r, 00106 unsigned char g, 00107 unsigned char b 00108 ) 00109 { 00110 return 00111 ((unsigned short) ( 00112 ((((unsigned short)r) & 0xf8)<<8) | 00113 ((((unsigned short)g) & 0xfc)<<3) | 00114 ((((unsigned short)b) & 0xf8)>>3) 00115 )); 00116 } 00117 00118 00119 __forceinline unsigned int RGB32( 00120 unsigned char r, 00121 unsigned char g, 00122 unsigned char b 00123 ) 00124 { 00125 return 00126 ((unsigned int) ( 00127 0xff000000 | 00128 (((unsigned int)r)<<16) | 00129 (((unsigned int)g)<<8) | 00130 (((unsigned int)b)) 00131 )); 00132 } 00133 00134 00135 __forceinline unsigned int RGBA32( 00136 unsigned char r, 00137 unsigned char g, 00138 unsigned char b, 00139 unsigned char a 00140 ) 00141 { 00142 return 00143 ((unsigned int) ( 00144 (((unsigned int)a)<<24) | 00145 (((unsigned int)r)<<16) | 00146 (((unsigned int)g)<<8) | 00147 (((unsigned int)b)) 00148 )); 00149 } 00150 00151 00152 // Helper function for converting color values from 32 bit X8R8G8B8 to 16 bit R5G6B5 00153 __forceinline unsigned short RGB32TO16( 00154 unsigned int color 00155 ) 00156 { 00157 return 00158 ((unsigned short) ( 00159 ((color & 0x00f80000)>>8) | 00160 ((color & 0x0000fc00)>>5) | 00161 ((color & 0x000000f8)>>3) 00162 )); 00163 } 00164 00165 // Helper function for converting color values from 16 bit R5G6B5 to 32 bit X8R8G8B8 00166 __forceinline unsigned int RGB16TO32( 00167 unsigned short color 00168 ) 00169 { 00170 return 00171 (((unsigned int)color & 0xf800)<<8) | 00172 (((unsigned int)color & 0x07e0)<<5) | 00173 (((unsigned int)color & 0x001f)<<3) | 00174 (0xff000000); 00175 } 00176 00177 __forceinline unsigned int RGB16TO32( 00178 unsigned short color, 00179 unsigned char alpha 00180 ) 00181 { 00182 return 00183 (((unsigned int)color & 0xf800)<<8) | 00184 (((unsigned int)color & 0x07e0)<<5) | 00185 (((unsigned int)color & 0x001f)<<3) | 00186 (((unsigned int)alpha) <<24); 00187 } 00188 00189 // Clamp function used by colorhelper functions to clamp input values 00190 __forceinline float CH_CLAMP(float i) 00191 { 00192 if (i<0.0f) 00193 { 00194 i=0.0f; 00195 } 00196 00197 if (i>1.0f) 00198 { 00199 i=1.0f; 00200 } 00201 00202 return i; 00203 } 00204 00205 // Helper function for converting from float alpha value in the range 0 to 1 to an 8 bit value in the range 0 to 255 00206 __forceinline unsigned char FLOATTO8( 00207 float a 00208 ) 00209 { 00210 return ((unsigned char)(255.0f*CH_CLAMP(a))); 00211 } 00212 00213 00214 // Helper function for converting from r, g and b float values in the range 0 to 1 to 16 bit R5G6B5 00215 __forceinline unsigned short FLOATTORGB16( 00216 float r, 00217 float g, 00218 float b 00219 ) 00220 { 00221 return 00222 (((unsigned short)Round(31.0f*CH_CLAMP(r)))<<11) | 00223 (((unsigned short)Round(63.0f*CH_CLAMP(g)))<<5) | 00224 (((unsigned short)Round(31.0f*CH_CLAMP(b)))); 00225 } 00226 00227 // Helper function for converting from r, g and b float values in the range 0 to 1 to 32 bit X8R8G8B8 00228 __forceinline unsigned int FLOATTORGB32( 00229 float r, 00230 float g, 00231 float b 00232 ) 00233 { 00234 return 00235 (((unsigned int)Round(255.0f*CH_CLAMP(r)))<<16) | 00236 (((unsigned int)Round(255.0f*CH_CLAMP(g)))<<8) | 00237 (((unsigned int)Round(255.0f*CH_CLAMP(b)))) | 00238 0xff000000; 00239 } 00240 00241 // Helper function for converting from r, g, b and a float values in the range 0 to 1 to 32 bit A8R8G8B8 00242 __forceinline unsigned int FLOATTORGBA32( 00243 float r, 00244 float g, 00245 float b, 00246 float a 00247 ) 00248 { 00249 return 00250 (((unsigned int)(255.0f*CH_CLAMP(a)))<<24) | 00251 (((unsigned int)(255.0f*CH_CLAMP(r)))<<16) | 00252 (((unsigned int)(255.0f*CH_CLAMP(g)))<<8) | 00253 (((unsigned int)(255.0f*CH_CLAMP(b)))); 00254 } 00255 00256 __forceinline float RGB16TOFLOATR( 00257 unsigned short color 00258 ) 00259 { 00260 return (((float)((color & 0xf800)>>11))/((float)0x1f)); 00261 } 00262 00263 __forceinline float RGB16TOFLOATG( 00264 unsigned short color 00265 ) 00266 { 00267 return (((float)((color & 0x07e0)>>5))/((float)0x3f)); 00268 } 00269 00270 __forceinline float RGB16TOFLOATB( 00271 unsigned short color 00272 ) 00273 { 00274 return (((float)((color & 0x001f)))/((float)0x1f)); 00275 } 00276 00277 __forceinline float RGB32TOFLOATA( 00278 unsigned int color 00279 ) 00280 { 00281 return (((float)((color & 0xff000000)>>24))/((float)0xff)); 00282 } 00283 00284 00285 __forceinline float RGB32TOFLOATR( 00286 unsigned int color 00287 ) 00288 { 00289 return (((float)((color & 0x00ff0000)>>16))/((float)0xff)); 00290 } 00291 00292 __forceinline float RGB32TOFLOATG( 00293 unsigned int color 00294 ) 00295 { 00296 return (((float)((color & 0x0000ff00)>>8))/((float)0xff)); 00297 } 00298 00299 __forceinline float RGB32TOFLOATB( 00300 unsigned int color 00301 ) 00302 { 00303 return (((float)((color & 0x000000ff)))/((float)0xff)); 00304 } 00305 00306 #endif /* __ColorHelper_H__ */
Reproduction/republishing of any material on this site without permission is strictly prohibited.
