geColor

From GriffinEngine
Jump to: navigation, search

color datatype is used to store 32-bit RGBA color value. Technically this is implemented by packing four 8-bit channels into one 32-bit unsigned int. A large assortment of conversion and manipulation methods are provided in geColor.h.

Unlike float3 and float4, color variables are not objects, so there is no need to use pointers or to manually allocate/free them. Treat color variables as primitive types (e.g. int, float, etc). Any temporary color variables created in nested methods are allocated on the stack, and will be freed automatically.

Example Usage

New color from bytes/decimals

Note: alpha defaults to 1.0, meaning 100% opacity).
 color orange = colorFromByte( 255, 127, 0 );

New color, translucent (50%), made from float's

 color aqua = colorFromFloat( 0.0, 1.0, 1.0, 0.5 );

Same opaque orange color as above, but read from a string

 color orange2 = colorFromHex( "FF6600FF" );

The most efficient way to define color: hex

Note: first 6 digits are R/G/B, the last 2 digits are alpha. Do not omit any of the digits.
 color orange3 = 0xFF6600FF;

Create new color (white) from a float3 object.

Note: If you place "new float3()" directly inside "colorFromFloat3()", it would create a memory leak, since the float3 would never be deleted/freed.
 float3* f3 = new float3(1,1,1);
 color c = colorFromFloat3( f3 );
 delete f3;

Copy pixel (x,y) from bitmap1 to bitmap2, replacing opacity with 100%

 bitmap2->set(x,y, colorSetA( bitmap1->get(x,y), 1 ) );


Reference

Constants

  •  #define BYTE_TO_FLOAT 0.00392156863f
  •  typedef unsigned int color; // the color datatype
  •  #define GE_BLACK 0x00000000
  •  #define GE_WHITE 0xFFFFFFFF

Reading individual components/channels

  •  BYTE colorR( color c );
  •  BYTE colorG( color c );
  •  BYTE colorB( color c );
  •  BYTE colorA( color c );
  •  float colorRf( color c );
  •  float colorGf( color c );
  •  float colorBf( color c );
  •  float colorAf( color c );

Writing individual components/channels

  •  color colorSetR( color c, BYTE r );
  •  color colorSetG( color c, BYTE g );
  •  color colorSetB( color c, BYTE b );
  •  color colorSetA( color c, BYTE a );
  •  color colorSetRf( color c, float r );
  •  color colorSetGf( color c, float g );
  •  color colorSetBf( color c, float b );
  •  color colorSetAf( color c, float a );

Conversion to color

  •  color colorFromBytev( const BYTE* bp, int bpp = 3 );               // from byte array (specify the number of channels)
  •  color colorFromByte( BYTE r, BYTE g, BYTE b, BYTE a=0xFF );        // from individual bytes
  •  color colorFromFloatv( const float* fv, int bpp = 3 );             // from float array (specify the number of channels)
  •  color colorFromFloat( float r, float g, float b, float a=1.0f );   // from individual floats
  •  color colorFromDoublev( const double* dv, int bpp = 3 );           // from double array (specify the number of channels)
  •  color colorFromDouble( double r, double g, double b, double a=1 ); // from individual doubles
  •  color colorFromFloat3( const float3* f );           // from float3 object (don't forget to delete it)
  •  color colorFromFloat4( const float4* f );           // from float4 object (don't forget to delete it)
  •  color colorFromString( const string hexString );    // from a string of 8 hexadecimal characters (e.g. "FF6600FF")

Conversion from color

  •  BYTE* colorToBytev( color c );              // to new byte array (don't forget to delete[] it)
  •  BYTE* colorToBytev( color c, BYTE* bv );    // to existing byte array
  •  float* colorToFloatv( color c );            // to new float array (don't forget to delete[] it)
  •  float* colorToFloatv( color c, float* fv ); // to existing float array
  •  float3* colorToFloat3( color c );           // to new float3 object (don't forget to delete it)
  •  void colorToFloat3( float3* f, color c );   // to existing float3 object
  •  float4* colorToFloat4( color c );           // to new float4 object (don't forget to delete it)
  •  void colorToFloat4( float4* f, color c );   // to existing float4 object
  •  string colorToString( color c );            // to hex string (format "%08X", e.g. "FF6600FF")

Debugging

  •  void colorPrintByte( color c );  // print color information, as integers: "(%d,%d,%d,%d)"
  •  void colorPrintHex( color c );   // print color information, as hex: "%08X"
  •  void colorPrintFloat( color c ); // print color information, as floats: "(%1.3f,%1.3f,%1.3f,%1.3f)"
  •  void colorPrint( color c );      // print color in all 3 formats at once