2 * Copyright (C) 1989-95 GROUPE BULL
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * Except as contained in this notice, the name of GROUPE BULL shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from GROUPE BULL.
26 /*****************************************************************************\
30 * Rgb file utilities *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
36 * The code related to FOR_MSW has been added by
37 * HeDu (hedu@cul-ipn.uni-kiel.de) 4/94
41 * Part of this code has been taken from the ppmtoxpm.c file written by Mark
42 * W. Snitily but has been modified for my special need
51 #ifndef FOR_MSW /* normal part first, MSW part at
52 * the end, (huge ifdef!) */
54 * Read a rgb text file. It stores the rgb values (0->65535)
55 * and the rgb mnemonics (malloc'ed) into the "rgbn" array. Returns the
56 * number of entries stored.
64 int n, items, red, green, blue;
65 char line[512], name[512], *rgbname, *s1, *s2;
68 /* Open the rgb text file. Abort if error. */
69 if ((rgbf = fopen(rgb_fname, "r")) == NULL)
72 /* Loop reading each line in the file. */
75 /* Quit if rgb text file has too many entries. */
76 while (fgets(line, sizeof(line), rgbf) && n < MAX_RGBNAMES) {
78 /* Skip silently if line is bad. */
79 items = sscanf(line, "%d %d %d %[^\n]\n", &red, &green, &blue, name);
84 * Make sure rgb values are within 0->255 range. Skip silently if
87 if (red < 0 || red > 0xFF ||
88 green < 0 || green > 0xFF ||
89 blue < 0 || blue > 0xFF)
92 /* Allocate memory for ascii name. If error give up here. */
93 if (!(rgbname = (char *) XpmMalloc(strlen(name) + 1)))
96 /* Copy string to ascii name and lowercase it. */
97 for (s1 = name, s2 = rgbname; *s1; s1++)
101 /* Save the rgb values and ascii name in the array. */
102 rgb->r = red * 257; /* 65535/255 = 257 */
103 rgb->g = green * 257;
112 /* Return the number of read rgb names. */
113 return n < 0 ? 0 : n;
117 * Return the color name corresponding to the given rgb values
121 xpmRgbName rgbn[], /* rgb mnemonics from rgb text file */
122 int rgbn_max, /* number of rgb mnemonics in table */
123 int red, /* rgb values */
131 * Just perform a dumb linear search over the rgb values of the color
132 * mnemonics. One could speed things up by sorting the rgb values and
133 * using a binary search, or building a hash table, etc...
135 for (i = 0, rgb = rgbn; i < rgbn_max; i++, rgb++)
136 if (red == rgb->r && green == rgb->g && blue == rgb->b)
139 /* if not found return NULL */
144 * Free the strings which have been malloc'ed in xpmReadRgbNames
154 for (i = 0, rgb = rgbn; i < rgbn_max; i++, rgb++)
158 #else /* here comes the MSW part, the
159 * second part of the huge ifdef */
161 #include "rgbtab.h" /* hard coded rgb.txt table */
169 * check for consistency???
170 * table has to be sorted for calls on strcasecmp
172 return (numTheRGBRecords);
176 * MSW rgb values are made from 3 BYTEs, this is different from X XColor.red,
177 * which has something like #0303 for one color
181 xpmRgbName rgbn[], /* rgb mnemonics from rgb text file
183 int rgbn_max, /* not used */
184 int red, /* rgb values */
190 unsigned long rgbVal;
193 while (i < numTheRGBRecords) {
194 rgbVal = theRGBRecords[i].rgb;
195 if (GetRValue(rgbVal) == red &&
196 GetGValue(rgbVal) == green &&
197 GetBValue(rgbVal) == blue)
198 return (theRGBRecords[i].name);
204 /* used in XParseColor in simx.c */
212 int left, right, middle;
214 unsigned long rgbVal;
218 name = xpmstrdup(inname);
221 * the table in rgbtab.c has no names with spaces, and no grey, but a
224 /* so first extract ' ' */
225 while (p = strchr(name, ' ')) {
226 while (*(p)) { /* till eof of string */
227 *p = *(p + 1); /* copy to the left */
231 /* fold to lower case */
239 * substitute Grey with Gray, else rgbtab.h would have more than 100
240 * 'duplicate' entries
242 if (grey = strstr(name, "grey"))
247 right = numTheRGBRecords - 1;
249 middle = (left + right) / 2;
250 cmp = xpmstrcasecmp(name, theRGBRecords[middle].name);
252 rgbVal = theRGBRecords[middle].rgb;
253 *r = GetRValue(rgbVal);
254 *g = GetGValue(rgbVal);
255 *b = GetBValue(rgbVal);
258 } else if (cmp < 0) {
263 } while (left <= right);
266 * I don't like to run in a ColorInvalid error and to see no pixmap at
267 * all, so simply return a red pixel. Should be wrapped in an #ifdef
273 *b = 0; /* red error pixel */
287 #endif /* MSW part */