]>
git.saurik.com Git - wxWidgets.git/blob - src/xpm/rgb.c
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
48 #ifndef FOR_MSW /* normal part first, MSW part at
49 * the end, (huge ifdef!) */
51 * Read a rgb text file. It stores the rgb values (0->65535)
52 * and the rgb mnemonics (malloc'ed) into the "rgbn" array. Returns the
53 * number of entries stored.
56 xpmReadRgbNames(rgb_fname
, rgbn
)
62 int n
, items
, red
, green
, blue
;
63 char line
[512], name
[512], *rgbname
, *s1
, *s2
;
66 /* Open the rgb text file. Abort if error. */
67 if ((rgbf
= fopen(rgb_fname
, "r")) == NULL
)
70 /* Loop reading each line in the file. */
73 /* Quit if rgb text file has too many entries. */
74 while (fgets(line
, sizeof(line
), rgbf
) && n
< MAX_RGBNAMES
) {
76 /* Skip silently if line is bad. */
77 items
= sscanf(line
, "%d %d %d %[^\n]\n", &red
, &green
, &blue
, name
);
82 * Make sure rgb values are within 0->255 range. Skip silently if
85 if (red
< 0 || red
> 0xFF ||
86 green
< 0 || green
> 0xFF ||
87 blue
< 0 || blue
> 0xFF)
90 /* Allocate memory for ascii name. If error give up here. */
91 if (!(rgbname
= (char *) XpmMalloc(strlen(name
) + 1)))
94 /* Copy string to ascii name and lowercase it. */
95 for (s1
= name
, s2
= rgbname
; *s1
; s1
++)
99 /* Save the rgb values and ascii name in the array. */
100 rgb
->r
= red
* 257; /* 65535/255 = 257 */
101 rgb
->g
= green
* 257;
110 /* Return the number of read rgb names. */
111 return n
< 0 ? 0 : n
;
115 * Return the color name corresponding to the given rgb values
118 xpmGetRgbName(rgbn
, rgbn_max
, red
, green
, blue
)
119 xpmRgbName rgbn
[]; /* rgb mnemonics from rgb text file */
120 int rgbn_max
; /* number of rgb mnemonics in table */
121 int red
, green
, blue
; /* rgb values */
128 * Just perform a dumb linear search over the rgb values of the color
129 * mnemonics. One could speed things up by sorting the rgb values and
130 * using a binary search, or building a hash table, etc...
132 for (i
= 0, rgb
= rgbn
; i
< rgbn_max
; i
++, rgb
++)
133 if (red
== rgb
->r
&& green
== rgb
->g
&& blue
== rgb
->b
)
136 /* if not found return NULL */
141 * Free the strings which have been malloc'ed in xpmReadRgbNames
144 xpmFreeRgbNames(rgbn
, rgbn_max
)
152 for (i
= 0, rgb
= rgbn
; i
< rgbn_max
; i
++, rgb
++)
156 #else /* here comes the MSW part, the
157 * second part of the huge ifdef */
159 #include "rgbtab.h" /* hard coded rgb.txt table */
162 xpmReadRgbNames(rgb_fname
, rgbn
)
167 * check for consistency???
168 * table has to be sorted for calls on strcasecmp
170 return (numTheRGBRecords
);
174 * MSW rgb values are made from 3 BYTEs, this is different from X XColor.red,
175 * which has something like #0303 for one color
178 xpmGetRgbName(rgbn
, rgbn_max
, red
, green
, blue
)
179 xpmRgbName rgbn
[]; /* rgb mnemonics from rgb text file
181 int rgbn_max
; /* not used */
182 int red
, green
, blue
; /* rgb values */
186 unsigned long rgbVal
;
189 while (i
< numTheRGBRecords
) {
190 rgbVal
= theRGBRecords
[i
].rgb
;
191 if (GetRValue(rgbVal
) == red
&&
192 GetGValue(rgbVal
) == green
&&
193 GetBValue(rgbVal
) == blue
)
194 return (theRGBRecords
[i
].name
);
200 /* used in XParseColor in simx.c */
202 xpmGetRGBfromName(inname
, r
, g
, b
)
206 int left
, right
, middle
;
208 unsigned long rgbVal
;
212 name
= xpmstrdup(inname
);
215 * the table in rgbtab.c has no names with spaces, and no grey, but a
218 /* so first extract ' ' */
219 while (p
= strchr(name
, ' ')) {
220 while (*(p
)) { /* till eof of string */
221 *p
= *(p
+ 1); /* copy to the left */
225 /* fold to lower case */
233 * substitute Grey with Gray, else rgbtab.h would have more than 100
234 * 'duplicate' entries
236 if (grey
= strstr(name
, "grey"))
241 right
= numTheRGBRecords
- 1;
243 middle
= (left
+ right
) / 2;
244 cmp
= xpmstrcasecmp(name
, theRGBRecords
[middle
].name
);
246 rgbVal
= theRGBRecords
[middle
].rgb
;
247 *r
= GetRValue(rgbVal
);
248 *g
= GetGValue(rgbVal
);
249 *b
= GetBValue(rgbVal
);
252 } else if (cmp
< 0) {
257 } while (left
<= right
);
260 * I don't like to run in a ColorInvalid error and to see no pixmap at
261 * all, so simply return a red pixel. Should be wrapped in an #ifdef
267 *b
= 0; /* red error pixel */
274 xpmFreeRgbNames(rgbn
, rgbn_max
)
282 #endif /* MSW part */