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 Delphi 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 named pnlColor, three Edit controls named edtRed, edtGreen, and edtBlue, and a ColorDialog control:
     

  5. Implement the project as follows:
     
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ExtCtrls, StdCtrls, Buttons;
    
    type
      TForm1 = class(TForm)
        Label1: TLabel;
        Label2: TLabel;
        Label3: TLabel;
        edtRed: TEdit;
        edtGreen: TEdit;
        edtBlue: TEdit;
        pnlPreview: TPanel;
        Image1: TImage;
        BitBtn1: TBitBtn;
        procedure FormCreate(Sender: TObject);
        procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
      IsScanning: Boolean;
    
    implementation
    
    {$R *.DFM}
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
         IsScanning := False;
    end;
    
    procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
         IsScanning := True;
    end;
    
    procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
         IsScanning := False;
    end;
    
    procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    var
       Clr: TColor;
    begin
         If IsScanning = True Then
         Begin
              Clr := Image1.Canvas.Pixels[X, Y];
    
              edtRed.Text := IntToStr(GetRValue(Clr));
              edtGreen.Text := IntToStr(GetGValue(Clr));
              edtBlue.Text  := IntToStr(GetBValue(Clr));
              pnlPreview.Color := Clr;
         End
    end;
    
    end.

  6. Test the application

 

 


Copyright © 2004-2005 FunctionX, Inc.