The Type color
The type color represents colors.
In this example, variables myColor1 and myColor2 are of type color.
Values of variables myColor1 and myColor2 are given by named initializers. Named initializers begin with a slash character (/).
Each variable of type color has three members: .r, .g and .b.
The members .r, .g and .b must be in the range 0–100.
The members represent the three separate primary color components of your display device: .r for red , .g for green , and .b for blue .
In this example, the chosen intensities of red, green and blue components are such that their mix produces the azure color.
Try changing the values of members .r, .g and .b to get some other colors.
A nameless initializer consists of expressions inside square brackets . A nameless initializer for type color consists of three expressions, each specifying a value for one of the members .r, .g and .b, in the given order.
Members from type color can be printed out individually. Each member behaves like a variable.
The println statement can print out an entire color at once by outputting the values of members .r, .g and .b.
This program contains an error.
The variable un is introduced in the first statement. The value of this variable is not set. More precisely, this variable is uninitialized.
This variable cannot be used until it is initialized.
A variable of type color is initialized when all three members are initialized.
One member of variable un is uninitialized.
Correct the error by initializing the missing member and run the program.
un.r=80; un.g=22; un.b=0;
(Note: in place of 22, any number in the range 0-100 is OK.)
By modifying the intensity of the red component, this program draws discs with colors ranging from violet to magenta.
The named argument ln of println statement specifies the line number. When using a named argument in a println statement, all arguments need to be placed inside square brackets.
This program displays a color gradient by drawing many discs close together.
Multiple variables of type color can coexist in the same program. Each variable of type color has its own members .r, .g and .b.
An initializer can be given as an argument of a function. In this program, the last call of function disc has a nameless initializer for type color as the last argument.
Initializers are not expressions. println statement can print expressions only, not initializers.
There is a way to print out RGB values of a color given by named initializer /orange. How would you do it?
To print a value of an initializer, initialize a variable by the initializer and then print out the variable.
#color myorange = /orange; println "orange: ", myorange;
Note for color experts only: RGB values of type color are linear, not gamma-compressed. The gamma-compressed type with values 0-255 is named rgb. The getter color.rgb performs the conversion.
* * *
Named arguments, like fg, can be given by initializers. A named argument fg is actually of type colorA, which is described in a later chapter.
This program contains two errors. Still, it can be run.
Run the given program.
Values of members .r, .g and .b must be in range 0–100.
Attempting to assign a value outside the given range will raise a runtime error.
Runtime errors cannot be detected by the computer before the program is executed.
Correct both errors and run the program.
Additional chapter Computer Graphics Exercises is available. This chapter provides a set of 8 additional exercises on basics of computer graphics.
Some of the provided exercises are hard. The level of difficulty is such that 95% of students cannot sucessfully solve all the exercises, and 99% cannot solve all the exercises without looking at the hints.
If you find the exercises easy, that would mean you are a very talented person.
The exercises provide valuable lessons even if you have to look at the solutions.
To read the exercises, click on the following link: