NVidia Gameworks
  • Main Page
  • Classes
  • Files
  • File List
  • File Members

NvPackedColor.h

Go to the documentation of this file.
00001 // TAGRELEASE: PUBLIC
00002 
00003 #ifndef _NV_PACKEDCOLOR_H
00004 #define _NV_PACKEDCOLOR_H
00005 
00006 #include <NvFoundation.h>
00007 
00013 #define NV_PACK_COLOR_CHANNELS(r,g,b,a)    ( ( (((uint32_t)(a))&0xFF)<<24 ) | ( (((uint32_t)(b))&0xFF)<<16 ) | ( (((uint32_t)(g))&0xFF)<<8 ) | ( (((uint32_t)(r))&0xFF) ) )
00014 
00016 #define NV_PC_CHANNEL_TO_FLOAT(ch)   ( ch / 255.0f )
00017 
00018 //#define NV_PACKEDCOLOR_AS_STRUCT
00019 #ifdef NV_PACKEDCOLOR_AS_STRUCT // uses a struct to break backwards-compat and accidental uses
00020 
00021 class NvPackedColor
00022 {
00023 private:
00024     uint8_t r;
00025     uint8_t g;
00026     uint8_t b;
00027     uint8_t a;
00028 
00029     uint32_t mColorPacked;
00030 
00031 public:
00032     NvPackedColor()
00033     : r(0), g(0), b(0), a(0), mColorPacked(0)
00034     { /* no-op */ }
00035 
00036     NvPackedColor(uint8_t ir, uint8_t ig, uint8_t ib, uint8_t ia)
00037     : r(ir), g(ig), b(ib), a(ia)
00038     {
00039         mColorPacked = NV_PACK_COLOR_CHANNELS(r, g, b, a);
00040     };
00041 
00042 /* TBD uncomment when we NEED this variant...
00043     NvPackedColor(float ir, float ig, float ib, float ia)
00044     : r((uint8_t)(255*ir))
00045     , g((uint8_t)(255*ig))
00046     , b((uint8_t)(255*ib))
00047     , a((uint8_t)(255*ia))
00048     {
00049         mColorPacked = NV_PACK_COLOR_CHANNELS(r, g, b, a);
00050     };
00051 */
00052 
00053     bool operator==(const NvPackedColor& pc) {
00054         return mColorPacked==pc.mColorPacked;
00055     };
00056 
00057     void setAlpha(uint8_t ia) {
00058         a = ia;
00059         // just repack it, not done that often, not that much extra effort...
00060         mColorPacked = NV_PACK_COLOR_CHANNELS(r, g, b, a);
00061     };
00062 
00063     uint8_t red() { return r; };
00064     uint8_t green() { return g; };
00065     uint8_t blue() { return b; };
00066     uint8_t alpha() { return a; };
00067 
00068     float redFloat() { return NV_PC_CHANNEL_TO_FLOAT(r); };
00069     float greenFloat() { return NV_PC_CHANNEL_TO_FLOAT(g); };
00070     float blueFloat() { return NV_PC_CHANNEL_TO_FLOAT(b); };
00071     float alphaFloat() { return NV_PC_CHANNEL_TO_FLOAT(a); };
00072 
00073     bool isWhite() { return (r==0xff)&&(g==0xFF)&&(b=0xFF); };
00074 
00075     uint32_t getPackedLong() { return mColorPacked; }
00076 };
00077 
00078 static const NvPackedColor static_nvpcwhite((uint8_t)0xFF,0xFF,0xFF,0xFF);
00079 static const NvPackedColor static_nvpcblack((uint8_t)0x00,0x00,0x00,0xFF);
00080 
00081 #define NV_PACKED_COLOR(r, g, b, a)     NvPackedColor((uint8_t)(r),(uint8_t)(g),(uint8_t)(b),(uint8_t)(a))
00082 
00083 #define NV_PC_PREDEF_WHITE    static_nvpcwhite
00084 #define NV_PC_PREDEF_BLACK    static_nvpcblack
00085 
00086 #define NV_PC_RED(c)        ( c.red() )
00087 #define NV_PC_GREEN(c)      ( c.green() )
00088 #define NV_PC_BLUE(c)       ( c.blue() )
00089 #define NV_PC_ALPHA(c)      ( c.alpha() )
00090 
00091 #define NV_PC_RED_FLOAT(c)        ( c.redFloat() )
00092 #define NV_PC_GREEN_FLOAT(c)      ( c.greenFloat() )
00093 #define NV_PC_BLUE_FLOAT(c)       ( c.blueFloat() )
00094 #define NV_PC_ALPHA_FLOAT(c)      ( c.alphaFloat() )
00095 
00096 #define NV_PC_SET_ALPHA(c, a)     ( c.setAlpha(a) )
00097 
00098 #define NV_PC_PACK_UINT(c)  ( c.getPackedLong() )
00099 
00100 #define NV_PC_IS_WHITE(c) ( c.isWhite() )
00101 
00102 #define NV_PC_EQUAL(x,y)      ( x==y )
00103 
00104 #else /* code that doesn't break old pass-as-uint stuff */
00105 
00107 typedef uint32_t NvPackedColor;
00108 
00110 #define NV_PACKED_COLOR(r,g,b,a)    NV_PACK_COLOR_CHANNELS((r),(g),(b),(a))
00111 
00113 #define NV_PC_PREDEF_WHITE    NV_PACKED_COLOR(0xFF, 0xFF, 0xFF, 0xFF)
00114 
00115 #define NV_PC_PREDEF_BLACK    NV_PACKED_COLOR(0x00, 0x00, 0x00, 0xFF)
00116 
00118 #define NV_PC_RED(c)        ( ( c>>0 ) & 0xff )
00119 
00120 #define NV_PC_GREEN(c)      ( ( c>>8 ) & 0xff )
00121 
00122 #define NV_PC_BLUE(c)       ( ( c>>16 ) & 0xff )
00123 
00124 #define NV_PC_ALPHA(c)      ( ( c>>24 ) & 0xff )
00125 
00127 #define NV_PC_RED_FLOAT(c)        ( NV_PC_RED(c)/255.0f )
00128 
00129 #define NV_PC_GREEN_FLOAT(c)      ( NV_PC_GREEN(c)/255.0f )
00130 
00131 #define NV_PC_BLUE_FLOAT(c)       ( NV_PC_BLUE(c)/255.0f )
00132 
00133 #define NV_PC_ALPHA_FLOAT(c)      ( NV_PC_ALPHA(c)/255.0f )
00134 
00136 #define NV_PC_SET_ALPHA(c, a)     ( ((c)&0xFFFFFF) & ((((uint32_t)(a))&0xFF)<<24) )
00137 
00141 #define NV_PC_PACK_UINT(c)  (c)
00142 
00144 // This is the quick-cheat-check...
00145 #define NV_PC_IS_WHITE(c)   (((c)&0xFFFFFF) == 0xFFFFFF )
00146 
00148 #define NV_PC_EQUAL(x,y)    (x==y)
00149 #endif
00150 
00151 #endif // _NV_PACKEDCOLOR_H 
Generated on Sat Mar 8 14:58:35 2014 for NVIDIA GameWorks OpenGL App Framework and Libraries by Doxygen
©2014 NVIDIA Corporation.