The Type color [1/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 named initializers. Named 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.
A nameless initializer consists of expressions inside square brackets []. A nameless initializer for type color contains 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.
After executing the program, try printing out sum of the members .g and .b of the variable azure. 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.)
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 nameless initializer of the variable c. See what other color gradients you can get. You can also try modifying the second argument of the initializer.
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.
After running the program, try modifying the nameless initializer in the last statement of the source code. Try to modify it so that the last disc is painted in yellow.
After that, try adding another variable named mycolor and set it to a value given by the named initializer /purple. Then draw a disc in the color given by variable mycolor below the first disc.
After that, 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 below the second disc.
After that, 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.
Initializers are not expressions. println statement can print expressions only, not initializers.
Exercise:
There is a way to print out RGB values of a color given by named 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;
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.
* * *
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.
Run this program. Then, try changing the initial values of variables x, y, and h and observe the effects that the changes are producing.
You can also try modifying the initial value of variable text.
* * *
-- the Share button --
You can change the value of variable text, and then send this program to your friends by using the Share button.
You will have to type in the title of the program, then enter your name as the author, and select a license.
Then, click on the "Create Link" button to create a program link. Click on the program link to see your program in action. To show your program to others, just send them the program link URL, or use the sharing buttons on the run page.
Demonstrational programs use features which are not immediately explained.
You can try modifying the string logo in the given program.
<< F2:Prev - - F4:Next >>