The Type color [10/11]
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 marked initializers. Marked initializers begin with a slash character (/).
After executing the program, try changing the initial value of variable mycolor2. Set it to to yellow instead of blue.
You can click on the 'Overview' button in the menu to see how far you have progressed.
Each variable of type color has three members: .r, .g and .b.
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.
The members .r, .g and .b must be in the range 0–100.
After executing the program, try changing the values of members .r, .g and .b to get some other colors.
Let's compare a custom azure color, stored in the variable
azure,
with the azure color of the marked initalizer /azure .
[Run the program]
We can see that the variable azure contains a color slightly darker then the color of the the marked initalizer /azure .
In the first statement of the source code, the variable azure was assigned a value of type color by an unmarked initializer. An unmarked initializer consists of expressions inside square brackets []. An unmarked initializer for type color contains three expressions, each specifying a value for one of the members .r, .g and .b, in that 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.
After executing the program, try printing out sum of the members .g and .b of the variable azure. The result should equal the number 81, of course. Don't put any numbers in the inserted statement.
This program contains two errors. Still, it can be run.
Run the given program.
Values of members .r, .g and .b must be in the 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.
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.
Solution (click):
un.r=80; un.g=22; un.b=0;
(Note: in place of 22, any number in the range 0-100 is OK.)
Each member from type color can be individually changed.
Exercise:
1. Add another statement at the end of the source code. In it, modify the value of variable myColor by setting the blue component to value 10.
Solution
myColor.b = 10;
2. Draw a disc, below the third disc, in the color stored in the variable myColor.
Solution
disc(650, 500, 100, myColor);
Multiple variables of type color can coexist in the same program.
Each variable of type color has its own members
.r, .g and
.b.
[Run the program]
An initializer can be given as an argument of a function or procedure. In this program, the last call of procedure disc has an unmarked initializer for type color as the last argument.
Exercises:
Important: Take a peak at a solution if an exercise is too challenging.
1. Modify the unmarked initializer in the last statement of the source code.
Try to modify it so that the
last disc is painted approximately in yellow.
[Run the program]
Solution
disc(760, 360, 100, [70, 70, 0]);
2. (Challenging!) Add another statement at the end of the source code. In it, create a variable named mycolor of type color, and set it to a value given by the marked initializer /purple.
Solution
#color mycolor = /purple;
3. Draw a disc in the color given by variable mycolor . Choose any on-screen coordinates of your preference.
Solution
disc(200, 500, 100, mycolor);
4. Add another statement at the end of the source code. In it, modify the value of variable mycolor by setting the member .b to value 0. Then draw a disc in the color stored in the variable mycolor somewhere below the second disc.
Solution
mycolor.b = 0; disc(480, 500, 100, mycolor);
5. Add another statement at the end of the source code. In it, modify the value of variable mycolor by setting the green component to value 60. Then draw a disc in the color stored in the variable mycolor below the third disc.
Solution
mycolor.g = 60; disc(760, 500, 100, mycolor);
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.
After running the program, try modifying the value of the argument ln in the println statement.
This program displays a color gradient by drawing many discs close together.
After running the program, try modifying the last argument of the unmarked initializer of the variable c. See what other color gradients you can get. You can also try modifying the second argument of the initializer.
Initializers are not expressions.
println
statement can print expressions only, not initializers.
Exercise:
(Challenging!) There is a way to print out RGB values of a color given by marked initializer /orange. How would you do it?
Solution
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;
For 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.
* * *
Value of a named argument, like fg, can be given by an initializer or by an expression. A named argument fg is actually of type colorA, which is described in a later chapter.
<< F2:Prev - - F4:Next >>