home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / PROGRAMM / FGL112B.ZIP / USER05.DOC < prev    next >
Text File  |  1992-10-05  |  69KB  |  1,483 lines

  1.  
  2.  
  3. Chapter 5
  4.  
  5. The Use of Color
  6. 48  Fastgraph User's Guide
  7.  
  8.  
  9. Overview
  10.  
  11.      The use of color is an important part of any text or graphics
  12. application.  This chapter explains color as it applies to text and graphics
  13. modes.  It also describes palettes and video DAC registers for the graphics
  14. video modes that offer this functionality.  Finally, an explanation of
  15. Fastgraph's virtual colors is provided.
  16.  
  17.  
  18. Text Modes
  19.  
  20.      The term color is not really correct in text modes because each
  21. character cell has an associated attribute that controls the character's
  22. appearance in that cell.  The meaning of the attribute differs for color and
  23. monochrome text modes.
  24.  
  25.  
  26. Color Modes
  27.  
  28.      In color text modes (modes 0, 1, 2, and 3), the attribute determines a
  29. character's foreground color (the color of the character itself), its
  30. background color (the color of that part of the character cell not covered by
  31. the character), and whether or not it blinks.  Sixteen foreground colors
  32. (numbered 0 to 15) are available, but only eight background colors (numbered
  33. 0 to 7) are available.  The colors assigned to these values are listed in the
  34. following table.
  35.  
  36.  
  37.                    number color     number color
  38.  
  39.                       0   black        8   gray
  40.                       1   blue         9   light blue
  41.                       2   green       10   light green
  42.                       3   cyan        11   light cyan
  43.                       4   red         12   light red
  44.                       5   magenta     13   light magenta
  45.                       6   brown       14   yellow
  46.                       7   white       15   bright white
  47.  
  48.  
  49. At first it may seem the numbers have been arbitrarily assigned to the
  50. colors.  Upon further inspection, however, it becomes apparent this is not
  51. the case.  Each color number is a four bit quantity of the form IRGB, with I
  52. representing the intensity, R the red component, G the green component, and B
  53. the blue component.  If the corresponding bit is 1, it means the intensity or
  54. color component is set.  For example, normal red would be represented by the
  55. IRGB bit pattern 0100, which is 4 decimal, the color number for red.
  56.  
  57.      The fg_setattr routine defines the current text attribute.  Once
  58. fg_setattr is called, Fastgraph displays all subsequent text using that
  59. attribute.  The first argument of fg_setattr defines the foreground color,
  60. which must be an integer between 0 and 15.  Its second argument defines the
  61.                                               Chapter 5:  The Use of Color  49
  62.  
  63. background color, which must be between 0 and 7.  Its third argument
  64. determines if the foreground color blinks (1 means it blinks, 0 means it does
  65. not).  For example, the statement
  66.  
  67.                              fg_setattr(14,1,0);
  68.  
  69. specifies subsequent text will be displayed with a yellow foreground (14) on
  70. a blue background (1) and will not blink (0).
  71.  
  72.      Another Fastgraph routine, fg_setcolor, also can be used to define text
  73. attributes.  The fg_setcolor routine packs the three values passed to
  74. fg_setattr into a single argument, as shown below.
  75.  
  76.  
  77.                             bits  attribute
  78.  
  79.                             0-3   foreground color
  80.                             4-6   background color
  81.                              7    blinking
  82.  
  83. For example, calling fg_setcolor with an argument of 30 (1E hex) is
  84. equivalent to calling fg_setattr with arguments of 14, 1, and 0.
  85.  
  86.      The Fastgraph routine fg_getcolor returns the current text attribute, as
  87. defined in the most recent call to fg_setattr or fg_setcolor.  The
  88. fg_getcolor routine has no arguments and returns the attribute as its
  89. function value.  The returned value is encoded using the same scheme for
  90. passing a text attribute to the fg_setcolor routine.
  91.  
  92.  
  93. Monochrome Mode
  94.  
  95.      In the monochrome text mode (mode 7), colors are obviously not
  96. available.  The attribute instead determines whether a character is
  97. invisible, normal, bold, reversed, or certain combinations of these.  The
  98. following table shows the values assigned to the available display
  99. characteristics.
  100.  
  101.  
  102.  
  103.                  foreground  background  characteristic
  104.  
  105.                       0           0      invisible
  106.                       0           7      reversed
  107.                       1           0      underlined
  108.                       7           0      normal
  109.                       9           0      underlined bold
  110.                      15           0      bold
  111.  
  112. Additionally, you can turn blinking on or off for each of these combinations.
  113. Any combination of foreground and background values not listed in the above
  114. table produces a normal display characteristic.
  115. 50  Fastgraph User's Guide
  116.  
  117.      As in the color modes, the Fastgraph routines fg_setattr and fg_setcolor
  118. define the current text attribute.  For example, the statement
  119.  
  120.                               fg_setattr(0,7,1);
  121.  
  122. specifies subsequent text will be displayed in reverse video (0,7) and will
  123. blink (1).  The same attribute could be defined by calling fg_setcolor with
  124. an argument of 240 (F0 hex).  The fg_getcolor routine is also available and
  125. works as it does in the color text modes.
  126.  
  127.  
  128. Graphics Modes
  129.  
  130.      In graphics modes, each pixel has an associated color value that
  131. determines the color in which the pixel is displayed.  The number of
  132. available colors depends on the video mode.  Some of the graphics modes also
  133. have palette registers or video DAC registers to provide additional color
  134. capabilities.  The example programs presented in this section show the use of
  135. color in specific graphics video modes.
  136.  
  137.      The following subsections will discuss the use of color in each graphics
  138. video mode.  In these discussions, there will be several references to a
  139. group of colors called the standard color set.  This is a set of 16 colors
  140. common to many of the graphics video modes (and to the color text modes).
  141. The colors in the standard color set are listed in the following table.
  142.  
  143.  
  144.                    number color     number color
  145.  
  146.                       0   black        8   gray
  147.                       1   blue         9   light blue
  148.                       2   green       10   light green
  149.                       3   cyan        11   light cyan
  150.                       4   red         12   light red
  151.                       5   magenta     13   light magenta
  152.                       6   brown       14   yellow
  153.                       7   white       15   bright white
  154.  
  155.  
  156.      At this point it is important to understand the difference between the
  157. terms color number and color value.  Color number refers to the number that
  158. defines a color in the standard color set (for example, green is color number
  159. 2).  Color value refers to the actual value of a pixel in video memory, which
  160. ultimately determines the color in which that pixel is displayed.  The color
  161. value is sometimes just called the color.
  162.  
  163.      In each graphics mode, video memory is zeroed when the fg_setmode
  164. routine is called.  This means all pixels are initially set to color value 0,
  165. which by default is black.  For this reason, color value 0 is often called
  166. the background color in graphics video modes.
  167.  
  168.      The Fastgraph routine fg_setcolor defines the color in which subsequent
  169. graphics operations are performed.  This color is called the current color.
  170.                                               Chapter 5:  The Use of Color  51
  171.  
  172. Depending on the video mode, the current color can reference a color value
  173. (in CGA and Hercules graphics modes), a palette register (in Tandy, EGA, and
  174. VGA graphics modes), or a video DAC register (in 256-color modes).  The
  175. fg_setcolor routine takes a single integer argument that specifies the color.
  176. When fg_setmode is called, it sets the current color to 0.  The Fastgraph
  177. routine fg_getcolor returns the current color, as defined in the most recent
  178. call to fg_setcolor.  The fg_getcolor routine has no arguments and returns
  179. the current color as its function value.
  180.  
  181.  
  182. CGA Color Modes
  183.  
  184.      The CGA color modes (modes 4 and 5) have six sets of available colors,
  185. called palettes, numbered 0 to 5.  Each palette consists of four colors,
  186. numbered 0 to 3.  In each palette, the background color (color value 0) can
  187. be selected from the standard color set, but the other 3 colors are fixed.
  188. The following table shows the fixed colors assigned to each palette.
  189.  
  190.  
  191.                      palette 0       palette 1       palette 2
  192.  
  193.            color 1   light green     light cyan      light cyan
  194.            color 2   light red       light magenta   light red
  195.            color 3   yellow          bright white    bright white
  196.  
  197.                      palette 3       palette 4       palette 5
  198.  
  199.            color 1   green           cyan            cyan
  200.            color 2   red             magenta         red
  201.            color 3   brown           white           white
  202.  
  203.  
  204. Palette 1, with a black background, is the default palette when you select
  205. mode 4.  Palette 2, with a black background, is the default palette when you
  206. select mode 5.
  207.  
  208.      The CGA color modes have a border area called the overscan between the
  209. addressable pixel space and the physical edges of the screen.  The overscan
  210. area is always displayed in the background color, regardless of which CGA
  211. palette is used.
  212.  
  213.      In CGA color modes, the fg_setcolor routine defines the current color by
  214. referencing one of the four color values.  The fg_palette routine selects one
  215. of the six palettes and defines the background color for that palette.  The
  216. first argument of the fg_palette routine is an integer between 0 and 5 that
  217. specifies the palette number.  The second argument is an integer between 0
  218. and 15 that defines the background color, using the color numbers in the
  219. standard color set.
  220.  
  221.      Example 5-1 demonstrates the use of the fg_palette and fg_setcolor
  222. routines in mode 4.  After establishing the video mode, the program selects
  223. palette 0 and makes the background color blue (color number 1).  It then
  224. makes color 3 in palette 0 (yellow) the current color and displays the word
  225. "Hello".  Finally, it restores the original video mode and screen attributes
  226. before returning to DOS.
  227. 52  Fastgraph User's Guide
  228.  
  229.                                  Example 5-1.
  230.  
  231.                            #include <fastgraf.h>
  232.                            void main(void);
  233.  
  234.                            void main()
  235.                            {
  236.                               int mode;
  237.  
  238.                               mode = fg_getmode();
  239.                               fg_setmode(4);
  240.  
  241.                               fg_palette(0,1);
  242.                               fg_setcolor(3);
  243.                               fg_text("Hello",5);
  244.                               fg_waitkey();
  245.  
  246.                               fg_setmode(mode);
  247.                               fg_reset();
  248.                            }
  249.  
  250. CGA Two-Color Mode
  251.  
  252.      The CGA two-color mode (mode 6) has a fixed background color (color
  253. value 0) and a user-definable foreground color (color value 1).  The
  254. background color is always black.  The foreground color is bright white by
  255. default, but it can be changed to any of the colors in the standard color
  256. set.  It should be mentioned that changing the foreground color works on true
  257. CGA adapters, but there are very few EGA and VGA adapters that correctly
  258. implement changing the foreground color in their mode 6 emulation.
  259.  
  260.      In mode 6, the fg_setcolor routine defines the current color by
  261. referencing one of the two color values.  The fg_palette routine defines the
  262. actual foreground color (that is, the color of pixels whose color value is
  263. 1).  To be consistent with the other graphics modes, the fg_palette routine
  264. has two arguments, but the first one is not used.  The second argument is an
  265. integer between 0 and 15 that defines the foreground color, using the color
  266. numbers in the standard color set.
  267.  
  268.      Example 5-2 demonstrates the use of the fg_palette and fg_setcolor
  269. routines in mode 6.  After establishing the video mode, the program makes the
  270. foreground color yellow (color number 14).  It then makes color 1 the current
  271. color and displays the word "Hello".  Finally, it restores the original video
  272. mode and screen attributes before returning to DOS.
  273.  
  274.                                  Example 5-2.
  275.  
  276.                            #include <fastgraf.h>
  277.                            void main(void);
  278.  
  279.                            void main()
  280.                            {
  281.                               int mode;
  282.  
  283.                               mode = fg_getmode();
  284.                               fg_setmode(6);
  285.  
  286.                                               Chapter 5:  The Use of Color  53
  287.  
  288.                               fg_palette(0,14);
  289.                               fg_setcolor(1);
  290.                               fg_text("Hello",5);
  291.                               fg_waitkey();
  292.  
  293.                               fg_setmode(mode);
  294.                               fg_reset();
  295.                            }
  296.  
  297. Tandy and PCjr Modes
  298.  
  299.      The supported Tandy 1000 or PCjr graphics mode (mode 9) has 16 color
  300. values, numbered 0 to 15.  Each color value references one of 16 user-
  301. definable palette registers, often simply called palettes, also numbered 0 to
  302. 15.  The values assigned to the palette registers determine the colors in
  303. which pixels are displayed.  For example, if you assign palette register 2
  304. the value for red, then pixels whose color value is 2 will be red.
  305.  
  306.      Each palette can assume one of the 16 colors in the standard color set.
  307. By default, the values assigned to the 16 palettes correspond to the
  308. identically numbered colors in the standard color set.  In other words,
  309. palette 0 is assigned the value for black, palette 1 is assigned the value
  310. for blue, and so forth.
  311.  
  312.      In mode 9, the fg_setcolor routine defines the current color by
  313. referencing one of the 16 palette registers.  The fg_palette routine defines
  314. the actual color assigned to a specific palette register.  The first argument
  315. of the fg_palette routine is an integer between 0 and 15 that specifies the
  316. palette number.  The second argument is an integer between 0 and 15 that
  317. defines the palette value (the color assigned to the palette), using the IRGB
  318. color numbers in the standard color set.
  319.  
  320.      You also can use the Fastgraph routine fg_setrgb to define the color
  321. assigned to a specific palette register.  Whereas the fg_palette routine does
  322. this using a color number from the standard color set, fg_setrgb defines a
  323. palette register using red, green, and blue color components plus an
  324. intensity component.  The first argument of the fg_setrgb routine is an
  325. integer between 0 and 15 that specifies the palette register number.  The
  326. remaining three arguments are each integer values between -1 and 1 that
  327. respectively specify the red, green, and blue color components for that
  328. palette register.  The meanings of the color components are:
  329.  
  330.      -1 = color bit and intensity bit are set
  331.       0 = color bit is reset
  332.       1 = color bit is set
  333.  
  334. Since there is only one intensity bit in mode 9 color values, specifying -1
  335. for any of the RGB color components produces an intense color.  For example,
  336. the color light cyan is color number 11 in the standard color set, and it is
  337. produced by combining green and blue and setting the intensity bit.  This
  338. means any of these four statements
  339.  
  340.                             fg_palette(1,11);
  341.                             fg_setrgb(1,0,-1,1);
  342.                             fg_setrgb(1,0,1,-1);
  343.                             fg_setrgb(1,0,-1,-1);
  344.  
  345. 54  Fastgraph User's Guide
  346.  
  347.  
  348. could be used to define palette register 1 as light cyan in mode 9.
  349.  
  350.      Example 5-3 demonstrates the use of the fg_palette and fg_setcolor
  351. routines in mode 9.  After establishing the video mode, the program defines
  352. palette 0 to be blue (1) and palette 1 to be yellow (14).  Note that defining
  353. palette 0 changes the background color.  It then makes color 1 the current
  354. color and displays the word "Hello".  After waiting for a keystroke, the
  355. program changes the color of "Hello" by changing palette 1 to bright white
  356. (15).  Finally, it restores the original video mode and screen attributes
  357. before returning to DOS.
  358.  
  359.                                  Example 5-3.
  360.  
  361.                            #include <fastgraf.h>
  362.                            void main(void);
  363.  
  364.                            void main()
  365.                            {
  366.                               int mode;
  367.  
  368.                               mode = fg_getmode();
  369.                               fg_setmode(9);
  370.  
  371.                               fg_palette(0,1);
  372.                               fg_palette(1,14);
  373.  
  374.                               fg_setcolor(1);
  375.                               fg_text("Hello",5);
  376.                               fg_waitkey();
  377.  
  378.                               fg_palette(1,15);
  379.                               fg_waitkey();
  380.  
  381.                               fg_setmode(mode);
  382.                               fg_reset();
  383.                            }
  384.  
  385.  
  386.  
  387. Hercules Mode
  388.  
  389.      The Hercules graphics mode (mode 11) has a fixed background color (color
  390. value 0) and a fixed foreground color (color value 1).  The background color
  391. is always black, and the foreground color is dependent on the monochrome
  392. display being used (typically, it is green, amber, or white).
  393.  
  394.      The fg_setcolor routine defines the current color value by referencing
  395. one of the two color values.  The fg_palette routine has no effect in mode
  396. 11.
  397.  
  398.      Example 5-4 demonstrates the use of the fg_setcolor routine in mode 11.
  399. After establishing the video mode, the program makes color 1 the current
  400. color and displays the word "Hello".  It then restores the original video
  401. mode and screen attributes before returning to DOS.
  402.                                               Chapter 5:  The Use of Color  55
  403.  
  404.                                  Example 5-4.
  405.  
  406.                            #include <fastgraf.h>
  407.                            void main(void);
  408.  
  409.                            void main()
  410.                            {
  411.                               int mode;
  412.  
  413.                               mode = fg_getmode();
  414.                               fg_setmode(11);
  415.  
  416.                               fg_setcolor(1);
  417.                               fg_text("Hello",5);
  418.                               fg_waitkey();
  419.  
  420.                               fg_setmode(mode);
  421.                               fg_reset();
  422.                            }
  423.  
  424.  
  425. Hercules Low-Resolution Mode
  426.  
  427.      The Hercules low-resolution graphics mode (mode 12) has four color
  428. values, numbered 0 to 3.  The background color is always black, colors 1 and
  429. 2 are half intensity, and color 3 is full intensity.  Colors 1 and 2 both
  430. produce normal intensity colors, but they do so with different pixel
  431. patterns -- color 1 turns on the odd-numbered physical pixels, while color 2
  432. turns on the even-numbered physical pixels.  The appearance of colors 1 to 3
  433. is dependent on the monochrome display being used (typically, it is green,
  434. amber, or white).
  435.  
  436.      The fg_setcolor routine defines the current color value by referencing
  437. one of the four color values.  The fg_palette routine has no effect in mode
  438. 12.
  439.  
  440.      Example 5-5 demonstrates the use of the fg_setcolor routine in mode 12.
  441. After establishing the video mode, the program makes color 3 the current
  442. color and displays the word "Hello".  It then restores the original video
  443. mode and screen attributes before returning to DOS.
  444.  
  445.                                  Example 5-5.
  446.  
  447.                            #include <fastgraf.h>
  448.                            void main(void);
  449.  
  450.                            void main()
  451.                            {
  452.                               int mode;
  453.  
  454.                               mode = fg_getmode();
  455.                               fg_setmode(12);
  456.  
  457.                               fg_setcolor(3);
  458.                               fg_text("Hello",5);
  459.                               fg_waitkey();
  460.  
  461. 56  Fastgraph User's Guide
  462.  
  463.                               fg_setmode(mode);
  464.                               fg_reset();
  465.                            }
  466.  
  467.  
  468.  
  469. EGA 200-Line Modes
  470.  
  471.      The 200-line EGA graphics modes (modes 13 and 14) have 16 color values,
  472. numbered 0 to 15.  Each color value references one of 16 user-definable
  473. palette registers, often simply called palettes, also numbered 0 to 15.  The
  474. values assigned to the palette registers determine the colors in which pixels
  475. are displayed.  For example, if you assign palette register 2 the value for
  476. red, then pixels whose color value is 2 will be red.
  477.  
  478.      Each palette can assume one of the 16 colors in the standard color set.
  479. By default, the values assigned to the 16 palettes correspond to the
  480. identically numbered colors in the standard color set.  In other words,
  481. palette 0 is assigned the value for black, palette 1 is assigned the value
  482. for blue, and so forth.
  483.  
  484.      In modes 13 and 14, the fg_setcolor routine defines the current color by
  485. referencing one of 16 available palette registers.  The fg_palette routine
  486. defines the actual color assigned to a specific palette register.  The first
  487. argument of the fg_palette routine is an integer between 0 and 15 that
  488. specifies the palette number.  The second argument is an integer that defines
  489. the palette value (the color assigned to the palette).  Although the actual
  490. colors are taken from the standard color set, the binary structure of a
  491. palette value is different from the IRGB format used in the standard color
  492. set.  In modes 13 and 14, the binary structure of a palette value is IxRGB;
  493. bit 3 is ignored.  The mode 13 and mode 14 palette values that correspond to
  494. the standard color set are thus:
  495.  
  496.  
  497.                     value color      value color
  498.  
  499.                       0   black       16   gray
  500.                       1   blue        17   light blue
  501.                       2   green       18   light green
  502.                       3   cyan        19   light cyan
  503.                       4   red         20   light red
  504.                       5   magenta     21   light magenta
  505.                       6   brown       22   yellow
  506.                       7   white       23   bright white
  507.  
  508.  
  509.      You also can use the Fastgraph routine fg_setrgb to define the color
  510. assigned to a specific palette register.  Whereas the fg_palette routine does
  511. this using a color number from the standard color set, fg_setrgb defines a
  512. palette register using red, green, and blue color components, plus an
  513. intensity component.  The first argument of the fg_setrgb routine is an
  514. integer between 0 and 15 that specifies the palette register number.  The
  515. remaining three arguments are each integer values between -1 and 1 that
  516.                                               Chapter 5:  The Use of Color  57
  517.  
  518. respectively specify the red, green, and blue color components for that
  519. palette register.  The meanings of the color components are:
  520.  
  521.      -1 = color bit and intensity bit are set
  522.       0 = color bit is reset
  523.       1 = color bit is set
  524.  
  525. Since there is only one intensity bit in mode 13 and 14 color values,
  526. specifying -1 for any of the RGB color components produces an intense color.
  527. For example, the color light cyan is represented by the color value 19, and
  528. it is produced by combining green and blue and setting the intensity bit.
  529. This means any of these four statements
  530.  
  531.                             fg_palette(1,19);
  532.                             fg_setrgb(1,0,-1,1);
  533.                             fg_setrgb(1,0,1,-1);
  534.                             fg_setrgb(1,0,-1,-1);
  535.  
  536. could be used to define palette register 1 as light cyan in modes 13 and 14.
  537.  
  538.      The Fastgraph routine fg_setcolor defines the color value (that is, the
  539. palette number) in which subsequent graphics operations are performed.  The
  540. fg_setcolor routine takes a single integer argument that specifies this
  541. color.  When fg_setmode is called, it sets the color value to 0.  The
  542. Fastgraph routine fg_getcolor returns the current color value, as defined in
  543. the most recent call to fg_setcolor.  The fg_getcolor routine has no
  544. arguments and returns the current color as the function value.
  545.  
  546.      Example 5-6 demonstrates the use of the fg_palette and fg_setcolor
  547. routines in mode 13.  After establishing the video mode, the program defines
  548. palette 0 to be blue (1) and palette 1 to be yellow (22).  Note that defining
  549. palette 0 changes the background color.  It then makes color 1 the current
  550. color and displays the word "Hello".  After waiting for a keystroke, the
  551. program changes the color of "Hello" by changing palette 1 to bright white
  552. (23).  Finally, it restores the original video mode and screen attributes
  553. before returning to DOS.
  554.  
  555.                                  Example 5-6.
  556.  
  557.                            #include <fastgraf.h>
  558.                            void main(void);
  559.  
  560.                            void main()
  561.                            {
  562.                               int mode;
  563.  
  564.                               mode = fg_getmode();
  565.                               fg_setmode(13);
  566.  
  567.                               fg_palette(0,1);
  568.                               fg_palette(1,22);
  569.  
  570.                               fg_setcolor(1);
  571.                               fg_text("Hello",5);
  572.                               fg_waitkey();
  573.  
  574. 58  Fastgraph User's Guide
  575.  
  576.                               fg_palette(1,23);
  577.                               fg_waitkey();
  578.  
  579.                               fg_setmode(mode);
  580.                               fg_reset();
  581.                            }
  582.  
  583.  
  584.  
  585. EGA Monochrome Mode
  586.  
  587.      The EGA monochrome graphics mode (mode 15) assigns display attributes to
  588. its four color values, numbered 0 to 3.  Each color value references one of
  589. four user-definable palette registers, often simply called palettes, numbered
  590. 0, 1, 4, and 5.  This numbering scheme might seem rather strange at first,
  591. but it results from the disabling of two of the four video memory bit planes
  592. in mode 15.  The values assigned to the palette registers determine the pixel
  593. display attribute.  For example, if you assign palette register 1 the value
  594. for bold, then pixels whose value is 1 will be bold.
  595.  
  596.      In mode 15, the fg_setcolor routine defines the current color (actually,
  597. a display attribute) by referencing one of the four palette registers.  The
  598. fg_palette routine defines the actual display attribute assigned to a
  599. specific palette register.  The first argument of the fg_palette routine is
  600. an integer that specifies the palette number.  The second argument is an
  601. integer that defines the palette value (the display attribute assigned to the
  602. palette).  For each palette register, the following table shows the default
  603. palette value and its associated display attribute.
  604.  
  605.                          palette  palette  display
  606.                          number   value    attribute
  607.  
  608.                             0        0     invisible
  609.                             1        8     normal
  610.                             4       24     bold
  611.                             5       24     bold
  612.  
  613.      Example 5-7 demonstrates the use of the fg_palette and fg_setcolor
  614. routines in mode 15.  After establishing the video mode, the program makes
  615. color 4 (actually, palette 4, which is bold by default) the current color and
  616. displays the word "Hello".  After waiting for a keystroke, the program
  617. changes the display attribute of "Hello" by changing palette 4 to normal
  618. intensity (palette value 8).  Finally, it restores the original video mode
  619. and screen attributes before returning to DOS.
  620.  
  621.                                  Example 5-7.
  622.  
  623.                            #include <fastgraf.h>
  624.                            void main(void);
  625.  
  626.                            void main()
  627.                            {
  628.                               int mode;
  629.  
  630.                                               Chapter 5:  The Use of Color  59
  631.  
  632.                               mode = fg_getmode();
  633.                               fg_setmode(15);
  634.  
  635.                               fg_setcolor(4);
  636.                               fg_text("Hello",5);
  637.                               fg_waitkey();
  638.  
  639.                               fg_palette(4,8);
  640.                               fg_waitkey();
  641.  
  642.                               fg_setmode(mode);
  643.                               fg_reset();
  644.                            }
  645.  
  646.  
  647.  
  648. EGA Enhanced Mode
  649.  
  650.      The EGA enhanced graphics mode (mode 16) has 16 color values, numbered 0
  651. to 15.  Each color value references one of 16 user-definable palette
  652. registers, often simply called palettes, also numbered 0 to 15.  The values
  653. assigned to the palette registers determine the colors in which pixels are
  654. displayed.  For example, if you assign palette register 2 the value for red,
  655. then pixels whose color value is 2 will be red.
  656.  
  657.      Each palette can assume one of 64 available colors.  By default, the
  658. values assigned to the 16 palettes correspond to the identically numbered
  659. colors in the standard color set.  In other words, palette 0 is assigned the
  660. value for black, palette 1 is assigned the value for blue, and so forth.
  661. There are a few EGA-compatible adapters that do not properly assign the
  662. default colors to the 16 palette registers, so it is a good practice to do
  663. this explicitly in mode 16.
  664.  
  665.      In mode 16, the fg_setcolor routine defines the current color value by
  666. referencing one of the 16 palette registers.  The fg_palette routine defines
  667. the actual color assigned to a specific palette register.  The first argument
  668. of the fg_palette routine is an integer between 0 and 15 that specifies the
  669. palette number.  The second argument is an integer that defines the palette
  670. value (the color assigned to the palette).  The binary structure of a palette
  671. value is different from the IRGB format used in the standard color set.  In
  672. mode 16, the binary structure of a palette value is a 6-bit quantity of the
  673. form rgbRGB, where the lower case letters represent the low intensity (1/3
  674. intensity) color components, and the upper case letters represent the normal
  675. intensity (2/3 intensity) color components.  The mode 16 palette values that
  676. correspond to the standard color set are:
  677.  
  678.                     value color      value color
  679.  
  680.                       0   black       56   gray
  681.                       1   blue        57   light blue
  682.                       2   green       58   light green
  683.                       3   cyan        59   light cyan
  684.                       4   red         60   light red
  685.                       5   magenta     61   light magenta
  686.                      20   brown       62   yellow
  687.                       7   white       63   bright white
  688.  
  689. 60  Fastgraph User's Guide
  690.  
  691.  
  692.      The normal intensity components in mode 16 produce the same normal
  693. intensity colors as in other 16-color graphics modes.  Similarly, combining
  694. the low and normal intensities in mode 16 produces the high intensity colors
  695. of the other modes.  The only exception to this is for the default brown,
  696. formed from the bit pattern 010100 (20 decimal).  This value produces a more
  697. true brown than the value 6 decimal, which is really an olive green.
  698.  
  699.      The palette values used in mode 16 are 6-bit quantities, which means
  700. there are 64 different colors available in mode 16.  This group of 64 colors
  701. consists of the 16 colors in the standard color set plus 48 additional colors
  702. that are not available in any of the other EGA modes.  However, because the
  703. EGA palette registers hold 4-bit quantities, only 16 of these colors can be
  704. displayed at the same time.  In other words, the EGA enhanced mode provides
  705. the capability of displaying 16 simultaneous colors from a group of 64.
  706.  
  707.      You also can use the Fastgraph routine fg_setrgb to define the color
  708. assigned to a specific palette register.  Whereas the fg_palette routine does
  709. this using a value between 0 and 63, fg_setrgb defines a palette register
  710. using red, green, and blue color components.  The first argument of the
  711. fg_setrgb routine is an integer between 0 and 15 that specifies the palette
  712. register number.  The remaining three arguments are each integer values
  713. between 0 and 3 that respectively specify the intensities in thirds of the
  714. red, green, and blue color components for that palette register.  For
  715. example, the color cyan is represented by the value 3 in the above table, and
  716. it is produced by combining normal intensity (2/3 intensity) green and blue.
  717. This means either of the statements
  718.  
  719.                              fg_palette(1,3);
  720.                              fg_setrgb(1,0,2,2);
  721.  
  722. could be used to define palette register 1 as cyan.
  723.  
  724.      Example 5-8 demonstrates the use of the fg_palette and fg_setcolor
  725. routines in mode 16.  It uses the Fastgraph routine fg_rect (discussed in the
  726. next chapter) to draw rectangles of a specified size.  After establishing the
  727. video mode, the program uses a for loop to draw 16 equal-size rectangles, one
  728. in each of the 16 color values.  In the same loop, the program uses the
  729. fg_palette routine to change each palette to black.  The while loop that
  730. follows performs four iterations.  The first iteration changes palette 0 to
  731. 0, palette 1 to 1, and so forth.  Hence, the 16 rectangles appear in the
  732. palette values 0 to 15.  The rectangles remain in these colors until is key
  733. is pressed to begin the next iteration.  The second iteration changes palette
  734. 0 to 16, palette 1 to 17, and so forth.  This makes the 16 rectangles appear
  735. in the palette values 16 to 31.  Iterations three and four are similar, so
  736. the overall effect of the program is to display all 64 colors, 16 at a time.
  737. Finally, the program restores the original video mode and screen attributes
  738. before returning to DOS.
  739.                                               Chapter 5:  The Use of Color  61
  740.  
  741.                                  Example 5-8.
  742.  
  743.                 #include <fastgraf.h>
  744.                 void main(void);
  745.  
  746.                 #define COLORS 16
  747.                 #define WIDTH  40
  748.  
  749.                 void main()
  750.                 {
  751.                    int base;
  752.                    int color;
  753.                    int minx, maxx;
  754.                    int mode;
  755.  
  756.                    mode = fg_getmode();
  757.                    fg_setmode(16);
  758.  
  759.                    base = 0;
  760.                    minx = 0;
  761.                    maxx = WIDTH - 1;
  762.  
  763.                    for (color = 0; color < COLORS; color++) {
  764.                       fg_palette(color,0);
  765.                       fg_setcolor(color);
  766.                       fg_rect(minx,maxx,0,349);
  767.                       minx = maxx + 1;
  768.                       maxx = maxx + WIDTH;
  769.                       }
  770.  
  771.                    while (base < COLORS*4) {
  772.                       for (color = 0; color < COLORS; color++)
  773.                          fg_palette(color,base+color);
  774.                       base += COLORS;
  775.                       fg_waitkey();
  776.                       }
  777.  
  778.                    fg_setmode(mode);
  779.                    fg_reset();
  780.                 }
  781.  
  782. VGA and MCGA Two-Color Mode
  783.  
  784.      The VGA and MCGA high-resolution two-color mode (mode 17) has a
  785. background color (color value 0) and a foreground color (color value 1).
  786. Each color value references one of two user-definable palette registers,
  787. often simply called palettes, also numbered 0 and 1.  Each palette register
  788. in turn references one of 16 user-definable 18-bit video DAC registers,
  789. numbered 0 to 15.  The values assigned to the palette registers and video DAC
  790. registers determine the colors in which pixels are displayed.  For example,
  791. if palette register 1 contains the value 3, and video DAC register 3 contains
  792. the color value for red, then pixels whose color value is 1 (that is, the
  793. foreground pixels) will be red.
  794.  
  795.      By default, palette register 0 references video DAC register 0, and
  796. palette register 1 references video DAC register 1.  In addition, video DAC
  797. register 0 initially contains the color value for black, while the other 15
  798. 62  Fastgraph User's Guide
  799.  
  800. video DAC registers (1 through 15) contain the color value for bright white.
  801. This means background pixels (color value 0) are black by default, while
  802. foreground pixels (color value 1) are bright white.
  803.  
  804.      The 18-bit video DAC values consist of three 6-bit red, green, and blue
  805. color components.  Hence, each color component is an integer between 0 and
  806. 63; increasing values produce more intense colors.  The default color
  807. components for DAC register 0 are red=0, blue=0, and green=0, which produces
  808. black.  The default values for the other DAC registers are red=63, blue=63,
  809. and green=63, which produces bright white.  Because the video DAC registers
  810. are 18 bits long, each DAC can specify one of 262,144 (218) colors.  However,
  811. because the palette registers hold 1-bit quantities, only two of these colors
  812. can be displayed at the same time.  In other words, mode 17 provides the
  813. capability of displaying two simultaneous colors from a group of 262,144.
  814.  
  815.      In mode 17, the fg_setcolor routine defines the current color by
  816. referencing one of the two palette registers.  The fg_palette routine defines
  817. the value of a palette register by referencing one of the 16 video DAC
  818. registers.  That is, the fg_palette routine specifies the video DAC register
  819. that a palette register references.  The first argument of the fg_palette
  820. routine is either 0 or 1 and specifies the palette number.  The second
  821. argument is an integer between 0 and 15 that specifies the video DAC register
  822. for that palette.
  823.  
  824.      The Fastgraph routine fg_setrgb defines the value of a video DAC
  825. register in mode 17.  The first argument of the fg_setrgb routine is an
  826. integer between 0 and 15 that specifies the DAC register number.  The
  827. remaining three arguments are each integer values between 0 and 63 that
  828. respectively specify the red, green, and blue color components for that DAC
  829. register.
  830.  
  831.      Example 5-9 demonstrates the use of the fg_palette, fg_setrgb, and
  832. fg_setcolor routines in mode 17.  After establishing the video mode, the
  833. program defines DAC register 0 to be blue (red=0, green=0, blue=42) and DAC
  834. register 1 to be yellow (red=63, green=63, blue=21).  Note that defining DAC
  835. register 0 changes the background color because palette 0 references DAC
  836. register 0.  The program then makes color 1 the current color (palette 1
  837. still references DAC register 1) and displays the word "Hello" in yellow.
  838. After waiting for a keystroke, the program changes the color of "Hello" by
  839. making palette 1 reference DAC register 15 (which still contains its default
  840. value, bright white).  Finally, it restores the original video mode and
  841. screen attributes before returning to DOS.
  842.  
  843.                                  Example 5-9.
  844.  
  845.                           #include <fastgraf.h>
  846.                           void main(void);
  847.  
  848.                           void main()
  849.                           {
  850.                              int mode;
  851.  
  852.                              mode = fg_getmode();
  853.                              fg_setmode(17);
  854.                              fg_setrgb(0,0,0,42);
  855.                              fg_setrgb(1,63,63,21);
  856.  
  857.                                               Chapter 5:  The Use of Color  63
  858.  
  859.                              fg_setcolor(1);
  860.                              fg_text("Hello",5);
  861.                              fg_waitkey();
  862.  
  863.                              fg_palette(1,15);
  864.                              fg_waitkey();
  865.  
  866.                              fg_setmode(mode);
  867.                              fg_reset();
  868.                           }
  869.  
  870.  
  871.  
  872. VGA 16-Color Mode
  873.  
  874.      The VGA high-resolution two-color mode (mode 18) has 16 color values,
  875. numbered 0 to 15.  Each color value references one of 16 user-definable
  876. palette registers, often simply called palettes, also numbered 0 to 15.  Each
  877. palette register in turn references one of 16 user-definable 18-bit video DAC
  878. registers, also numbered 0 to 15.  The values assigned to the palette
  879. registers and video DAC registers determine the colors in which pixels are
  880. displayed.  For example, if palette register 1 contains the value 3, and
  881. video DAC register 3 contains the color value for red, then pixels whose
  882. color value is 1 will be red.
  883.  
  884.      By default, each of the 16 palette registers references the video DAC
  885. register of the same number.  In addition, the 16 video DAC registers
  886. respectively contain the color values for the 16 colors in the standard color
  887. set.
  888.  
  889.      The 18-bit video DAC values consist of three 6-bit red, green, and blue
  890. color components.  Hence, each color component is an integer between 0 and
  891. 63; increasing values produce more intense colors.  The default RGB color
  892. components for the 16 video DAC registers are:
  893.  
  894.  
  895.          DAC   R   G   B  color      DAC   R   G   B  color
  896.  
  897.           0    0   0   0  black       8   21  21  21  gray
  898.           1    0   0  42  blue        9   21  21  63  light blue
  899.           2    0  42   0  green      10   21  63  21  light green
  900.           3    0  42  42  cyan       11   21  63  63  light cyan
  901.           4   42   0   0  red        12   63  21  21  light red
  902.           5   42   0  42  magenta    13   63  21  63  light magenta
  903.           6   42  21   0  brown      14   63  63  21  yellow
  904.           7   42  42  42  white      15   63  63  63  bright white
  905.  
  906.  
  907. Because the video DAC registers are 18 bits long, each DAC can specify one of
  908. 262,144 (218) colors.  However, because the palette registers hold 4-bit
  909. quantities, only 16 of these colors can be displayed at the same time.  In
  910. other words, mode 18 provides the capability of displaying 16 simultaneous
  911. colors from a group of 262,144.
  912. 64  Fastgraph User's Guide
  913.  
  914.  
  915.      In mode 18, the fg_setcolor, fg_palette, and fg_setrgb routines function
  916. exactly as in mode 17 with one exception:  there are 16 palette registers
  917. instead of just two.  Example 5-9 on page 62 demonstrates the use of these
  918. routines in mode 17, but it also would work in mode 18 if that video mode
  919. number were specified in the call to fg_setmode.
  920.  
  921.  
  922. VGA and MCGA 256-Color Modes
  923.  
  924.      The VGA and MCGA 256-color modes (modes 19 through 23) have 256 color
  925. values, numbered 0 to 255.  Each color value directly references one of 256
  926. user-definable 18-bit video DAC registers, also numbered 0 to 255.  The
  927. values assigned to the video DAC registers determine the colors in which
  928. pixels are displayed.  For example, if video DAC register 3 contains the
  929. color value for red, then pixels whose color value is 3 will be red.
  930.  
  931.      By default, the first 16 video DAC registers (0 to 15) contain the color
  932. values for the standard color set.  The next 16 DAC registers (16 to 31)
  933. contain the color values for a gray scale of gradually increasing intensity.
  934. The next 216 DAC registers (32 to 247) contain three groups of 72 colors
  935. each, with the first group (32 to 103) at high intensity, the second group
  936. (104 to 175) at moderate intensity, and the third group (176 to 247) at low
  937. intensity.  Each group consists of three ranges of decreasing saturation
  938. (increasing whiteness), with each range varying in hue from blue to red to
  939. green.  Finally, the last 8 DAC registers (248 to 255) alternate between
  940. black and bright white.  This information is summarized in the following
  941. table.
  942.  
  943.           DACs        default color values
  944.  
  945.           0 to 15     standard color set
  946.           16 to 31    gray scale of gradually increasing intensity
  947.           32 to 55    high saturation, high intensity colors
  948.           56 to 79    moderate saturation, high intensity colors
  949.           80 to 103   low saturation, high intensity colors
  950.           104 to 127  high saturation, moderate intensity colors
  951.           128 to 151  moderate saturation, moderate intensity colors
  952.           152 to 175  low saturation, moderateintensity colors
  953.           176 to 199  high saturation, low intensity colors
  954.           200 to 223  moderate saturation, low intensity colors
  955.           224 to 247  low saturation, low intensity colors
  956.           248 to 255  alternate between black and bright white
  957.  
  958.                                               Chapter 5:  The Use of Color  65
  959.  
  960.      The 18-bit video DAC values consist of three 6-bit red, green, and blue
  961. color components.  Hence, each color component is an integer between 0 and
  962. 63; increasing values produce more intense colors.  Because the video DAC
  963. registers are 18 bits long, each DAC can specify one of 262,144 (218) colors.
  964. However, because the color values are 8-bit quantities, only 256 of these
  965. colors can be displayed at the same time.  In other words, modes 19 through
  966. 23 provide the capability of displaying 256 simultaneous colors from a group
  967. of 262,144.
  968.  
  969.      In the VGA and MCGA 256-color video modes, the fg_setcolor routine
  970. defines the current color by referencing on of the 256 video DAC registers.
  971. The fg_setrgb routine defines the actual color of a video DAC register.  The
  972. first argument of the fg_setrgb routine is an integer between 0 and 255 that
  973. specifies the DAC register number.  The remaining three arguments are each
  974. integer values between 0 and 63 that respectively specify the red, green, and
  975. blue color components for that DAC register.  Another Fastgraph routine,
  976. fg_getrgb, returns the color components for a specified DAC register.  Its
  977. arguments are the same as for fg_setrgb, except the last three arguments (the
  978. return values) are passed by reference rather than by value.
  979.  
  980.      You also can use the Fastgraph routine fg_palette to define the value of
  981. a video DAC register in modes 19 through 23.  The first argument of the
  982. fg_palette routine is an integer between 0 and 255 that specifies the DAC
  983. register number.  The second argument is an integer between 0 and 63 that
  984. specifies the color value for that video DAC register, using the same 64
  985. values as in the EGA enhanced mode (mode 16).
  986.  
  987.      Example 5-10 demonstrates the use of the fg_setcolor routine in mode 19.
  988. The program uses the Fastgraph routine fg_rect to draw vertical lines.  After
  989. establishing the video mode, the program uses a for loop to draw 256 vertical
  990. lines, one in each of the 256 colors (using the default DAC values).
  991. Finally, the program restores the original video mode and screen attributes
  992. before returning to DOS.
  993.  
  994.    Example 5-10.                                Example 5-11.
  995.  
  996. #include<fastgraf.h>                    #include <fastgraf.h>
  997. void main(void);                        void main(void);
  998.  
  999. #define COLORS 256                      void main()
  1000.                                         {
  1001. void main()                                int old_mode;
  1002. {                                          int red,green, blue;
  1003.    int base;
  1004.    int color;                              old_mode = fg_getmode();
  1005.    int mode;                               fg_setmode(19);
  1006.  
  1007. 66  Fastgraph User's Guide
  1008.  
  1009.   int x;
  1010.                                            fg_setcolor(103);
  1011.   mode = fg_getmode();                     fg_text("Hello",5);
  1012.   fg_setmode(19);                          fg_waitfor(18);
  1013.  
  1014.   x = 32;                                  fg_getrgb(103,&red,&green,&blue);
  1015.  
  1016.   for (color=0; color<COLORS; color++) {   while(red+green+blue > 0){
  1017.      fg_setcolor(color);                      if (red > 0) red--;
  1018.      fg_rect(x,x,0,199);                      if (green > 0) green--;
  1019.      x++;                                     if (blue > 0) blue--;
  1020.      }                                        fg_setrgb(103,red,green,blue);
  1021.   fg_waitkey();                               fg_waitfor(1);
  1022.                                               }
  1023.   fg_setmode(mode);
  1024.   fg_reset();                              fg_setmode(old_mode);
  1025. }                                          fg_reset();
  1026.                                           }
  1027.  
  1028.  
  1029.      Example 5-11 shows an interesting effect available in VGA and MCGA
  1030. modes.  The program uses the Fastgraph routine fg_waitfor (discussed in
  1031. Chapter 14) to delay the program's execution.  After establishing the video
  1032. mode, the program displays the word "Hello" in color 103, which by default is
  1033. a pastel blue.  It then uses the Fastgraph routine fg_getrgb to retrieve the
  1034. color components for this color.  The while loop gradually decreases the
  1035. color components until all three components are zero, which makes the word
  1036. "Hello" smoothly fade to black.  Finally, the program restores the original
  1037. video mode and screen attributes before returning to DOS.
  1038.  
  1039.      The fg_setrgb and fg_getrgb routines work with individual DAC registers.
  1040. If you want to define or retrieve a block of consecutive DAC registers, using
  1041. the fg_setdacs and fg_getdacs routines is more efficient.  The fg_setdacs
  1042. routine defines the values of a block of contiguous DAC registers.  Its first
  1043. argument is the index of the first DAC register to define (between 0 and
  1044. 255), and its second argument is the number of DAC registers to define
  1045. (between 1 and 256).  The third argument is a byte array containing the RGB
  1046. color components for the DAC registers being defined.  The array's first
  1047. three bytes contain the red, green, and blue components for the first DAC,
  1048.                                               Chapter 5:  The Use of Color  67
  1049.  
  1050. the next three for the second DAC, and so forth.  The size of this array must
  1051. be at least three times the value of the second argument.  The fg_getdacs
  1052. arguments are the same as those for fg_setdacs, but the RGB array instead
  1053. receives the current values of the specified DAC registers.  Both routines
  1054. treat the DAC register numbers in a circular fashion (for example, defining
  1055. four DACs starting with number 254 will define DACs 254, 255, 0, and 1).
  1056.  
  1057.      Example 5-12 is similar to example 5-11, but it fades many colors
  1058. simultaneously.  The program displays seven asterisks, one each in colors 9
  1059. through 15.  It uses fg_getdacs to obtain the current settings of the
  1060. corresponding DAC registers; these values are stored in the array RGBvalues.
  1061. The while loop gradually fades the RGB components to zero, using fg_setdacs
  1062. to update their values, similar to the method of example 5-11.  This
  1063. illustrates an attractive way of turning an image into a blank screen.
  1064.  
  1065.                                 Example 5-12.
  1066.  
  1067.                        #include <fastgraf.h>
  1068.                        void main(void);
  1069.  
  1070.                        void main()
  1071.                        {
  1072.                           int decreasing;
  1073.                           int i;
  1074.                           int old_mode;
  1075.                           char RGBvalues[21];
  1076.  
  1077.                           old_mode = fg_getmode();
  1078.                           fg_setmode(19);
  1079.  
  1080.                           for (i = 9; i <= 15; i++) {
  1081.                              fg_setcolor(i);
  1082.                              fg_text("*",1);
  1083.                              }
  1084.  
  1085.                           fg_getdacs(9,7,RGBvalues);
  1086.                           fg_waitfor(18);
  1087.  
  1088.                           do {
  1089.                              decreasing = 0;
  1090.                              for (i = 0; i < 21; i++)
  1091.                                 if (RGBvalues[i] > 0) {
  1092.                                    RGBvalues[i]--;
  1093.                                    decreasing = 1;
  1094.                                    }
  1095.                              fg_setdacs(9,7,RGBvalues);
  1096.                              fg_waitfor(1);
  1097.                              }
  1098.                           while (decreasing);
  1099.  
  1100.                           fg_setmode(old_mode);
  1101.                           fg_reset();
  1102.                        }
  1103.  
  1104.  
  1105. Note that examples 5-11 and 5-12 also would work in video modes 17 and 18 as
  1106. long as you just use the first 16 video DAC registers.
  1107. 68  Fastgraph User's Guide
  1108.  
  1109.  
  1110. RGB Color Mapping
  1111.  
  1112.      If you're developing an application that runs in 256-color and 16-color
  1113. graphics modes, you've probably noticed the inherent differences in defining
  1114. color values.  In fact, the palette register values even use different
  1115. structures within the various 16-color modes.  The Fastgraph routine
  1116. fg_maprgb helps simplify these differences.  It maps three RGB color
  1117. components (each between 0 and 63) into a 16-color palette value suitable for
  1118. the current video mode.  Of course, the range of available colors is much
  1119. more restricted in the 16-color modes than in the 256-color modes, so
  1120. fg_maprgb must map the RGB components into the closest available color.
  1121.  
  1122.      Example 5-13 runs in any 16-color or 256-color graphics mode and
  1123. demonstrates the use of the fg_maprgb routine.  In 256-color modes, the
  1124. program simply uses fg_setrgb to define DAC register 1 to a pastel blue
  1125. (red=45, green=49, blue=63).  In 16-color modes, however, the program calls
  1126. fg_maprgb to convert the color components into a palette value in IRGB,
  1127. IxRGB, or rgbRGB format (depending on the current video mode).  The fg_maprgb
  1128. return value is passed to fg_palette to set palette register 1 to the closest
  1129. available color defined by the specified RGB components.
  1130.  
  1131.                                 Example 5-13.
  1132.  
  1133.           #include <fastgraf.h>
  1134.           #include <stdio.h>
  1135.           #include <stdlib.h>
  1136.           void main(void);
  1137.  
  1138.           void main()
  1139.           {
  1140.              int new_mode, old_mode;
  1141.  
  1142.              new_mode = fg_bestmode(320,200,1);
  1143.              if (new_mode < 0 || new_mode == 4 || new_mode == 12) {
  1144.                 printf("This program requires a 320 x 200 ");
  1145.                 printf("16-color or 256-color graphics mode.\n");
  1146.                 exit(1);
  1147.                 }
  1148.              old_mode = fg_getmode();
  1149.              fg_setmode(new_mode);
  1150.  
  1151.              fg_setcolor(1);
  1152.              if (new_mode <= 16)
  1153.                 fg_palette(1,fg_maprgb(45,49,63));
  1154.              else
  1155.                 fg_setrgb(1,45,49,63);
  1156.              fg_text("Hello",5);
  1157.              fg_waitkey();
  1158.  
  1159.              fg_setmode(old_mode);
  1160.              fg_reset();
  1161.           }
  1162.  
  1163.                                               Chapter 5:  The Use of Color  69
  1164.  
  1165.  
  1166. Defining All Palette Registers
  1167.  
  1168.      Fastgraph includes a routine fg_palettes that defines all 16 palette
  1169. registers in modes 9, 13, 14, 16, and 18.  You also can use the fg_palettes
  1170. routine to define the first 16 video DAC registers in modes 19 through 23.
  1171. It has no effect in other video modes.
  1172.  
  1173.      Using fg_palettes is much faster than calling the fg_palette routine 16
  1174. times.  The argument to the fg_palettes routine is a 16-element integer array
  1175. that contains the color values assigned respectively to palette registers (or
  1176. video DAC registers) 0 to 15.  Example 5-14 demonstrates how to zero the
  1177. palette registers (that is, change them all to black) in mode 13.
  1178.  
  1179.                                 Example 5-14.
  1180.  
  1181.               #include <fastgraf.h>
  1182.               void main(void);
  1183.  
  1184.               int zeroes[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  1185.  
  1186.               void main()
  1187.               {
  1188.                  int mode;
  1189.  
  1190.                  mode = fg_getmode();
  1191.                  fg_setmode(13);
  1192.  
  1193.                  fg_palettes(zeroes);
  1194.  
  1195.                  fg_setmode(mode);
  1196.                  fg_reset();
  1197.               }
  1198.  
  1199. Of course, as this example is written, it appears to do nothing more than
  1200. blank the screen.  Its purpose is to show an example of the fg_palettes
  1201. routine.
  1202.  
  1203.  
  1204. Virtual Colors
  1205.  
  1206.      By this time it should be clear the use of color is rather specific to
  1207. each graphics video mode.  One of the most obvious differences is the number
  1208. of available colors in each mode; it ranges from 2 to 256.  By available
  1209. colors, we mean the number of colors that can be displayed simultaneously.
  1210.  
  1211.      To simplify programming in graphics modes, Fastgraph provides 256
  1212. virtual colors.  The virtual colors are used in the graphics video modes
  1213. having fewer than 256 available colors.  Virtual colors allow you to use 256
  1214. color indices in all graphics modes, even if a particular mode does not have
  1215. 256 available colors.
  1216.  
  1217.      When you establish a video mode with the fg_setmode routine, Fastgraph
  1218. initializes all the virtual color indices.  It does this by replicating the
  1219. video mode's color values through the 256 virtual color indices.  For
  1220. example, the CGA color modes (4 and 5) have four color values, numbered 0
  1221. through 3.  In these modes, the fg_setmode routine initializes color indices
  1222. 70  Fastgraph User's Guide
  1223.  
  1224. 0, 4, 8, ... , 252 to 0; color indices 1, 5, 9, ... , 253 to 1; color indices
  1225. 2, 6, 10, ... , 254 to 2; and color indices 3, 7, 11, ... , 255 to 3.
  1226. Similarly, in 16-color graphics modes the color indices 0, 16, 32, ... , 240
  1227. are set to 0, and so forth.  In the monochrome EGA graphics video mode (mode
  1228. 15), the color values are numbered 0, 1, 4, and 5, so fg_setmode replicates
  1229. the color indices in groups of eight, even though there are only four
  1230. available colors.  An analysis of the color value sequences reveals an often
  1231. useful feature:  by default, virtual color 0 is black and virtual color 15 is
  1232. white or bright white in all graphics video modes.
  1233.  
  1234.      It is thus possible to write a multiple-mode program using the same
  1235. color indices for each graphics mode.  For example, a program that contains
  1236. the statement fg_setcolor(5) would produce subsequent graphics in color 5
  1237. (magenta by default) when running in a 16-color graphics mode.  It would
  1238. produce subsequent graphics in color 1 (light cyan by default) when running
  1239. in a CGA color mode.  This is because 1 is the default value assigned to
  1240. virtual color index 5 in the CGA color modes.
  1241.  
  1242.      The fg_setmode routine establishes default values for the 256 virtual
  1243. color indices, but it might be desirable to assign other available colors to
  1244. them.  Going back to the discussion in the previous paragraph, color number 2
  1245. is light magenta in the default CGA mode 4 palette.  It might make more sense
  1246. if the color value 2 were assigned to virtual color index 5, as this would
  1247. make the graphics drawn in color 5 the same color in mode 4 as in other color
  1248. modes.  The Fastgraph routine fg_defcolor is provided for this purpose.
  1249.  
  1250.      The fg_defcolor routine assigns a color value to a virtual color index.
  1251. It has two arguments:  the first specifies the virtual color index (between 0
  1252. and 255), and the second specifies the color value (between 0 and the number
  1253. of available colors in the current video mode).  For example, the statement
  1254.  
  1255.                               fg_defcolor(5,2);
  1256.  
  1257. would assign the color value 2 to the color index 5.  Another Fastgraph
  1258. routine, fg_getindex, returns the current value assigned to a specified color
  1259. index.  After executing the above call to fg_defcolor, the statement
  1260.  
  1261.                            color = fg_getindex(5);
  1262.  
  1263. would store the value 2 (the current value of color index 5) in the integer
  1264. variable color.
  1265.  
  1266.      It is important to understand the difference between virtual colors and
  1267. palette registers.  Modifying the value of a palette register changes the
  1268. color of all pixels already drawn using that palette.  Modifying a virtual
  1269. color index does not do this; it only specifies any graphics drawn in that
  1270. color from this point on will appear in the new color.
  1271.  
  1272.      Example 5-15 demonstrates the use of virtual colors in mode 4.  After
  1273. establishing the video mode, the program uses the fg_defcolor routine to
  1274. define virtual color indices 0 and 255 to be 1, which by default is light
  1275. cyan in mode 4.  It then draws characters using color indices 0, 1, and 255,
  1276. and in each case the characters appear in light cyan.  Finally, the program
  1277. restores the original video mode and screen attributes before returning to
  1278. DOS.
  1279.                                               Chapter 5:  The Use of Color  71
  1280.  
  1281.                                 Example 5-15.
  1282.  
  1283.                            #include <fastgraf.h>
  1284.                            void main(void);
  1285.  
  1286.                            void main()
  1287.                            {
  1288.                               int mode;
  1289.  
  1290.                               mode = fg_getmode();
  1291.                               fg_setmode(4);
  1292.  
  1293.                               fg_defcolor(0,1);
  1294.                               fg_defcolor(255,1);
  1295.  
  1296.                               fg_setcolor(0);
  1297.                               fg_text("0",1);
  1298.                               fg_setcolor(1);
  1299.                               fg_text(" 1",2);
  1300.                               fg_setcolor(255);
  1301.                               fg_text(" 255",4);
  1302.                               fg_waitkey();
  1303.  
  1304.                               fg_setmode(mode);
  1305.                               fg_reset();
  1306.                            }
  1307.  
  1308.  
  1309. A Multiple-Mode Example
  1310.  
  1311.      Even though the color capabilities differ between the supported video
  1312. modes, Fastgraph makes it easy to write a program that runs in many video
  1313. modes.  This section will present an example of such a program.
  1314.  
  1315.      Example 5-16 illustrates a program that will run in any of Fastgraph's
  1316. supported video modes.  The program first asks for the video mode number,
  1317. checks if the mode number is valid, and then checks if the requested mode is
  1318. available on the user's system.  After doing this, the program establishes
  1319. the video mode and performs its mode-specific code.  It then displays a brief
  1320. message that includes the video mode number in which the program is running.
  1321. This information remains on the screen until a key is pressed, at which time
  1322. the program restores the original video mode and screen attributes before
  1323. returning to DOS.
  1324.  
  1325.                                 Example 5-16.
  1326.  
  1327.        #include <fastgraf.h>
  1328.        #include <stdio.h>
  1329.        #include <stdlib.h>
  1330.        void main(void);
  1331.  
  1332.        void main()
  1333.        {
  1334.           int mode, old_mode;
  1335.           char string[5];
  1336.  
  1337. 72  Fastgraph User's Guide
  1338.  
  1339.        /* Ask for the video mode number */
  1340.           printf("Which video mode? ");
  1341.           scanf("%d",&mode);
  1342.  
  1343.        /* Make sure the entered value is valid */
  1344.           if (mode < 0 || mode > 23) {
  1345.              printf("%d is not a valid video mode number.\n",mode);
  1346.              exit(1);
  1347.              }
  1348.  
  1349.        /* Make sure the requested video mode is available */
  1350.           if (fg_testmode(mode,1) == 0) {
  1351.              printf("Mode %d is not available on this system.\n",mode);
  1352.              exit(1);
  1353.              }
  1354.  
  1355.        /* Establish the video mode */
  1356.           old_mode = fg_getmode();
  1357.           fg_setmode(mode);
  1358.  
  1359.        /* Perform mode-specific initializations */
  1360.           if (mode <= 3 || mode == 7)   /* text modes */
  1361.              fg_cursor(0);
  1362.  
  1363.           else if (mode == 4 || mode == 5) { /* CGA color modes */
  1364.              fg_palette(0,0);
  1365.              fg_defcolor(14,3);
  1366.              }
  1367.  
  1368.           else if (mode == 6) {         /* CGA two-color mode */
  1369.              fg_palette(0,14);
  1370.              fg_defcolor(14,1);
  1371.              }
  1372.  
  1373.           else if (mode == 11)          /* Hercules mode */
  1374.              fg_defcolor(14,1);
  1375.  
  1376.           else if (mode == 12)          /* Hercules low-res mode */
  1377.              fg_defcolor(14,3);
  1378.  
  1379.           else if (mode == 17) {        /* VGA two-color mode */
  1380.              fg_palette(1,14);
  1381.              fg_setrgb(14,63,63,21);
  1382.              fg_defcolor(14,1);
  1383.              }
  1384.  
  1385.        /* Display a message that includes the video mode number */
  1386.           fg_setcolor(14);
  1387.           fg_text("I'm running in mode ",20);
  1388.           sprintf(string,"%d. ",mode);
  1389.           fg_text(string,3);
  1390.  
  1391.        /* Wait for a keystroke */
  1392.           fg_waitkey();
  1393.  
  1394.                                               Chapter 5:  The Use of Color  73
  1395.  
  1396.        /* Restore the original video mode and screen attributes */
  1397.           fg_setmode(old_mode);
  1398.           fg_reset();
  1399.        }
  1400.  
  1401.      Example 5-16 displays its message in yellow for those video modes that
  1402. offer color.  In monochrome video modes, it displays the message in normal
  1403. intensity.  The program uses virtual color 14, which by default is yellow in
  1404. many video modes; the mode-specific code in example 5-16 makes color 14 yellow
  1405. in other video modes.  In text video modes (modes 0 to 3 and 7), the program
  1406. uses the fg_cursor routine to make the cursor invisible.  In CGA color modes
  1407. (modes 4 and 5), the program uses the fg_palette routine to select a CGA
  1408. palette that contains yellow as color 3 and then uses fg_defcolor to assign
  1409. color 3 to virtual color 14.  In CGA two-color mode (mode 6), the program uses
  1410. the fg_palette routine to make color 1 yellow and then uses fg_defcolor to
  1411. assign color 1 to virtual color 14.  In the Hercules modes (modes 11 and 12),
  1412. the program uses the fg_defcolor routine to assign the value for normal
  1413. intensity pixels to color 14.  In VGA two-color mode (mode 17), the program
  1414. uses the fg_palette routine to assign video DAC register 14 to palette
  1415. register 1.  It then defines video DAC register 14 to be yellow with the
  1416. fg_setrgb routine and finally uses fg_defcolor to assign color 1 (that is,
  1417. palette register 1) to virtual color 14.  In all other video modes, color 14
  1418. is yellow by default.
  1419.  
  1420.  
  1421. Summary of Color-Related Routines
  1422.  
  1423.      This section summarizes the functional descriptions of the Fastgraph
  1424. routines presented in this chapter.  More detailed information about these
  1425. routines, including their arguments and return values, may be found in the
  1426. Fastgraph Reference Manual.
  1427.  
  1428.      FG_DEFCOLOR assigns a color value to a virtual color index.  This routine
  1429. is only meaningful in the graphics video modes that have fewer than 256
  1430. available colors.
  1431.  
  1432.      FG_GETCOLOR returns the current text attribute (in text modes) or color
  1433. index (in graphics modes), as specified in the most recent call to fg_setattr
  1434. or fg_setcolor.
  1435.  
  1436.      FG_GETDACS retrieves the red, green, and blue color components for a
  1437. block of consecutively numbered video DAC registers.  This routine is only
  1438. meaningful in VGA and MCGA graphics modes.
  1439.  
  1440.      FG_GETINDEX returns the color value assigned to a specified virtual
  1441. color index.  In text modes and in graphics modes that have 256 available
  1442. colors, this routine returns the value passed to it.
  1443.  
  1444.      FG_GETRGB returns the red, green, and blue color components for a
  1445. specified video DAC register.  This routine is only meaningful in VGA and
  1446. MCGA graphics modes.
  1447.  
  1448.       FG_MAPRGB maps six-bit red, green, and blue color components into a
  1449. suitable palette value for the current video mode.  You can then pass this
  1450. value to the fg_palette routine.  This routine is meaningful only in 16-color
  1451. graphics video modes.
  1452. 74  Fastgraph User's Guide
  1453.  
  1454.      FG_PALETTE has different functions depending on the current graphics
  1455. video mode.  For the CGA four-color modes, it establishes the current palette
  1456. (of six available) and defines the background color for that palette.  In the
  1457. CGA two-color mode, it defines the foreground color.  For the Tandy/PCjr,
  1458. EGA, and VGA graphics modes, it defines the value of a single palette
  1459. register.  For the 256-color MCGA and VGA graphics modes, it defines the
  1460. value of a single video DAC register.  The fg_palette routine has no effect
  1461. in text modes or Hercules graphics modes.
  1462.  
  1463.      FG_PALETTES defines all 16 palette registers (in Tandy/PCjr, EGA, and
  1464. VGA graphics modes), or the first 16 video DAC registers (in 256-color MCGA
  1465. and VGA graphics modes).  The fg_palettes routine has no effect in text
  1466. modes, CGA graphics modes, or Hercules graphics modes.
  1467.  
  1468.      FG_SETATTR establishes the current text attribute in text video modes.
  1469. This routine has no effect in graphics modes.
  1470.  
  1471.      FG_SETCOLOR establishes the current color index (which may be a virtual
  1472. color index in graphics modes).  In text modes, the fg_setcolor routine
  1473. provides an alternate method of establishing the current text attribute.
  1474.  
  1475.      FG_SETDACS defines the values of a block of consecutively numbered video
  1476. DAC registers by specifying their red, green, and blue color components.
  1477. This routine is only meaningful in VGA and MCGA graphics modes.
  1478.  
  1479.      FG_SETRGB defines the value of a single palette register (in Tandy/PCjr
  1480. and EGA graphics modes) or video DAC register (in VGA and MCGA modes) by
  1481. specifying its red, green, and blue color components.  The fg_setrgb routine
  1482. has no effect in text modes, CGA graphics modes, or Hercules graphics modes.
  1483.