Chapter 22: Graphics Programming

Graphics programming in Windows is based on the GDI, the Windows graphical device interface. The GDI is an API designed to enable us to draw graphics on screen and on the printer. In Delphi, you can use GDI objects and functions directly or you can use the VCL classes that encapsulate GDI functions and objects. In this chapter, we'll mostly work with VCL classes that encapsulate GDI functionality.

Using Colors

In Windows, colors are defined by three values: red, green, and blue. Each value specifies the intensity of the color component. If all the values are set to the minimum value 0, the resulting color is black. If all values are set to the maximum value 255, the resulting color is white. To create a color from these separate color components, you have to use the RGB function.

The RGB function accepts three byte parameters — one parameter for each color component — and returns a COLORREF value. The COLORREF value is a 32-bit unsigned integer value used to specify a color.

function RGB(r, g, b: Byte): COLORREF;

Listing 22-1 uses the RGB function to enable the user to select custom colors, as shown in Figure 22-1.

image from book
Figure 22-1: Creating custom colors with the RGB function

The application uses a TPanel component as a preview frame and three TScrollBar components that enable the user to modify the red, green, and blue color components. The Max property of all three TScrollBar components is set to the maximum value for each component: 255. All three TScrollBar components share a single, extremely simple OnChange event handler.

Listing 22-1: Using the RGB function

image from book
procedure TForm1.ScrollChange(Sender: TObject); begin   Panel1.Color := RGB(RedBar.Position,     GreenBar.Position, BlueBar.Position); end;
image from book

In Delphi, colors are automatically represented by 32-bit TColor values. The TColor type is declared in the Graphics unit, along with several useful color constants. These color constants cover:

  • Standard colors like clWhite, clRed, clGreen, clBlue, clBlack, clMaroon

  • System colors like clBtnFace, clScrollBar, clActiveBorder, clMenu, clWindow

  • Named web colors like clWebBlueViolet, clWebGainsboro, clWebThistle

You can also specify colors as hexadecimal numbers. When doing so, remember to specify the components "backwards," that is, first specify the blue value, then green, and finally red:

Color := $000000; { black } Color := $FF0000; { blue } Color := $00FF00; { green } Color := $0000FF; { red } Color := $FFFFFF; { white }

Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: