C++ Builder

La Isla Bonita



Color Functions: ColorToString and ColorToRGB
Color Functions: ColorToString and ColorToRGB


Description

In this example I will show how to get the "TColors" colors corresponding to the TColors type (such as clBlack, clRed, clGreen, etc) and their hexadecimal representation.
We will use the VCL color functions: ColorToString() and ColorToRGB(). Two similar methods are developed. Both methods use the same VCL color functions but one method uses AnsiStrings (method 1) while the other one uses C strings (method 2).
All controls on the Form1 are created at runtime: (2 Tbuttons and 2 TLabels).

Code

VCLColorFunctions.cpp


#include <vcl.h>
#include 
#pragma hdrstop

#include "VCLColorFunctions.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;
const int DX_SEPARATION = 50;
const int DY_SEPARATION = 25;

TButton* b1;
TButton* b2;
TLabel*  l1;
TLabel*  l2;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    Color  = clTeal;
    Width  = 600;
    Height = 500;

    b1 = new TButton(this);
    b1->Parent = this;
    b1->Width  = 200;
    b1->Height = 100;
    b1->Top    = 50;
    b1->Left   = Width/2 - b1->Width - DX_SEPARATION;
    b1->Caption = "Scan Colors (AnsiString)";
    b1->OnClick = Button1Click;

    b2 = new TButton(this);
    b2->Parent = this;
    b2->Width  = b1->Width;
    b2->Height = b1->Height;
    b2->Top    = b1->Top;
    b2->Left   = Width/2 + DX_SEPARATION;
    b2->Caption = "Scan Colors (C str)";
    b2->OnClick = Button2Click;

    l1 = new TLabel(this);
    l1->Parent = this;
    l1->Caption = "TColor : Hexadecimal";
    l1->Width  = 200;
    l1->Height = 100;
    l1->Top    = b1->Top + b1->Height + DY_SEPARATION;
    l1->Left   = Width/2 - l1->Width  - DX_SEPARATION;

    l2 = new TLabel(this);
    l2->Parent = this;
    l2->Caption = "TColor : Hexadecimal";
    l2->Width  = 200;
    l2->Height = 100;
    l2->Top    = l1->Top;
    l2->Left   = Width/2 + DX_SEPARATION;
}
//---------------------------------------------------------------------------

String GetTColors()
{
    // Method 1: AnsiStrings
    const int MAX = 16777215;  //0xffffff
    String colors;
    String stmp;

    for (int i=0; i<= MAX; ++i)
    {
       stmp = ColorToString((TColor)i);

       // AnsiStrings are one-based.
       // TColors start with cl, so we have a TColor when
       // the second element of the AnsiString (index 2) is 'l'
       if (stmp[2] == 'l' )
           colors += stmp + " " +
                     IntToHex(ColorToRGB((TColor)i), 6) + "\n";
    }

    return colors;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    l1->Caption = String(GetTColors());
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
    // Method 2: C strings
    const int MAX = 16777215;
    char colors[1000] = "";
    char stmp[10];

    for (int i=0; i<= MAX; ++i)
    {
       strcpy(stmp, ColorToString((TColor)i).c_str());

       // C strings are zero-based
       // TColors start with cl, so we have a TColor when
       // the second element of the C string (index 1) is 'l'
       if (stmp[1] == 'l' )
       {
           strcat(stmp, " ");
           strcat(stmp, IntToHex(ColorToRGB((TColor)i), 6).c_str());
           strcat(stmp, "\n");
           strcat(colors, stmp);
       }
    }

    l2->Caption = String(colors);
}

VCLColorFunctions.h


#ifndef VCLColorFunctionsH
#define VCLColorFunctionsH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.