<- previous    index    next ->

Lecture 3 Color

With pigment paint, the "primary colors" are red, blue and yellow.
With electronic displays the "primary colors" are red, green and blue.

The program Jcolor.java uses the built in names for colors and
lists the numeric values that correspond to the red, green and blue
color components. The output of Jcolor.java is:



Notice that each color has a group of three numbers that represent
the amount of red, green and blue, hereafter referred to as RGB.

RGB = 0,0,0         is black, no color.
RGB = 255, 255, 255 is white, all color.
RGB = 255,0,0       is red.
RGB = 0,255,0       is green.
RGB = 0,0,255       is blue.


A more complicated Java program consists of three files that need
to be compiled, gives the user "sliders" to choose a color.
The Red, Green and Blue components can be selected independently.

MyColorChooser.java 
DrawPanel.java 
PaletteFrame.java 

an optional Java applet is:
PaletteApp.java 
PaletteApp.html 

A sample of PaletteFrame output is:




In programming there is usually an alternative floating point
RGB with the color components in the range 0.0 to 1.0 equivalent
to 0 to 255. A fourth component "Alpha", A, opacity can be
present making the RGBA of a pixel.



A sample of X Windows coding of a colorwheel is colorw.c uses calculated values for colors. The output is



X Windows defines many more color names than Java, these are available
in rgb.txt

Colors are used in combination with lighting to fool the eye into
seeing various textures. teapots.c renders the
Utah Teapot with various colors and surfaces to provide the image



There are many formats for graphics files. Two of the most common used
on the WWW are  .gif  and  .jpg, Gif and Jpeg image files. Most graphics
formats can be converted to most other graphics formats. A common program
used for modifying images and changing formats is Paint Shop Pro. A free
version of this program may be downloaded form the WWW for MS Windows.
A similar program for Linux is Gimp which comes with many Linux
distributions and may also be freely downloaded.

Images may be scanned, captured from the WWW and created using a graphics
editor. In order to use graphics in your application program, you need
to be able to read the specific file format. Two demonstration programs
alpha_fade.c and alpha_fade2.c
are provided with respective files gifread.c and
jpegread.c 
These demonstration programs read four  .gif  or  .jpg  files and also
demonstrate the use of "Alpha" to fade from one image to the next.

Many other graphics formats can be read. Some have code available on the WWW
but some you may have to write your own code to read the format. The basic
structure of graphic image files is a header with information about the
image such as height and width in pixels. Then there is generally a
Color Table, often coded as "ct". The basic idea is to have a set of colors,
a set of RGB's, stored in a table and then use one unsigned byte for
each pixel. The value in the unsigned byte is an index into the Color Table.
The terminology is that a color table with RGB components of eight bits
has 24 bits for each color or 2^24, over 4 million, possible colors.
The Color Table may have a maximum of 256 entries, called the pallet,
for this particular image. An unsigned byte can index from 0 to 255 thus
selecting one of the 256 colors in the pallet for each pixel.

Some graphics image formats allow compression such that the original
image is not exactly reproduced yet can look acceptable. This saves on
disk space and computer input/output time yet uses more CPU time.
But, in your application program, each pixel is usually stored as
a 32 bit word, RGBA. Note that OpenGL texture mapping files are stored
just as they would appear in RAM in your application. X Windows
bitmap files, d13.xbm ,
are actually "C" header files d13.h with the bits
encoded as hexadecimal. The .xbm files can be read at execution time
or included with  "#include". For use in OpenGL use xbmread.c as
tested in xbm_to_gl.c
Each pixel in the  .xbm  file is on or off. The user specifies the
foreground and background color.

Just basic colors are not enough to get good looking graphics.
Shading across each, usually small, polygon provides the finishing
touch.

The subject of "lighting" will be covered in a future lecture.

Gouraud shading interpolates the colors at the vertices across the polygon.

Phone specular shading interpolates the normal vector at the vertices
across the polygon. More will be discussed on lighting in a later lecture.

    <- previous    index    next ->

Other links

Go to top