Computing a Picture
Let us quickly summarize the most essential concepts we have mentioned so far. The picture is imagined as being divided into small squares called pixels. In order to calculate the entire picture, the color of each pixel has to be computed and set. All in all, our pictures will be consisting of pixels organized in 600 rows and 800 columns.
In our programs, a color will be a type of value. To
create a value of this type, we can use the function makecol
, which is made available by the Allegro library. It has 3
parameters, each one specifying the intensity of the red, green and blue
component, in that order. The maximum intensity for each component is 255. A
grayish white color having 55% of full intensity[*] is created by the
expression:
Why 55%, you might be asking. Is not the number 200 actually 78% of the number 255? Yes, but the color intensities are gamma-coded according to the sRGB standard. The exact calculation is somewhat complex (not that much), and is called gamma decoding. Gamma decoding can be approximated by raising a value to the power of 2.2. Thus, the actual intensity produced will be approximately 2002.2 compared to 2552.2. That turns out to be around 59%.
It is not physically possible to create all visible colors by joining three primary color components, regardless of the choice of primary colors. The primary colors that were selected for your monitor and television set can create a reasonably large gamut of colors by mixing light passing through screen's primary color filters. This gamut is larger then the one available by classical paints, but much smaller than the gamut of a photographic film. Monochromatic lasers can produce any visible color.
makecol(200, 200, 200)
A value of white color of full intensity is the result of the expression:
makecol(255, 255, 255)
It is not allowed to set any color component to a value greater than 255, or to a value smaller than 0. The values of color components are integers.
In programmers' parlance, black is a color, too. The value of the black color is produced by the expression:
makecol(0,0,0)
Without further ado, here we present a table of some commonly used colors:
black | makecol(0,0,0) |
light grey | makecol(192,192,192) |
blue | makecol(0,0,255) |
grey | makecol(128,128,128) |
red | makecol(255,0,0) |
dark grey | makecol(64,64,64) |
magenta | makecol(255,0,255) |
pink | makecol(255,192,203) |
green | makecol(0,255,0) |
violet | makecol(139,0,255) |
cyan | makecol(0,255,255) |
brown | makecol(150,75,0) |
yellow | makecol(255,255,0) |
orange | makecol(255,127,0) |
white | makecol(255,255,255) |
electric blue | makecol(125,249,255) |
In our programs, the entire image
displayed on the screen will be represented by a variable named surface
. We
will be using this surface as if it was a surface for painting. The programs
will draw various objects on this surface, such as lines, points, rectangles
and circles. After the picture on the surface has been completed, we will use the
Allegro library to display the surface on the screen.
Let us see how to set a color of a pixel on a surface. If we would like to set a pixel on a 200th row and 300th column to blue color, we would write:
putpixel(surface, 200, 300, makecol(0,0,255) );
Now, how to draw a blue line? Well, if
we were to set all the pixels on the 300th row that are located
between columns 100 and 400 to the color blue, the result would be an image of
a horizontal blue line.[*] We can do just that with the help of a for
loop:
The numbers given are not quite correct, which will become apparent soon.
for (int i=100;i<400;i++) putpixel(surface, i, 300, makecol(0,0,255) );
The entire program that draws a horizontal blue line would go:
#include <allegro.h> int main() { if (allegro_init()!=0) return 1; set_color_depth(32); if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0)!=0) return 2; BITMAP* surface = create_bitmap(SCREEN_W, SCREEN_H); install_keyboard(); clear_to_color(surface, makecol(255,255,255) ); for (int i=100;i<400;i++) putpixel(surface, i, 300, makecol(0,0,255) ); blit(surface, screen, 0,0,0,0,SCREEN_W,SCREEN_H); while (!keypressed()) rest(20); return 0; } END_OF_MAIN()
The given program produces a horizontal blue line on a white background. What makes the background white? The following command does:
clear_to_color(surface, makecol(255,255,255) );
It sets every pixel of a given surface to a given color.
For the time being, you do not have
to pay too much attention to parts of this program that have not been explained
yet, so ignore the line beginning with word install_keyboard
and everything
above, as well as the line starting with word blit
and all below it.