That is a great solution - and here is a similiarly useful construct example I use for RGBA colors. Notice the UNION in the header.
In short - You can make "two structures" point to the same place in memory like Benjamin did, and access any and each part individually. Note that the compiler throws warnings but union structs have been in existance since (bell labs, AT&T) wrote the first c compiler.
jfc_rgba.h
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
#pragma once
#include <DarkGDK.h>
//----------------------------------------------------------------------------
// RGBA - Usage: uRGBA MyColor; MyColor.Red=100; MyColor.Pixel=(a dword)
//----------------------------------------------------------------------------
typedef union RGBA {
struct {
unsigned char Blue;
unsigned char Green;
unsigned char Red;
unsigned char Alpha;
};
DWORD Pixel;
} uRGBA;
//----------------------------------------------------------------------------
//============================================================================
// Begin JFC_RGBA
//============================================================================
class JFC_RGBA{
private:
void Init(void);
public:
JFC_RGBA();
JFC_RGBA(DWORD p_RGBA);
JFC_RGBA(unsigned char p_Red,unsigned char p_Green, unsigned char p_Blue, unsigned char p_Alpha );
JFC_RGBA(unsigned char p_Red,unsigned char p_Green, unsigned char p_Blue);
~JFC_RGBA();
uRGBA *RGBA;
void Red_Set(unsigned char p_Red);
void Green_Set(unsigned char p_Green);
void Blue_Set(unsigned char p_Blue);
void Alpha_Set(unsigned char p_Alpha);
void RGB_Set(unsigned char p_Red, unsigned char p_Green, unsigned char p_Blue);
void RGB_Set(DWORD p_RGB);
void RGBA_Set(unsigned char p_Red, unsigned char p_Green, unsigned char p_Blue, unsigned char p_Alpha);
void RGBA_Set(DWORD p_RGBA);
unsigned int Red_Get(void);
unsigned int Green_Get(void);
unsigned int Blue_Get(void);
unsigned int Alpha_Get(void);
DWORD RGB_Get(void);
DWORD RGBA_Get(void);
};
//============================================================================
// End JFC_RGBA
//============================================================================
jfc_rgba.cpp
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
#include "JFC_rgba.h"
//============================================================================
// begin JFC_RGB
//============================================================================
//----------------------------------------------------------------------------
void JFC_RGBA::Init(void){
//----------------------------------------------------------------------------
this->RGBA = new uRGBA();
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
JFC_RGBA::JFC_RGBA(){
//----------------------------------------------------------------------------
this->Init();
RGBA->Pixel=0;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
JFC_RGBA::JFC_RGBA(DWORD p_RGBA){
//----------------------------------------------------------------------------
this->Init();
this->RGBA_Set(p_RGBA);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
JFC_RGBA::JFC_RGBA(
unsigned char p_Red,
unsigned char p_Green,
unsigned char p_Blue,
unsigned char p_Alpha
){
//----------------------------------------------------------------------------
this->Init();
this->RGBA_Set(p_Red, p_Green,p_Blue, p_Alpha);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
JFC_RGBA::JFC_RGBA(
unsigned char p_Red,
unsigned char p_Green,
unsigned char p_Blue
){
//----------------------------------------------------------------------------
this->Init();
this->RGBA_Set(p_Red, p_Green,p_Blue,(unsigned char)255);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
JFC_RGBA::~JFC_RGBA(){
//----------------------------------------------------------------------------
delete RGBA;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_RGBA::Red_Set(unsigned char p_Red){
//----------------------------------------------------------------------------
this->RGBA->Red=p_Red;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_RGBA::Green_Set(unsigned char p_Green){
//----------------------------------------------------------------------------
this->RGBA->Green=p_Green;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_RGBA::Blue_Set(unsigned char p_Blue){
//----------------------------------------------------------------------------
this->RGBA->Blue=p_Blue;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_RGBA::Alpha_Set(unsigned char p_Alpha){
//----------------------------------------------------------------------------
this->RGBA->Alpha=p_Alpha;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_RGBA::RGB_Set(
unsigned char p_Red,
unsigned char p_Green,
unsigned char p_Blue
){
//----------------------------------------------------------------------------
this->RGBA->Red=p_Red;
this->RGBA->Green=p_Green;
this->RGBA->Blue=p_Blue;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_RGBA::RGB_Set(DWORD p_RGB){
//----------------------------------------------------------------------------
uRGBA Temp;
Temp.Pixel=p_RGB;
Temp.Alpha=this->RGBA->Alpha;
this->RGBA->Pixel=Temp.Pixel;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_RGBA::RGBA_Set(
unsigned char p_Red,
unsigned char p_Green,
unsigned char p_Blue,
unsigned char p_Alpha
){
//----------------------------------------------------------------------------
this->RGBA->Red=p_Red;
this->RGBA->Green=p_Green;
this->RGBA->Blue=p_Blue;
this->RGBA->Alpha=p_Alpha;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_RGBA::RGBA_Set(DWORD p_RGBA){
//----------------------------------------------------------------------------
this->RGBA->Pixel=p_RGBA;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
unsigned int JFC_RGBA::Red_Get(void){
//----------------------------------------------------------------------------
return this->RGBA->Red;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
unsigned int JFC_RGBA::Green_Get(void){
//----------------------------------------------------------------------------
return this->RGBA->Green;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
unsigned int JFC_RGBA::Blue_Get(void){
//----------------------------------------------------------------------------
return this->RGBA->Blue;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
unsigned int JFC_RGBA::Alpha_Get(void){
//----------------------------------------------------------------------------
return this->RGBA->Alpha;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
DWORD JFC_RGBA::RGB_Get(void){
//----------------------------------------------------------------------------
uRGBA Temp;
Temp.Pixel = this->RGBA->Pixel;
Temp.Alpha=0;
return Temp.Pixel;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
DWORD JFC_RGBA::RGBA_Get(void){
//----------------------------------------------------------------------------
return this->RGBA->Pixel;
};
//----------------------------------------------------------------------------
//============================================================================
// end JFC_RGB
//============================================================================