Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Programming the Be Operating System-Chapter 5: Drawing ppt
Nội dung xem thử
Mô tả chi tiết
134
Chapter 5
In this chapter:
• Colors
• Patterns
• The Drawing Pen 5 • Shapes
Drawing 5.
When a Be application draws, it always draws in a view. That’s why the chapter
that deals with views precedes this chapter. In Chapter 4, Windows, Views, and
Messages, you saw that a BView-derived class overrides its inherited hook member function Draw() so that it can define exactly what view objects should draw
in when they get updated. The example projects in this chapter contain classes
and member functions that remain unchanged, or changed very little, from previous example projects. What will be different is the content of the Draw() function.
The code that demonstrates the concepts of each drawing topic can usually be
added to the Draw() routine.
In Be programming, the colors and patterns that fill a shape aren’t defined explicitly for that shape. Instead, traits of the graphics environment of the view that
receives the drawing are first altered. In other words, many drawing characteristics, such as color and font, are defined at the view level, so all subsequent drawing can use the view settings. In this chapter, you’ll see how to define a color,
then set a view to draw in that color. You’ll see how the same is done for patterns—whether using Be-defined patterns or your own application-defined ones.
After you learn how to manipulate the graphic characteristics of a view, it’s on to
the drawing of specific shapes. The point (represented by BPoint objects) is used
on its own, to define the end points of a line, and to define the vertices of more
sophisticated shapes (such as triangles or polygons). The rectangle (represented by
BRect objects) is used on its own and as the basis of more sophisticated shapes.
These shapes include round rectangles, ellipses, and regions. Round rectangles
and ellipses are closely related to BRect objects, and aren’t defined by their own
classes. Polygons and regions are more sophisticated shapes that make use of
points and rectangles, but are represented by their own class types (BPolygon and
BRegion). In this chapter, you’ll see how to outline and fill each of these different
Colors 135
shapes. Finally, I show how to combine any type and number of these various
shapes into a picture represented by a BPicture object.
Colors
The BeOS is capable of defining colors using any of a number of color spaces. A
color space is a scheme, or system, for representing colors as numbers. There are
several color space Be-defined constants, each containing a number that reflects
the number of bits used to represent a single color in a single pixel. For instance,
the B_COLOR_8_BIT color space devotes 8 bits to defining the color of a single
pixel. The more memory devoted to defining the color of a single pixel, the more
possible colors a pixel can display.
B_GRAY1
Each pixel in a drawing is either black (bit is on, or 1) or white (bit is off, or
0).
B_GRAY8
Each pixel in a drawing can be one of 256 shades of gray—from black (bit is
set to a value of 255) to white (bit is set to a value of 0).
B_CMAP8
Each pixel in a drawing can be one of 256 colors. A pixel value in the range
of 0 to 255 is used as an index into a color map. This system color map is
identical for all applications. That means that when two programs use the
same value to color a pixel, the same color will be displayed.
B_RGB15
Each pixel in a drawing is created from three separate color components: red,
green, and blue. Five out of a total of sixteen bits are devoted to defining each
color component. The sixteenth bit is ignored.
B_RGB32
Like the B_RGB15 color space, each pixel in a drawing is created from three
separate color components: red, green, and blue. In B_RGB32 space, however, eight bits are devoted to defining each color component. The remaining
eight bits are ignored.
B_RGBA32
Like the B_RGB32 color space, each pixel in a drawing is created from three
separate color components: red, green, and blue. Like B_RGB, eight bits are
used to define each of the three color components. In B_RGBA32 space, however, the remaining eight bits aren’t ignored—they’re devoted to defining an
alpha byte, which is used to specify a transparency level for a color.
136 Chapter 5: Drawing
RGB Color System
As listed above, the BeOS supports a number of color spaces. The RGB color
space is popular because it provides over sixteen million unique colors (the number of combinations using values in the range of 0 to 255 for each of the three
color components), and because it is a color system with which many programmers and end users are familiar with (it’s common to several operating systems).
The BeOS defines rgb_color as a struct with four fields:
typedef struct {
uint8 red;
uint8 green;
uint8 blue;
uint8 alpha;
} rgb_color
A variable of type rgb_color can be initialized at the time of declaration. The
order of the supplied values corresponds to the ordering of the struct definition. The following declares an rgb_color variable named redColor and assigns
the red and alpha fields a value of 255 and the other two fields a value of 0:
rgb_color redColor = {255, 0, 0, 255};
To add a hint of blue to the color defined by redColor, the third value could be
changed from 0 to, say, 50. Because the alpha component of a color isn’t supported at the time of this writing, the last value should be 255. Once supported, an
alpha value of 255 will represent a color that is completely opaque; an object of
that color will completely cover anything underneath it. An alpha field value of 0
will result in a color that is completely transparent—an effect you probably don’t
want. An rgb_color variable can be set to represent a new color at any time by
specifying new values for some or all of the three color components. Here an
rgb_color variable named blueColor is first declared, then assigned values:
rgb_color blueColor;
blueColor.red = 0;
blueColor.green = 0;
blueColor.blue = 255;
blueColor.alpha = 255;
While choosing values for the red, green, and blue components of a
color is easy if you want a primary color, the process isn’t completely intuitive for other colors. Quickly now, what values should
you use to generate chartreuse? To experiment with colors and their
RGB components, run the ColorControl program that’s discussed a
little later in this chapter. By the way, to create the pale, yellowish
green color that’s chartreuse, try values of about 200, 230, and 100
for the red, green, and blue components, respectively.
Colors 137
High and Low Colors
Like all graphics objects, an rgb_color variable doesn’t display any color in a
window on its own—it only sets up a color for later use. A view always keeps
track of two colors, dubbed the high and low colors. When you draw in the view,
you specify whether the current high color, the current low color, or a mix of the
two colors should be used.
Views and default colors
When a new view comes into existence, it sets a number of drawing characteristics to default values. Included among these are:
• A high color of black
• A low color of white
• A background color of white
Additionally, when a BView drawing function is invoked, by default it uses the
view’s high color for the drawing. Together, these facts tell you that unless you
explicitly specify otherwise, drawing will be in black on a white background.
Setting the high and low colors
The BView member functions SetHighColor() and SetLowColor() alter the
current high and low colors of a view. Pass SetHighColor() an rgb_color and
that color becomes the new high color—and remains as such until the next call to
SetHighColor(). The SetLowColor() routine works the same way. This next
snippet sets a view’s high color to red and its low color to blue:
rgb_color redColor = {255, 0, 0, 255};
rgb_color blueColor = {0, 0, 255, 255};
SetHighColor(redColor);
SetLowColor(blueColor);
Drawing with the high and low colors
Passing an rgb_color structure to SetHighColor() or SetLowColor() establishes that color as the one to be used by a view when drawing. Now let’s see
how the high color is used to draw in color:
rgb_color redColor = {255, 0, 0, 255};
BRect aRect(10, 10, 110, 110);
SetHighColor(redColor);
FillRect(aRect, B_SOLID_HIGH);