2 /* Copyright (c) Mark J. Kilgard, 1994, 1996, 1997. */
4 /* This program is freely distributable without licensing fees
5 and is provided without guarantee or warrantee expressed or
6 implied. This program is -not- in the public domain. */
11 #if defined(__OS2PM__)
12 #define IsWindowVisible WinIsWindowVisible
15 #define CLAMP(i) ((i) > 1.0 ? 1.0 : ((i) < 0.0 ? 0.0 : (i)))
19 glutSetColor(int ndx
, GLfloat red
, GLfloat green
, GLfloat blue
)
21 GLUTcolormap
*cmap
, *newcmap
;
26 if (__glutCurrentWindow
->renderWin
== __glutCurrentWindow
->win
) {
27 cmap
= __glutCurrentWindow
->colormap
;
28 vis
= __glutCurrentWindow
->vis
;
30 cmap
= __glutCurrentWindow
->overlay
->colormap
;
31 vis
= __glutCurrentWindow
->overlay
->vis
;
32 if (ndx
== __glutCurrentWindow
->overlay
->transparentPixel
) {
34 "glutSetColor: cannot set color of overlay transparent index %d\n",
41 __glutWarning("glutSetColor: current window is RGBA");
44 #if defined(_WIN32) || defined(__OS2PM__)
45 if (ndx
>= 256 || /* always assume 256 colors on Win32 */
47 if (ndx
>= vis
->visual
->map_entries
||
50 __glutWarning("glutSetColor: index %d out of range", ndx
);
53 if (cmap
->refcnt
> 1) {
54 newcmap
= __glutAssociateNewColormap(vis
);
56 /* Wouldn't it be nice if XCopyColormapAndFree could be
57 told not to free the old colormap's entries! */
58 for (i
= cmap
->size
- 1; i
>= 0; i
--) {
60 /* We are going to set this cell shortly! */
63 if (cmap
->cells
[i
].component
[GLUT_RED
] >= 0.0) {
65 newcmap
->cells
[i
].component
[GLUT_RED
] =
66 cmap
->cells
[i
].component
[GLUT_RED
];
67 color
.red
= (GLfloat
) 0xffff *
68 cmap
->cells
[i
].component
[GLUT_RED
];
69 newcmap
->cells
[i
].component
[GLUT_GREEN
] =
70 cmap
->cells
[i
].component
[GLUT_GREEN
];
71 color
.green
= (GLfloat
) 0xffff *
72 cmap
->cells
[i
].component
[GLUT_GREEN
];
73 newcmap
->cells
[i
].component
[GLUT_BLUE
] =
74 cmap
->cells
[i
].component
[GLUT_BLUE
];
75 color
.blue
= (GLfloat
) 0xffff *
76 cmap
->cells
[i
].component
[GLUT_BLUE
];
77 color
.flags
= DoRed
| DoGreen
| DoBlue
;
78 #if defined(_WIN32) || defined(__OS2PM__)
79 if (IsWindowVisible(__glutCurrentWindow
->win
)) {
80 XHDC
= __glutCurrentWindow
->hdc
;
85 XStoreColor(__glutDisplay
, newcmap
->cmap
, &color
);
87 /* Leave unallocated entries unallocated. */
91 if (__glutCurrentWindow
->renderWin
== __glutCurrentWindow
->win
) {
92 __glutCurrentWindow
->colormap
= cmap
;
93 __glutCurrentWindow
->cmap
= cmap
->cmap
;
95 __glutCurrentWindow
->overlay
->colormap
= cmap
;
96 __glutCurrentWindow
->overlay
->cmap
= cmap
->cmap
;
98 XSetWindowColormap(__glutDisplay
,
99 __glutCurrentWindow
->renderWin
, cmap
->cmap
);
101 #if !defined(_WIN32) && !defined(__OS2PM__)
103 GLUTwindow
*toplevel
;
105 toplevel
= __glutToplevelOf(__glutCurrentWindow
);
106 if (toplevel
->cmap
!= cmap
->cmap
) {
107 __glutPutOnWorkList(toplevel
, GLUT_COLORMAP_WORK
);
114 cmap
->cells
[ndx
].component
[GLUT_RED
] = red
;
115 color
.red
= (GLfloat
) 0xffff *red
;
116 green
= CLAMP(green
);
117 cmap
->cells
[ndx
].component
[GLUT_GREEN
] = green
;
118 color
.green
= (GLfloat
) 0xffff *green
;
120 cmap
->cells
[ndx
].component
[GLUT_BLUE
] = blue
;
121 color
.blue
= (GLfloat
) 0xffff *blue
;
122 color
.flags
= DoRed
| DoGreen
| DoBlue
;
123 #if defined(_WIN32) || defined(__OS2PM__)
124 if (IsWindowVisible(__glutCurrentWindow
->win
)) {
125 XHDC
= __glutCurrentWindow
->hdc
;
130 XStoreColor(__glutDisplay
, cmap
->cmap
, &color
);
134 glutGetColor(int ndx
, int comp
)
136 GLUTcolormap
*colormap
;
139 if (__glutCurrentWindow
->renderWin
== __glutCurrentWindow
->win
) {
140 colormap
= __glutCurrentWindow
->colormap
;
141 vis
= __glutCurrentWindow
->vis
;
143 colormap
= __glutCurrentWindow
->overlay
->colormap
;
144 vis
= __glutCurrentWindow
->overlay
->vis
;
145 if (ndx
== __glutCurrentWindow
->overlay
->transparentPixel
) {
146 __glutWarning("glutGetColor: requesting overlay transparent index %d\n",
153 __glutWarning("glutGetColor: current window is RGBA");
156 #if defined(_WIN32) || defined(__OS2PM__)
157 #define OUT_OF_RANGE_NDX(ndx) (ndx >= 256 || ndx < 0)
159 #define OUT_OF_RANGE_NDX(ndx) (ndx >= vis->visual->map_entries || ndx < 0)
161 if (OUT_OF_RANGE_NDX(ndx
)) {
162 __glutWarning("glutGetColor: index %d out of range", ndx
);
165 return colormap
->cells
[ndx
].component
[comp
];
169 glutCopyColormap(int winnum
)
171 GLUTwindow
*window
= __glutWindowList
[winnum
- 1];
172 GLUTcolormap
*oldcmap
, *newcmap
;
175 if (__glutCurrentWindow
->renderWin
== __glutCurrentWindow
->win
) {
176 oldcmap
= __glutCurrentWindow
->colormap
;
177 dstvis
= __glutCurrentWindow
->vis
;
178 newcmap
= window
->colormap
;
180 oldcmap
= __glutCurrentWindow
->overlay
->colormap
;
181 dstvis
= __glutCurrentWindow
->overlay
->vis
;
182 if (!window
->overlay
) {
183 __glutWarning("glutCopyColormap: window %d has no overlay", winnum
);
186 newcmap
= window
->overlay
->colormap
;
190 __glutWarning("glutCopyColormap: destination colormap must be color index");
195 "glutCopyColormap: source colormap of window %d must be color index",
199 if (newcmap
== oldcmap
) {
200 /* Source and destination are the same; now copy needed. */
203 #if !defined(_WIN32) && !defined(__OS2PM__)
204 /* Play safe: compare visual IDs, not Visual*'s. */
205 if (newcmap
->visual
->visualid
== oldcmap
->visual
->visualid
) {
207 /* Visuals match! "Copy" by reference... */
208 __glutFreeColormap(oldcmap
);
210 if (__glutCurrentWindow
->renderWin
== __glutCurrentWindow
->win
) {
211 __glutCurrentWindow
->colormap
= newcmap
;
212 __glutCurrentWindow
->cmap
= newcmap
->cmap
;
214 __glutCurrentWindow
->overlay
->colormap
= newcmap
;
215 __glutCurrentWindow
->overlay
->cmap
= newcmap
->cmap
;
217 XSetWindowColormap(__glutDisplay
, __glutCurrentWindow
->renderWin
,
219 #if !defined(_WIN32) && !defined(__OS2PM__)
220 __glutPutOnWorkList(__glutToplevelOf(window
), GLUT_COLORMAP_WORK
);
224 GLUTcolormap
*copycmap
;
228 /* Visuals different - need a distinct X colormap! */
229 copycmap
= __glutAssociateNewColormap(dstvis
);
230 /* Wouldn't it be nice if XCopyColormapAndFree could be
231 told not to free the old colormap's entries! */
232 last
= newcmap
->size
;
233 if (last
> copycmap
->size
) {
234 last
= copycmap
->size
;
236 for (i
= last
- 1; i
>= 0; i
--) {
237 if (newcmap
->cells
[i
].component
[GLUT_RED
] >= 0.0) {
239 copycmap
->cells
[i
].component
[GLUT_RED
] =
240 newcmap
->cells
[i
].component
[GLUT_RED
];
241 color
.red
= (GLfloat
) 0xffff *
242 newcmap
->cells
[i
].component
[GLUT_RED
];
243 copycmap
->cells
[i
].component
[GLUT_GREEN
] =
244 newcmap
->cells
[i
].component
[GLUT_GREEN
];
245 color
.green
= (GLfloat
) 0xffff *
246 newcmap
->cells
[i
].component
[GLUT_GREEN
];
247 copycmap
->cells
[i
].component
[GLUT_BLUE
] =
248 newcmap
->cells
[i
].component
[GLUT_BLUE
];
249 color
.blue
= (GLfloat
) 0xffff *
250 newcmap
->cells
[i
].component
[GLUT_BLUE
];
251 color
.flags
= DoRed
| DoGreen
| DoBlue
;
252 XStoreColor(__glutDisplay
, copycmap
->cmap
, &color
);