Debian Codegraph @

terps/tads/tads2/msdos/osscolor.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2002 by Michael J. Roberts. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 osscolor.c - ossxxx color routines for DOS
10 Function
11
12 Notes
13
14 Modified
15 05/19/02 MJRoberts - Creation
16 */
17
18 #include "os.h"
19 #include "osgen.h"
20
21 extern int os_f_plain;
22
23 /*
24 * Given an OSGEN_COLOR_xxx value, translate to the 4-bit system color:
25 *
26 *. 0 = black
27 *. 1 = navy
28 *. 2 = green
29 *. 3 = teal
30 *. 4 = maroon
31 *. 5 = purple
32 *. 6 = olive
33 *. 7 = silver
34 *
35 *. 8 = gray ("bright black")
36 *. 9 = blue
37 *. 10 = lime
38 *. 11 = cyan
39 *. 12 = red
40 *. 13 = magenta
41 *. 14 = yellow
42 *. 15 = white
43 *
44 * If the color value is one of the "parameterized" colors, we'll use the
45 * current value for the color as set in the global variables
46 * (text_normal_color, etc).
47 */
48 static int xlat_color(int color)
49 {
50 switch(color)
51 {
52 case OSGEN_COLOR_TEXT:
53 /* return the foreground portion of the normal text color */
54 return (text_normal_color & 0x07);
55
56 case OSGEN_COLOR_TEXTBG:
57 /* return the background portion of the normal text color */
58 return ((text_normal_color >> 4) & 0x07);
59
60 case OSGEN_COLOR_STATUSLINE:
61 /* return the foreground portion of the statusline color */
62 return (sdesc_color & 0x07);
63
64 case OSGEN_COLOR_STATUSBG:
65 /* return the background portion of the statusline color */
66 return ((sdesc_color >> 4) & 0x07);
67
68 case OSGEN_COLOR_INPUT:
69 /* return the foreground portion of the normal text color */
70 return (text_normal_color & 0x07);
71
72 case OSGEN_COLOR_BLACK:
73 return 0;
74
75 case OSGEN_COLOR_NAVY:
76 return 1;
77
78 case OSGEN_COLOR_GREEN:
79 return 2;
80
81 case OSGEN_COLOR_TEAL:
82 return 3;
83
84 case OSGEN_COLOR_MAROON:
85 return 4;
86
87 case OSGEN_COLOR_PURPLE:
88 return 5;
89
90 case OSGEN_COLOR_OLIVE:
91 return 6;
92
93 case OSGEN_COLOR_SILVER:
94 return 7;
95
96 case OSGEN_COLOR_GRAY:
97 return 8;
98
99 case OSGEN_COLOR_BLUE:
100 return 9;
101
102 case OSGEN_COLOR_LIME:
103 return 10;
104
105 case OSGEN_COLOR_CYAN:
106 return 11;
107
108 case OSGEN_COLOR_RED:
109 return 12;
110
111 case OSGEN_COLOR_MAGENTA:
112 return 13;
113
114 case OSGEN_COLOR_YELLOW:
115 return 14;
116
117 case OSGEN_COLOR_WHITE:
118 return 15;
119
120 default:
121 return 0;
122 }
123 }
124
125 /*
126 * Translate a portable color specifiation to an ossxxx value.
127 *
128 * For DOS character mode, the OS-level color consists of a combination of
129 * bit fields:
130 *
131 *. 7 6 5 4 3 2 1 0
132 * b b b b h f f f
133 *
134 *. 'b' = background color - a 4-bit value in bits 4-7
135 *. 'h' = highlight - a 1-bit value at bit 3
136 *. 'f' = foreground color - a 3-bit value in bits 0-2
137 *
138 * The foreground and background values are taken from the set listed in
139 * the comments on xlat_color() above.
140 *
141 * We render the "hilite" attribute using foreground color intensity: for
142 * normal text, we only use the dim colors, values 0-7; for highlighted
143 * text, we only use the bright colors, values 8-15. This means that we
144 * can map the full complement of 16 OSGEN_COLOR_xxx values to system
145 * colors for the background, but we must collapse the low- and
146 * high-intensity version of the OSGEN_COLOR_xxx values, so that we can
147 * independently set the dim/bright color according to the highlighting
148 * attribute.
149 */
150 int ossgetcolor(int fg, int bg, int attrs, int screen_color)
151 {
152 int color;
153
154 /*
155 * Check for a special case: if they're just setting the BOLD attribute
156 * for text in the normal text color (the PARAMETERIZED normal text
157 * color, not the color that happens to match the current parameter
158 * value), then use the text_bold_color global variable, which comes
159 * from the user preferences.
160 */
161 if (fg == OSGEN_COLOR_TEXT && bg == OSGEN_COLOR_TRANSPARENT
162 && (attrs & OS_ATTR_BOLD) != 0)
163 {
164 /*
165 * they specifically want bold normal text, so use the
166 * parameterized setting for this style
167 */
168 return text_bold_color;
169 }
170
171 /*
172 * First, figure the background color. If 'bg' is transparent, then
173 * use the current screen color. Otherwise, use the given background
174 * color.
175 */
176 if (bg == OSGEN_COLOR_TRANSPARENT)
177 {
178 /* use the screen color */
179 color = xlat_color(screen_color);
180 }
181 else
182 {
183 /* use the explicit color */
184 color = xlat_color(bg);
185 }
186
187 /* shift the background color into the appropriate bit field */
188 color <<= 4;
189
190 /*
191 * Map the foreground color and combine it with the background color.
192 * Note that we must collapse the bright and dim foreground colors
193 * into a single set, because we need to control brightness
194 * independently with the "highlight" attribute; to do this, simply
195 * mask out the brightness bit from the translated foreground color.
196 */
197 if (fg == OSGEN_COLOR_TRANSPARENT)
198 {
199 /* it's transparent, so use the screen color */
200 color |= (xlat_color(screen_color) & 0x07);
201 }
202 else
203 {
204 /* use the explicit color */
205 color |= (xlat_color(fg) & 0x07);
206 }
207
208 /* if the BOLD attribute is set, set the highlight bit in the color */
209 if ((attrs & OS_ATTR_BOLD) != 0)
210 color |= 0x08;
211
212 /* return the color */
213 return color;
214 }
215
216 /*
217 * Get extended system information
218 */
219 int oss_get_sysinfo(int code, void *param, long *result)
220 {
221 switch(code)
222 {
223 case SYSINFO_TEXT_COLORS:
224 /* we support the ANSI colors, foreground and background */
225 *result = SYSINFO_TXC_ANSI_FGBG;
226 return TRUE;
227
228 case SYSINFO_TEXT_HILITE:
229 /*
230 * we can render highlighted text with a unique appearance, as long
231 * as we're not in 'plain' mode
232 */
233 *result = (os_f_plain ? 0 : 1);
234 return TRUE;
235
236 default:
237 /* not recognized */
238 return FALSE;
239 }
240 }
241