Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to adjust RGB values using TTrackBar Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
29-Aug-02
Category
VCL-General
Language
Delphi 2.x
Views
100
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I would like to program an application in which you can control values of red, 
green and blue with trackbar. How can I do that?

Answer:

Solve 1:

Drop three TrackBars on a form. Set Min to 0, Max to 255. Drop a TImage on the 
form. Then try this code:

1   { ... }
2   var
3     Form1: TForm1;
4     MyColor: LongWord;
5     RedColor: LongWord = $00000000;
6     GreenColor: LongWord = $00000000;
7     BlueColor: LongWord = $00000000;
8   
9   implementation
10  
11  {$R *.DFM}
12  
13  procedure TForm1.FormCreate(Sender: TObject);
14  begin
15    DoImageFill;
16  end;
17  
18  procedure TForm1.DoImageFill;
19  begin
20    MyColor := RedColor or GreenColor or BlueColor;
21    Image1.Canvas.Brush.Color := TColor(MyColor);
22    Image1.Canvas.FillRect(Rect(0, 0, Image1.Width, Image1.Height));
23  end;
24  
25  procedure TForm1.RedBarChange(Sender: TObject);
26  begin
27    RedColor := RedBar.Position;
28    DoImageFill;
29  end;
30  
31  procedure TForm1.GreenBarChange(Sender: TObject);
32  begin
33    GreenColor := GreenBar.Position shl 8;
34    DoImageFill;
35  end;
36  
37  procedure TForm1.BlueBarChange(Sender: TObject);
38  begin
39    BlueColor := BlueBar.Position shl 16;
40    DoImageFill;
41  end;
42  
43  end.



Solve 2:

Each color value ranges from 0 to 255. Set the three trackbars with this range. You 
can use the RGB function to create a color from these values.

44  { ... }
45  type
46    TForm1 = class(TForm)
47      redTrackBar: TTrackBar;
48      greenTrackBar: TTrackBar;
49      blueTrackBar: TTrackBar;
50      Panel1: TPanel;
51      procedure blueTrackBarChange(Sender: TObject);
52      procedure greenTrackBarChange(Sender: TObject);
53      procedure redTrackBarChange(Sender: TObject);
54    public
55      { Public declarations }
56      procedure ChangeColor;
57    end;
58  
59  procedure TForm1.ChangeColor;
60  begin
61    Panel1.Color := RGB(redTrackBar.Position, greenTrackBar.Position, 
62  blueTrackBar.Position);
63  end;
64  
65  procedure TForm1.blueTrackBarChange(Sender: TObject);
66  begin
67    ChangeColor;
68  end;
69  
70  procedure TForm1.greenTrackBarChange(Sender: TObject);
71  begin
72    ChangeColor;
73  end;
74  
75  procedure TForm1.redTrackBarChange(Sender: TObject);
76  begin
77    ChangeColor;
78  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC