Color Selector

 

Introduction

 This exercise shows how to retrieve the color of a pixel and create a TColor object:

 

Dragging And Dropping Controls

  1. Start Borland C++ Builder with the startup form.

  2. Right-click the desired picture above (I created them using MS Image Composer) and save it to your hard disk where you can retrieve it in the next step

  3. On the form, add an Image control and set its Picture property to the picture you saved

  4. Design the rest of the form with a Panel control named pnlColor, three Edit controls named edtRed, edtGreen, and edtBlue, and a ColorDialog control:
     

  5. Here is the header file of the form:
     
    //---------------------------------------------------------------------------
    #ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    #include <Dialogs.hpp>
    #include <ExtCtrls.hpp>
    #include <jpeg.hpp>
    #include <Graphics.hpp>
    #include <Buttons.hpp>
    //---------------------------------------------------------------------------
    class TForm1 : public TForm
    {
    __published:	// IDE-managed Components
        TLabel *Label2;
        TEdit *edtRed;
        TLabel *Label3;
        TEdit *edtGreen;
        TLabel *Label4;
        TEdit *edtBlue;
        TColorDialog *ColorDialog1;
        TPanel *pnlColor;
        TImage *Image1;
        TBitBtn *BitBtn1;
        void __fastcall pnlColorDblClick(TObject *Sender);
        void __fastcall BitBtn1Click(TObject *Sender);
        void __fastcall Image1MouseDown(TObject *Sender, TMouseButton Button,
              TShiftState Shift, int X, int Y);
        void __fastcall Image1MouseUp(TObject *Sender, TMouseButton Button,
              TShiftState Shift, int X, int Y);
        void __fastcall Image1MouseMove(TObject *Sender, TShiftState Shift,
              int X, int Y);
    private:
        Boolean IsScanning;	// User declarations
    public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif

  6. Here is the source file of the form:
     
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #include <Windows.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
        IsScanning = False;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::pnlColorDblClick(TObject *Sender)
    {
        if( ColorDialog1->Execute() )
        {
            COLORREF Clr = ColorDialog1->Color;
            pnlColor->Color = TColor(Clr);
            edtRed->Text = GetRValue(Clr);
            edtGreen->Text = GetGValue(Clr);
            edtBlue->Text = GetBValue(Clr);
        }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::BitBtn1Click(TObject *Sender)
    {
        Close();
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::Image1MouseDown(TObject *Sender,
          TMouseButton Button, TShiftState Shift, int X, int Y)
    {
        IsScanning = True;
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
          TShiftState Shift, int X, int Y)
    {
        IsScanning = False;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
          int X, int Y)
    {
        if( IsScanning == True )
        {
            TColor Clr = Image1->Canvas->Pixels[X][Y];
    
            edtRed->Text = GetRValue(Clr);
            edtGreen->Text = GetGValue(Clr);
            edtBlue->Text = GetBValue(Clr);
    
            pnlColor->Color = Clr;
        }
    }
    //---------------------------------------------------------------------------

  7. Test the application


Copyright © 2003-2007 FunctionX, Inc.