]> git.saurik.com Git - wxWidgets.git/blob - src/mac/xpm/rgb.c
Fix for the wrong painting rectangle being set.
[wxWidgets.git] / src / mac / xpm / rgb.c
1 /*
2 * Copyright (C) 1989-95 GROUPE BULL
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
20 *
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.
24 */
25
26 /*****************************************************************************\
27 * rgb.c: *
28 * *
29 * XPM library *
30 * Rgb file utilities *
31 * *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
34
35 /*
36 * The code related to FOR_MSW has been added by
37 * HeDu (hedu@cul-ipn.uni-kiel.de) 4/94
38 */
39
40 /*
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
43 */
44
45 #include "XpmI.h"
46 #include <ctype.h>
47
48 #include "rgbtab.h" /* hard coded rgb.txt table */
49
50 int
51 xpmReadRgbNames(rgb_fname, rgbn)
52 char *rgb_fname;
53 xpmRgbName rgbn[];
54 {
55 /*
56 * check for consistency???
57 * table has to be sorted for calls on strcasecmp
58 */
59 return (numTheRGBRecords);
60 }
61
62 /*
63 * MSW rgb values are made from 3 BYTEs, this is different from X XColor.red,
64 * which has something like #0303 for one color
65 */
66
67 /* portable rgb values have 16 bit unsigned value range for each color these are promoted from the table */
68
69 char *
70 xpmGetRgbName(rgbn, rgbn_max, red, green, blue)
71 xpmRgbName rgbn[]; /* rgb mnemonics from rgb text file
72 * not used */
73 int rgbn_max; /* not used */
74 int red, green, blue; /* rgb values */
75
76 {
77 int i;
78 RGBColor rgbVal;
79
80 i = 0;
81 while (i < numTheRGBRecords) {
82 rgbVal = theRGBRecords[i].rgb;
83 if (GetRValue(rgbVal) == red &&
84 GetGValue(rgbVal) == green &&
85 GetBValue(rgbVal) == blue)
86 return (theRGBRecords[i].name);
87 i++;
88 }
89 return (NULL);
90 }
91
92 /* used in XParseColor in simx.c */
93 int
94 xpmGetRGBfromName(inname, r, g, b)
95 char *inname;
96 int *r, *g, *b;
97 {
98 int left, right, middle;
99 int cmp;
100 RGBColor rgbVal;
101 char *name;
102 char *grey, *p;
103
104 name = xpmstrdup(inname);
105
106 /*
107 * the table in rgbtab.c has no names with spaces, and no grey, but a
108 * lot of gray
109 */
110 /* so first extract ' ' */
111 while ((p = strchr(name, ' '))!=NULL) {
112 while (*(p)) { /* till eof of string */
113 *p = *(p + 1); /* copy to the left */
114 p++;
115 }
116 }
117 /* fold to lower case */
118 p = name;
119 while (*p) {
120 *p = tolower(*p);
121 p++;
122 }
123
124 /*
125 * substitute Grey with Gray, else rgbtab.h would have more than 100
126 * 'duplicate' entries
127 */
128 if ((grey = strstr(name, "grey"))!=NULL)
129 grey[2] = 'a';
130
131 /* binary search */
132 left = 0;
133 right = numTheRGBRecords - 1;
134 do {
135 middle = (left + right) / 2;
136 cmp = xpmstrcasecmp(name, theRGBRecords[middle].name);
137 if (cmp == 0) {
138 rgbVal = theRGBRecords[middle].rgb;
139 *r = GetRValue(rgbVal);
140 *g = GetGValue(rgbVal);
141 *b = GetBValue(rgbVal);
142 free(name);
143 return (1);
144 } else if (cmp < 0) {
145 right = middle - 1;
146 } else { /* > 0 */
147 left = middle + 1;
148 }
149 } while (left <= right);
150
151 /*
152 * I don't like to run in a ColorInvalid error and to see no pixmap at
153 * all, so simply return a red pixel. Should be wrapped in an #ifdef
154 * HeDu
155 */
156
157 #ifdef FOR_MSW
158 *r = 255;
159 *g = 0;
160 *b = 0; /* red error pixel */
161 #else
162 *r = 0xFFFF;
163 *g = 0;
164 *b = 0; /* red error pixel */
165 #endif
166 free(name);
167 return (1);
168 }
169
170 void
171 xpmFreeRgbNames(rgbn, rgbn_max)
172 xpmRgbName rgbn[];
173 int rgbn_max;
174
175 {
176 /* nothing to do */
177 }