]>
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 /* Visual Age cannot deal with old, non-ansi, code */
163 int xpmReadRgbNames(char* rgb_fname
, xpmRgbName rgbn
[])
166 xpmReadRgbNames(rgb_fname
, rgbn
)
172 * check for consistency???
173 * table has to be sorted for calls on strcasecmp
175 return (numTheRGBRecords
);
179 * MSW rgb values are made from 3 BYTEs, this is different from X XColor.red,
180 * which has something like #0303 for one color
183 /* Visual Age cannot deal with old, non-ansi, code */
184 char* xpmGetRgbName(xpmRgbName rgbn
[], int rgbn_max
, int red
, int green
, int blue
)
187 xpmGetRgbName(rgbn
, rgbn_max
, red
, green
, blue
)
188 xpmRgbName rgbn
[]; /* rgb mnemonics from rgb text file
190 int rgbn_max
; /* not used */
191 int red
, green
, blue
; /* rgb values */
195 unsigned long rgbVal
;
198 while (i
< numTheRGBRecords
) {
199 rgbVal
= theRGBRecords
[i
].rgb
;
200 if (GetRValue(rgbVal
) == red
&&
201 GetGValue(rgbVal
) == green
&&
202 GetBValue(rgbVal
) == blue
)
203 return (theRGBRecords
[i
].name
);
209 /* used in XParseColor in simx.c */
211 /* Visual Age cannot deal with old, non-ansi, code */
212 int xpmGetRGBfromName(char* inname
, int* r
, int* g
, int* b
)
215 xpmGetRGBfromName(inname
, r
, g
, b
)
220 int left
, right
, middle
;
222 unsigned long rgbVal
;
226 name
= xpmstrdup(inname
);
229 * the table in rgbtab.c has no names with spaces, and no grey, but a
232 /* so first extract ' ' */
233 while (p
= strchr(name
, ' ')) {
234 while (*(p
)) { /* till eof of string */
235 *p
= *(p
+ 1); /* copy to the left */
239 /* fold to lower case */
247 * substitute Grey with Gray, else rgbtab.h would have more than 100
248 * 'duplicate' entries
250 if (grey
= strstr(name
, "grey"))
255 right
= numTheRGBRecords
- 1;
257 middle
= (left
+ right
) / 2;
258 cmp
= xpmstrcasecmp(name
, theRGBRecords
[middle
].name
);
260 rgbVal
= theRGBRecords
[middle
].rgb
;
261 *r
= GetRValue(rgbVal
);
262 *g
= GetGValue(rgbVal
);
263 *b
= GetBValue(rgbVal
);
266 } else if (cmp
< 0) {
271 } while (left
<= right
);
274 * I don't like to run in a ColorInvalid error and to see no pixmap at
275 * all, so simply return a red pixel. Should be wrapped in an #ifdef
281 *b
= 0; /* red error pixel */
288 /* Visual Age cannot deal with old, non-ansi, code */
289 void xpmFreeRgbNames(xpmRgbName rgbn
[], int rgbn_max
)
292 xpmFreeRgbNames(rgbn
, rgbn_max
)
300 #endif /* MSW part */