1 | /* |
---|
2 | * $Id: StrToXColor.c,v 1.2 1999-01-22 23:17:01 ghudson Exp $ |
---|
3 | * |
---|
4 | * Copyright 1990, 1991 by the Massachusetts Institute of Technology. |
---|
5 | * |
---|
6 | * For copying and distribution information, please see the file |
---|
7 | * <mit-copyright.h>. |
---|
8 | * |
---|
9 | */ |
---|
10 | |
---|
11 | #if (!defined(lint)) && (!defined(SABER)) |
---|
12 | static char *rcsid = |
---|
13 | "$Id: StrToXColor.c,v 1.2 1999-01-22 23:17:01 ghudson Exp $"; |
---|
14 | #endif |
---|
15 | |
---|
16 | #include "mit-copyright.h" |
---|
17 | #include "Jets.h" |
---|
18 | #include <ctype.h> |
---|
19 | |
---|
20 | #define MONO 0 |
---|
21 | #define GRAY 1 |
---|
22 | #define COLOR 2 |
---|
23 | |
---|
24 | #define MAXCOLORS 50 |
---|
25 | Colormap xjcolormap; |
---|
26 | unsigned long writecolors[MAXCOLORS]; |
---|
27 | |
---|
28 | /* |
---|
29 | * string to Pixel conversion |
---|
30 | */ |
---|
31 | int StrToXPixel(display, string, pixel) |
---|
32 | Display *display; |
---|
33 | char *string; |
---|
34 | unsigned long *pixel; |
---|
35 | { |
---|
36 | char errtext[100]; |
---|
37 | char *error = "couldn't %s color: %s"; |
---|
38 | XColor x_color; |
---|
39 | char *copy, *address2, *address3, *color; |
---|
40 | char sep = ','; |
---|
41 | char *ptr; |
---|
42 | static int init=0; |
---|
43 | static int dpy_type; |
---|
44 | int cnum, i; |
---|
45 | static int wcavail; |
---|
46 | |
---|
47 | copy = XjNewString(string); |
---|
48 | |
---|
49 | /* |
---|
50 | * figure out the display type -- MONO, GRAY or COLOR |
---|
51 | */ |
---|
52 | if (!init) |
---|
53 | { |
---|
54 | init = 1; |
---|
55 | if (DisplayPlanes(display, DefaultScreen(display)) > 1) |
---|
56 | { |
---|
57 | Visual *visual; |
---|
58 | visual = |
---|
59 | DefaultVisualOfScreen(DefaultScreenOfDisplay(display)); |
---|
60 | wcavail = 0; |
---|
61 | switch(visual->class) |
---|
62 | { |
---|
63 | case StaticGray: |
---|
64 | case GrayScale: |
---|
65 | dpy_type = GRAY; |
---|
66 | break; |
---|
67 | case PseudoColor: |
---|
68 | wcavail = 1; |
---|
69 | case StaticColor: |
---|
70 | case TrueColor: |
---|
71 | case DirectColor: |
---|
72 | dpy_type = COLOR; |
---|
73 | break; |
---|
74 | default: |
---|
75 | dpy_type = MONO; |
---|
76 | break; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | for (i = 0; i < MAXCOLORS; i++) |
---|
81 | writecolors[i] = XjNoColor; |
---|
82 | xjcolormap = DefaultColormap(display, |
---|
83 | DefaultScreen(display)); |
---|
84 | } |
---|
85 | |
---|
86 | /* |
---|
87 | * parse the resource -- if there are 2 or 3 "words", then |
---|
88 | * assign them to the other addresses. |
---|
89 | */ |
---|
90 | if ((ptr = address2 = strchr(copy, sep)) != NULL) |
---|
91 | { |
---|
92 | ptr--; /* strip trailing spaces off word */ |
---|
93 | while (isspace(*ptr)) /* before comma */ |
---|
94 | ptr--; |
---|
95 | *++ptr = '\0'; /* terminate string */ |
---|
96 | |
---|
97 | address2++; /* strip leading spaces off word */ |
---|
98 | while (isspace(*address2)) /* after comma */ |
---|
99 | address2++; |
---|
100 | |
---|
101 | if ((ptr = address3 = strchr(address2, sep)) != NULL) |
---|
102 | { |
---|
103 | ptr--; /* strip trailing spaces off word */ |
---|
104 | while (isspace(*ptr)) /* before comma */ |
---|
105 | ptr--; |
---|
106 | *++ptr = '\0'; /* terminate string */ |
---|
107 | |
---|
108 | address3++; /* strip leading spaces off word */ |
---|
109 | while (isspace(*address3)) /* after comma */ |
---|
110 | address3++; |
---|
111 | |
---|
112 | ptr = address3 + strlen(address3) - 1; /* strip trailing */ |
---|
113 | while (isspace(*ptr)) /* spaces off last word... */ |
---|
114 | ptr--; |
---|
115 | *++ptr = '\0'; /* terminate string */ |
---|
116 | } |
---|
117 | else |
---|
118 | { /* if there are only 2 words, then */ |
---|
119 | address3 = address2; /* set the "color address" to the */ |
---|
120 | address2 = copy; /* 2nd one, and the "grayscale */ |
---|
121 | } /* address" to the 1st one... */ |
---|
122 | } |
---|
123 | else /* if there is only 1 word, then */ |
---|
124 | address3 = address2 = copy; /* set all addresses to it... */ |
---|
125 | |
---|
126 | /* |
---|
127 | * figure out which address to use based on the display type. |
---|
128 | */ |
---|
129 | switch(dpy_type) |
---|
130 | { |
---|
131 | case COLOR: |
---|
132 | color = address3; |
---|
133 | break; |
---|
134 | case GRAY: |
---|
135 | color = address2; |
---|
136 | break; |
---|
137 | default: |
---|
138 | color = copy; |
---|
139 | break; |
---|
140 | } |
---|
141 | |
---|
142 | /* |
---|
143 | * now figure out what color it is and fill it in. |
---|
144 | */ |
---|
145 | if (strcasecmp(XjNone, color) == 0) |
---|
146 | { |
---|
147 | *pixel = -1; |
---|
148 | XjFree(copy); |
---|
149 | return 0; |
---|
150 | } |
---|
151 | |
---|
152 | if (strcmp(XjDefaultForeground, color) == 0) |
---|
153 | { |
---|
154 | *pixel = BlackPixel(display, DefaultScreen(display)); |
---|
155 | XjFree(copy); |
---|
156 | return 0; |
---|
157 | } |
---|
158 | |
---|
159 | if (strcmp(XjDefaultBackground, color) == 0) |
---|
160 | { |
---|
161 | *pixel = WhitePixel(display, DefaultScreen(display)); |
---|
162 | XjFree(copy); |
---|
163 | return 0; |
---|
164 | } |
---|
165 | |
---|
166 | /* |
---|
167 | * Support for writable color cells. |
---|
168 | */ |
---|
169 | if (strncmp(XjColor, color, strlen(XjColor)) == 0) |
---|
170 | { |
---|
171 | cnum = atoi(&color[strlen(XjColor)]); |
---|
172 | if (cnum < 0 || cnum > MAXCOLORS || !wcavail) |
---|
173 | { |
---|
174 | if (!wcavail) |
---|
175 | XjWarning("display does not support dynamic colors"); |
---|
176 | else |
---|
177 | { |
---|
178 | sprintf(errtext, |
---|
179 | "invalid color number: %d is not between 0 and %d", |
---|
180 | cnum, MAXCOLORS); |
---|
181 | XjWarning(errtext); |
---|
182 | } |
---|
183 | XjFree(copy); |
---|
184 | *pixel = -1; |
---|
185 | return -1; |
---|
186 | } |
---|
187 | |
---|
188 | if (writecolors[cnum] == XjNoColor) |
---|
189 | { |
---|
190 | if (!XAllocColorCells(display, xjcolormap, |
---|
191 | False, NULL, 0, |
---|
192 | &writecolors[cnum], 1)) |
---|
193 | { |
---|
194 | sprintf(errtext, "could not allocate %s", color); |
---|
195 | XjWarning(errtext); |
---|
196 | XjFree(copy); |
---|
197 | *pixel = -1; |
---|
198 | return -1; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | *pixel = writecolors[cnum]; |
---|
203 | XjFree(copy); |
---|
204 | return 0; |
---|
205 | } |
---|
206 | |
---|
207 | if (!XParseColor(display, |
---|
208 | DefaultColormap(display, DefaultScreen(display)), |
---|
209 | color, |
---|
210 | &x_color)) |
---|
211 | { |
---|
212 | sprintf(errtext, error, "parse", color); |
---|
213 | XjWarning(errtext); |
---|
214 | *pixel = -1; |
---|
215 | XjFree(copy); |
---|
216 | return -1; |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | if (!XAllocColor(display, |
---|
221 | DefaultColormap(display, DefaultScreen(display)), |
---|
222 | &x_color)) |
---|
223 | { |
---|
224 | sprintf(errtext, error, "allocate", color); |
---|
225 | XjWarning(errtext); |
---|
226 | *pixel = -1; |
---|
227 | XjFree(copy); |
---|
228 | return -1; |
---|
229 | } |
---|
230 | |
---|
231 | *pixel = x_color.pixel; |
---|
232 | XjFree(copy); |
---|
233 | return 0; |
---|
234 | } |
---|
235 | |
---|
236 | /* |
---|
237 | * string to Color conversion |
---|
238 | */ |
---|
239 | int StrToXColor(display, window, where, resource, type, address) |
---|
240 | Display *display; |
---|
241 | Window window; |
---|
242 | caddr_t where; |
---|
243 | XjResource *resource; |
---|
244 | char *type; |
---|
245 | caddr_t address; |
---|
246 | { |
---|
247 | unsigned long pixel; |
---|
248 | |
---|
249 | if (StrToXPixel(display, address, &pixel)) |
---|
250 | { |
---|
251 | /* we give black for foreground or white for background, in */ |
---|
252 | /* case we can't figure something better out */ |
---|
253 | |
---|
254 | if (resource->resource_class == XjCForeground) |
---|
255 | *((int *)((char *)where + resource->resource_offset)) = |
---|
256 | BlackPixel(display, DefaultScreen(display)); |
---|
257 | else |
---|
258 | *((int *)((char *)where + resource->resource_offset)) = |
---|
259 | WhitePixel(display, DefaultScreen(display)); |
---|
260 | } |
---|
261 | else |
---|
262 | *((int *)((char *)where + resource->resource_offset)) = pixel; |
---|
263 | |
---|
264 | return 0; |
---|
265 | } |
---|