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 var3 Form1: TForm1;
4 MyColor: LongWord;
5 RedColor: LongWord = $00000000;
6 GreenColor: LongWord = $00000000;
7 BlueColor: LongWord = $00000000;
8 9 implementation10 11 {$R *.DFM}12 13 procedure TForm1.FormCreate(Sender: TObject);
14 begin15 DoImageFill;
16 end;
17 18 procedure TForm1.DoImageFill;
19 begin20 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 begin27 RedColor := RedBar.Position;
28 DoImageFill;
29 end;
30 31 procedure TForm1.GreenBarChange(Sender: TObject);
32 begin33 GreenColor := GreenBar.Position shl 8;
34 DoImageFill;
35 end;
36 37 procedure TForm1.BlueBarChange(Sender: TObject);
38 begin39 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 type46 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 public55 { Public declarations }56 procedure ChangeColor;
57 end;
58 59 procedure TForm1.ChangeColor;
60 begin61 Panel1.Color := RGB(redTrackBar.Position, greenTrackBar.Position,
62 blueTrackBar.Position);
63 end;
64 65 procedure TForm1.blueTrackBarChange(Sender: TObject);
66 begin67 ChangeColor;
68 end;
69 70 procedure TForm1.greenTrackBarChange(Sender: TObject);
71 begin72 ChangeColor;
73 end;
74 75 procedure TForm1.redTrackBarChange(Sender: TObject);
76 begin77 ChangeColor;
78 end;