Chapter 7: Color tables and color mapping systems
Previous Chapter LLUs Home Next Chapter
A color table is a table that associates color values, such as cyan or yellow, with nonnegative integer indices. This chapter describes how to define color tables in NCAR Graphics and how the associated color indices are used to assign colors to graphics primitives like polylines, polymarkers, text, and filled areas.
In this chapter you will also see how to convert from one "color space" to another.
A color space, or color model, is a three-dimensional coordinate system where coordinates in that system represent colors. There are many popular color spaces; some of them are more suitable for hardware and others are more suitable for human conceptualizing. Two of the more common color models used by hardware are the RGB (red, green, blue) model used by most workstations and the YIQ model used by broadcast TV. Two of the more popular models for human conceptualizing are the HLS (hue, lightness, saturation) and HSV (hue, saturation, value) models.
This chapter describes utilities for converting between the RGB, HLS, HSV, and YIQ models. For a complete description of these models consult the book Computer Graphics by James D. Foley, Andries van Dam, Steven K. Feiner, and John F. Hughes, (Addison-Wesley, 1990).
To provide sample graphics plots for the functions in this chapter would require the use of color; for this reason such plots have not been provided.
This chapter uses the notation [x,y), where x and y are real numbers, to mean the set of real numbers between x and y, including x, but not including y.
This chapter lists entry points for setting up color tables and for converting between several systems for representing color values. The table also lists entry points for assigning colors to the GKS primitives of polyline, polymarker, text and filled area.
- GSCR
- Sets color representation. Used to set up color tables.
- GSPLCI
- Sets polyline color index. Specifies a color index to be used for GKS polylines.
- GSPMCI
- Sets polymarker color index. Specifies a color index to be used for GKS polymarkers.
- GSTXCI
- Sets text color index. Specifies a color index to be used for GKS text.
- GSFACI
- Sets fill area color index. Specifies a color index to be used for GKS filled areas.
- HLSRGB
- Converts from the hue, lightness, saturation color model to the red, green, blue color model.
- RGBHLS
- Converts from the red, green, blue color model to the hue, lightness, saturation model.
- HSVRGB
- Converts from the hue, saturation, value color model to the red, green, blue model.
- RGBHSV
- Converts from the red, green, blue color model to the hue, saturation, value model.
- YIQRGB
- Converts from the YIQ color model to the red, green, blue model.
- RGBYIQ
- Converts from the red, green, blue color model to the YIQ model.
Color in NCAR Graphics uses an indexing scheme. A table is set up to associate actual color values with indices, and the indices are used to tell NCAR Graphics what color value to use. The GKS function "Set Color Representation," GSCR, is used to set up the color tables. There are various ways to specify color information in specific situations, but all ways ultimately will use color indices as set up in a color table. In some situations, a given function will inherit its color from the underlying GKS specifications. For example, when drawing lines with LINE, the lines will be drawn using the current GKS line color. This module shows how to specify colors for the basic GKS primitives of polylines (GSPLCI), filled areas (GSFACI), text (GSTXCI), and polymarkers (GSPMCI). For specifying colors in other situations, consult the documentation on color in the appropriate chapter.
1 CALL GSCR(IWK, 0, 0.0, 0.0, 0.0)
2 CALL GSCR(IWK, 1, 1.0, 1.0, 1.0)
3 CALL GSCR(IWK, 2, 1.0, 0.0, 0.0)
4 CALL GSCR(IWK, 3, 0.0, 1.0, 0.0)
5 CALL GSCR(IWK, 4, 1.0, 1.0, 0.0)
6 CALL GSCR(IWK, 5, 0.0, 1.0, 1.0)
7 CALL GSPLCI(3)
8 CALL GPL(5,X1,Y)
9 CALL GSMKSC(4.)
10 CALL GPM(1,.5,.25)
11 CALL GSTXCI(4)
12 CALL GTX(0.5,0.5,'Text')
13 CALL GSFACI(5)
14 CALL GSFAIS(1)
15 CALL GFA(5,X2,Y)
16 CALL GSPMCI(2)
17 CALL GPM(1,.5,.75)
CALL GSCR (WKID, CI, CR, CG, CB)
CALL GSPLCI (CI)
CALL GSTXCI (CI)
CALL GSFACI (CI)
CALL GSPMCI (CI)
- WKID
- Integer, Input ---A number assigned to a workstation as an identifier that is to be used in subsequent calls to GKS functions that require a workstation identifier. In NCAR GKS, WKID can be any nonnegative integer.
- CI
- Integer, Input ---A color index. CI can be any nonnegative integer and can be used in any subsequent calls that require a color index.
- CR
- Real, Input ---A number between 0. and 1. (inclusive) that gives an intensity for the red component of the color value that is to be associated with the color index CI.
- CG
- Real, Input ---A number between 0. and 1. (inclusive) that gives an intensity for the green component of the color value that is to be associated with the color index CI.
- CB
- Real, Input ---A number between 0. and 1. (inclusive) that gives an intensity for the blue component of the color value that is to be associated with the color index CI.
The fccex01.f code segment shows how to set up a color table and how to assign color values to the GKS primitives of polyline, polymarker, fill area, and text.
Lines 1 through 6 set up a color table. Line 1 sets the background color as black. Color index 0 is always used as the background color. Line 2 sets the foreground color as white. Color index 1 is always used as the foreground color.
Color index 2 is set to red in line 3; color index 3 is set to green in line 4; color index 4 is set to yellow in line 5; and color index 5 is set to cyan in line 6.
Notice that calls to GSCR require a GKS workstation ID, since color tables are workstation specific. You can define different color tables for different workstations. For example, color index 2 can signify red on an NCGM workstation and green on an X11 workstation. See the chapter on GKS workstations for details on workstations.
The GKS functions GSPLCI, GSTXCI, GSFACI, and GSPMCI specify what colors are used to draw the GKS primitives. Arguments to the GKS functions set the color indices, which in turn control what colors are used to draw the primitives. The default color for all primitives is the foreground color.
Line 7 specifies that lines will be drawn with color index 3 (green in the example) until another call to GSPLCI is made. Line 8 draws a rectangle using five points. This rectangle will be green.
Line 9 indicates that polymarkers are to be scaled by a factor of four and line 10 draws an asterisk polymarker (the default polymarker). This marker will be drawn in white (the foreground color) since no color index has been set for GPM by using GSPMCI.
Line 11 establishes the text color as yellow and line 12 draws a yellow text string.
Line 13 establishes cyan as the fill color; line 14 specifies that filled areas should be solid; and line 15 draws a filled rectangle in cyan.
Line 16 establishes red as the new marker color and line 17 draws a red polymarker.
In any given program it is best that all color indices that are to be used should be defined (including the background color and the foreground color), or no color indices should be defined. If no indices are defined, then implementation-dependent defaults will be used for all colors on a given output device.
Also, the results of dynamic color table settings (those occurring after plotting has begun) are not predictable, so color tables should be set before any graphic objects are drawn on a workstation.
HLSRGB and RGBHLS provide conversion between the HLS (hue, lightness, saturation) color space and the RGB (red, green, blue) space. HLSRGB converts HLS coordinates to RGB coordinates and RGBHLS converts RGB coordinates to HLS coordinates.
1 HUE = 120.
2 RLIGHT = 50.
3 SATR = 100.
4 CALL HLSRGB(HUE,RLIGHT,SATR,RED,GREEN,BLUE)
5 RED = 1.
6 GREEN = 0.
7 BLUE = 0.
8 CALL RGBHLS(RED,GREEN,BLUE,HUE,RLIGHT,SATR)
CALL HLSRGB (H, L, S, R, G, B)
CALL RGBHLS (R, G, B, H, L, S)
- H
- Real, Input for HLSRGB; Output for RGBHLS---A value for hue in the HLS color space. H is in the range [0.,360.).
- L
- Real, Input for HLSRGB; Output for RGBHLS---A value for lightness in the HLS color space. L is in the range [0.,100.].
- S
- Real, Input for HLSRGB; Output for RGBHLS---A value for saturation in the HLS color space. S is in the range [0.,100.].
- R
- Real, Input for RGBHLS; Output for HLSRGB---A value for red in the RGB color space. R is in the range [0.,1.].
- G
- Real, Input for RGBHLS; Output for HLSRGB---A value for green in the RGB color space. G is in the range [0.,1.].
- B
- Real, Input for RGBHLS; Output for HLSRGB---A value for blue in the RGB color space. B is in the range [0.,1.].
Lines 1 through 3 of code segment 1 from fcce02.f assign values to hue, lightness, and saturation in the HLS space that correspond to red=1., green=0., and blue=0. in the RGB space. The result of the call in line 4 is that R will be returned as 1., G as 0., and B as 0.
Lines 5 through 8 of code segment 1 from fcce02.f essentially reverse the process described in lines 1 through 4. Lines 5 through 7 assign values to red, green, and blue in the RGB space that correspond to hue=120., lightness=50., and saturation =100. in the HLS space. The result of the call in line 8 is that H will be returned as 120., L will be returned as 50., and S will be returned as 100.
HSVRGB and RGBHSV provide conversion between the HSV (hue, saturation, value) color space and the RGB (red, green, blue) space. HSVRGB converts HSV coordinates to RGB coordinates and RGBHSV converts RGB coordinates to HSV coordinates.
1 HUE = 120.
2 SATR = 1.
3 VALUE = 1.
4 CALL HSVRGB(HUE,SATR,VALUE,RED,GREEN,BLUE)
5 RED = 0.
6 GREEN = 0.
7 BLUE = 1.
8 CALL RGBHSV(RED,GREEN,BLUE,HUE,SATR,VALUE)
CALL HSVRGB (H, S, V, R, G, B)
CALL RGBHSV (R, G, B, H, S, V)
- H
- Real, Input for HSVRGB; Output for RGBHSV---A value for hue in the HSV color space. H is in the range [0.,360.).
- S
- Real, Input for HSVRGB; Output for RGBHSV---A value for lightness in the HSV color space. S is in the range [0.,1.].
- V
- Real, Input for HSVRGB; Output for RGBHSV---A value for the value component in the HSV color space. V is in the range [0.,1.].
- R
- Real, Input for RGBHSV; Output for HSVRGB ---A value for red in the RGB color space. R is in the range [0.,1.].
- G
- Real, Input for RGBHSV; Output for HSVRGB ---A value for green in the RGB color space. G is in the range [0.,1.].
- B
- Real, Input for RGBHSV; Output for HSVRGB ---A value for blue in the RGB color space. B is in the range [0.,1.].
Lines 1 through 3 of code segment 2 from fcce02.f assign values to hue, saturation, and value in the HSV space that correspond to red=0., green=1., and blue=0. in the RGB space. The result of the call in line 4 is that R will be returned as 0., G as 1., and B as 0.
Lines 5 through 7 of code segment 2 from fcce02.f assign values to red, green, and blue in the RGB space that correspond to values of hue=240., saturation=1., and value=1. in the HSV space. The result of the call in line 8 is that H will be returned as 240., S will be returned as 1., and V will be returned as 1.
YIQRGB and RGBYIQ provide conversion between the YIQ color model used by broadcast TV and the RGB (red, green, blue) model. YIQRGB converts YIQ coordinates to RGB coordinates and RGBYIQ converts RGB coordinates to YIQ coordinates.
1 Y = 0.59
2 RI = -.28
3 Q = -.52
4 CALL YIQRGB (Y,RI,Q,RED,GREEN,BLUE)
5 RED = 1.0
6 GREEN = 1.0
7 BLUE = 1.0
8 CALL RGBYIQ(RED,GREEN,BLUE,Y,RI,Q)
CALL YIQRGB (Y, I, Q, R, G, B)
CALL RGBYIQ (R, G, B, Y, I, Q)
- Y
- Real, Input for YIQRGB; Output for RGBYIQ---A value for the Y component of the YIQ color space. Y is in the range [0.,1.].
- I
- Real, Input for YIQRGB; Output for RGBYIQ---A value for the I component of the YIQ color space. I is in the range [-.6,.6].
- Q
- Real, Input for YIQRGB; Output for RGBYIQ---A value for the Q component of the YIQ color space. Q is in the range [-.52,.52].
- R
- Real, Input for RGBYIQ; Output for YIQRGB ---A value for red in the RGB color space. R is in the range [0.,1.].
- G
- Real, Input for RGBYIQ; Output for YIQRGB ---A value for green in the RGB color space. G is in the range [0.,1.].
- B
- Real, Input for RGBYIQ; Output for YIQRGB ---A value for blue in the RGB color space. B is in the range [0.,1.].
Lines 1 through 3 of code segment 3 from fcce02.f assign values to the Y, I, and Q components of the YIQ color space that correspond to red=0., green=1., and blue=0. in the RGB space. The result of the call in line 4 is that R will be returned as 0., G as 1., and B as 0.
Lines 5 through 7 of code segment 3 from fcce02.f assign values to red, green, and blue in the RGB space that correspond to values of Y=1., I=0., and Q=0. in the YIQ space. The result of the call in line 8 is that Y will be returned as 1., RI will be returned as 0., and Q will be returned as 0. Y is the grayscale component of a color TV signal that is shown on black and white TVs.
Previous Chapter LLUs Home Next Chapter