Description
This example shows how obtain both the decimal and the hexadeximal
representation of given color. The color whose representation we want
to obtain is selected from a TColorDialog component. All components on the form
are created dynamically.
ColorRepresentation.cpp
ColourRepresentation.h
Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.
We use the _ltoa() function to convert the TColor values into decimal and
hexadecimal values. Notice that this function is part of neither ANSI C++
nor ANSI C.
#include <vcl.h>
#pragma hdrstop
#include "ColorRepresentation.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
TColorDialog* ColorDialog1;
TButton* Button1;
void Get_ColorRepresentation( TColorDialog* p )
{
TColor color;
char s_deci_number[25];
char s_hexa_number[25];
char s_DECI[50] = "DECIMAL: ";
char s_HEXA[50] = "HEXADECIMAL: ";
if ( p->Execute() )
{
color = p->Color;
}
_ltoa( (long)color, s_deci_number, 10 );
_ltoa( (long)color, s_hexa_number, 16 );
strcat( s_DECI, s_deci_number );
strcat( s_DECI, "\n" );
strcat( s_HEXA, s_hexa_number );
strcat(s_DECI, s_HEXA );
Application->MessageBox( s_DECI, "Color Representation", MB_OK );
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
//Set Form parameters
Width = 800;
Height = 600;
Position = poScreenCenter;
Color = clBlack;
//Create a Color Dialog:
ColorDialog1 = new TColorDialog( this );
//Create Button
Button1 = new TButton( this );
Button1->Parent = this;
Button1->Top = 50;
Button1->Width = 200;
Button1->Height = 100;
Button1->Left = ( Width - Button1->Width )/2;
Button1->Caption = "Show Color Representation";
Button1->OnClick = Button1Click;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Get_ColorRepresentation( ColorDialog1 );
}
#ifndef ColorRepresentationH
#define ColorRepresentationH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Dialogs.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif