00001 /***************************************************************** 00002 * rgba.H 00003 * 00004 * RGBA conversions. 00005 * 00006 * Convenience methods to extract components of an RGBA 00007 * color value stored as a 4-byte unsigned int; or to return 00008 * the luminance of the color. 00009 * 00010 * The R, G, and B components store red, green, and blue, 00011 * respectively. The A component stores alpha 00012 * (transparency). All components are 8-bit unsigned 00013 * values. Alpha ranges from 0 (see-thru) to 255 (opaque). 00014 *****************************************************************/ 00015 #ifndef RGBA_H_IS_INCLUDED 00016 #define RGBA_H_IS_INCLUDED 00017 00018 typedef unsigned int uint; 00019 typedef unsigned char uchar; 00020 00021 #if defined(WIN32) || defined(i386) 00022 /**************************************** 00023 * Intel platform: colors stored as ABGR 00024 ****************************************/ 00025 inline uint rgba_to_r(uint c) { return c & 0xff; } 00026 inline uint rgba_to_g(uint c) { return (c>> 8) & 0xff; } 00027 inline uint rgba_to_b(uint c) { return (c>>16) & 0xff; } 00028 inline uint rgba_to_a(uint c) { return (c>>24); } 00029 inline uint build_rgba(uchar r, uchar g, uchar b, uchar a=255U) { 00030 return ((a<<24) | (b<<16) | (g<<8) | r); 00031 } 00032 00033 inline uint 00034 grey_to_rgba(uint c) 00035 { 00036 // treat c as an 8-bit value in range [0,255]. this is 00037 // enforced by masking off any bits outside the lowest 8 00038 return (0x10101*(c & 0xff) + 0xff000000); 00039 } 00040 00041 #else 00042 /**************************************** 00043 * Non-Intel platform: colors stored RGBA 00044 ****************************************/ 00045 inline uint rgba_to_r(uint c) { return (c>>24); } 00046 inline uint rgba_to_g(uint c) { return (c>>16) & 0xff; } 00047 inline uint rgba_to_b(uint c) { return (c>> 8) & 0xff; } 00048 inline uint rgba_to_a(uint c) { return c & 0xff; } 00049 inline uint build_rgba(uchar r, uchar g, uchar b, uchar a=255U) { 00050 return ((r<<24) | (g<<16) | (b<<8) | a); 00051 } 00052 00053 inline uint 00054 grey_to_rgba(uint c) 00055 { 00056 // treat c as an 8-bit value in range [0,255]. this is 00057 // enforced by masking off any bits outside the lowest 8 00058 return (0x1010100*(c & 0xff) + 255); 00059 } 00060 00061 #endif 00062 00063 /**************************************** 00064 * Both platforms 00065 ****************************************/ 00066 inline uint 00067 rgba_to_grey(uint c) 00068 { 00069 // return RGB luminance as a uint in range [0,255] 00070 return uint(.30*rgba_to_r(c) + .59*rgba_to_g(c) + .11*rgba_to_b(c)); 00071 } 00072 00073 // return componenents as doubles in range [0,1]: 00074 inline double rgba_to_r_d(uint c) { return rgba_to_r(c)/255.0; } 00075 inline double rgba_to_g_d(uint c) { return rgba_to_g(c)/255.0; } 00076 inline double rgba_to_b_d(uint c) { return rgba_to_b(c)/255.0; } 00077 inline double rgba_to_a_d(uint c) { return rgba_to_a(c)/255.0; } 00078 00079 inline double 00080 rgba_to_grey_d(uint c) 00081 { 00082 // return RGB luminance as a double in range [0,1] 00083 return (.30*rgba_to_r(c) + .59*rgba_to_g(c) + .11*rgba_to_b(c))/255.0; 00084 } 00085 00086 inline uint 00087 rgba_to_tone(uint c) 00088 { 00089 // the same as rgba_to_grey() except we take into account the 00090 // transparency. return value is a uint in range [0,255]: 00091 return uint(rgba_to_a_d(c)*rgba_to_grey(c)); 00092 } 00093 00094 inline double 00095 rgba_to_tone_d(uint c) 00096 { 00097 // same as rgba_to_tone() except return value is a double in 00098 // range [0,1]: 00099 return rgba_to_grey_d(c) * rgba_to_a_d(c); 00100 } 00101 00102 #endif // RGBA_H_IS_INCLUDED 00103 00104 /* end of file rgba.H */ 00105 00106 00107 00108 00109