6.3. COLOR CHR$ CODES
Before you start reading this section, take a look at Appendix F, which
lists the CHR$ codes for all keys on the keyboard.
As you looked over the list of CHR$ codes, you probably noticed that
each color has a unique code, just like all the other keys and the
keyboard controls. If you print the codes themselves by using the CHR$
function mentioned in the last chapter, you can get the same results you
got by typing
or and the color key in a PRINT statement.
For example, try this:
NEW
10 PRINT CHR$(147): REM CLR/HOME
20 PRINT CHR$(28);"CHR$(28) CHANGES ME TO?"
RUN
CHR$(28) CHANGES ME TO?
When you RUN this program, the screen clears before the message in line
20 is PRINTed. The text should be red now.
In many cases, you'll find that it's much easier to use the CHR$
function to change colors, especially if you want to experiment. The next
page shows another way to get a rainbow of colors. There are a number of
similar lines in the program (40 through 110), so use the editing keys to
spare yourself a lot of typing. See the notes at the end of the program
listing to refresh your memory on editing procedures.
NEW
1 REM AUTOMATIC COLOR BARS
5 PRINT CHR$(147): REM CHR$(147) = CLR/HOME
10 PRINT CHR$(18); " ";: REM REVERSE BAR
20 CL = INT(8
RND(1))+1
30 ON CL GOTO 40,50,60,70,80,90,100,110
40 PRINT CHR$(5);: GOTO 10
50 PRINT CHR$(28);: GOTO 10
60 PRINT CHR$(30);: GOTO 10
70 PRINT CHR$(31);: GOTO 10
80 PRINT CHR$(144);: GOTO 10
90 PRINT CHR$(156);: GOTO 10
100 PRINT CHR$(158);: GOTO 10
110 PRINT CHR$(159);: GOTO 10
Type lines 1 through 40 normally. Your display should look like this:
1 REM AUTOMATIC COLOR BARS
5 PRINT CHR$(147): REM CHR$(147) = CLR/HOME
10 PRINT CHR$(18); " ";: REM REVERSE BAR
20 CL = INT(8
RND(1))+1
30 ON CL GOTO 40,50,60,70,80,90,100,110
40 PRINT CHR$(5);: GOTO 10

EDITING NOTES:
Use the <CRSR-UP> key to position the cursor on line 40. Then type 5
over the 4 of 40. Now use the <CRSR-RIGHT< key to move over to the 5 in
the CHR$ parentheses. Press <> and <INST/DEL> to open up a space,
and key in 28. Now just press <RETURN> with the cursor anywhere on the
line. The display should look like this now:
NEW
1 REM AUTOMATIC COLOR BARS
5 PRINT CHR$(147): REM CHR$(147) = CLR/HOME
10 PRINT CHR$(18); " ";: REM REVERSE BAR
20 CL = INT(8
RND(1))+1
30 ON CL GOTO 40,50,60,70,80,90,100,110
50 PRINT CHR$(28);: GOTO 10
Don't worry about line 40; it's still there, as you can see by LISTing
the program. Follow the same steps to modify line 40 with a new line
number and CHR$ code until you've entered all the remaining lines. As a
final check, LIST the entire program to make sure all the lines are right
before you RUN it.
You probably understand the color bar program except for line 30.
Here's a brief explanation of how this program works.
Line 5 prints the CHR$ code for <CLR/HOME>.
Line 10 turns on reverse type and prints 5 spaces, which turn out to be
a bar since they're reversed. The first time through the program, the bar
is light blue, the normal screen display color.
Line 20 uses the random function to select at random a color between 1
and 8.
Line 30 uses a variation of the IF/THEN statement, called ON/GOTO,
which lets the program choose from as list of line numbers where the
program will go next. If the ON variable (in this case CL) has a value of
1, the program goes to the first line number listed (here it's line 40).
If the variable has a value of 2, the program goes to the second line
listed, and so on.
Lines 40 through 110 just convert the random key colors to the
appropriate CHR$ code for that color and return the program to line 10 to
PRINT a section of the bar in that color. Then the whole process starts
again.
See if you can figure out how to produce 16 random colors. Expand
ON/GOTO to handle the additional colors and add the remaining CHR$ codes.