Color Decoding

 

 

Colors and the API Functions

A color is  a long integer that is made of a red, a green, and a blue values. The Win32 API provides three valuable functions that allow you to retrieve these values if you know the integral value of a color.

Suppose you know the complete integral value of a color. To retrieve its Red value, you can use the GetRValue() function. In the same way, you can use the GetGValue() and the GetBValue() functions to get the Green and the Blue values of the color. These functions can be applied whether the color value is in decimal or hexadecimal value. Here is an example:

 

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    long Clr = Edit1->Text.ToInt();

    int R = GetRValue(Clr);
    int G = GetGValue(Clr);
    int B = GetBValue(Clr);
}
//---------------------------------------------------------------------------

In VCL applications, the GetRValue(), GetGValue(), and GetBValue() functions can be applied on any valid TColor value.

Explicit Decoding

You can also decode a color if for some reason you cannot use the above functions. This can be accomplished using the & bit manipulation operator. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    int r, g, b;
    TColor Clr = ColorBox1->Selected;

    r =  Clr & 0xFF;
    g = (Clr & 0xFF00) / 0x100;
    b = (Clr & 0xFF0000) / 0x10000;

    Edit1->Text = r;
    Edit2->Text = g;
    Edit3->Text = b;

    int R = GetRValue(Clr);
    int G = GetGValue(Clr);
    int B = GetBValue(Clr);

    Edit5->Text = R;
    Edit6->Text = G;
    Edit7->Text = B;
}
//---------------------------------------------------------------------------

 

 

 


Copyright 2003-2007 FunctionX, Inc.