BRL-CAD
color.c
Go to the documentation of this file.
1 /* C O L O R . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libdm/color.c
21  *
22  */
23 
24 #include "common.h"
25 
26 #ifdef DM_X
27 
28 #include <stdio.h>
29 #include <string.h>
30 
31 #ifdef HAVE_X11_XLIB_H
32 # include <X11/Xlib.h>
33 #endif
34 
35 #include "bu/malloc.h"
36 #include "dm/dm_xvars.h"
37 
38 
39 /* Return the allocated pixel value that most closely represents
40  the color requested. */
41 unsigned long
42 X_get_pixel(unsigned char r, unsigned char g, unsigned char b, long unsigned int *pixels, int cd)
43  /* values assumed to be [0, 255] */
44 
45  /* cube dimension */
46 {
47  fastf_t f;
48  int rf, gf, bf;
49  int idx;
50 
51  if (r == 0 && g == 0 && b == 0)
52  return pixels[0];
53 
54  f = cd >> 8;
55  rf = r * f;
56  gf = g * f;
57  bf = b * f;
58 
59  idx = rf * cd * cd + gf * cd + bf;
60 
61  if (idx == 0) {
62  if (r != 0)
63  idx = cd * cd;
64 
65  if (g != 0)
66  idx += cd;
67 
68  if (b != 0)
69  idx += 1;
70  }
71 
72  return pixels[idx];
73 }
74 
75 /*
76  * Alloc/Store (hi - low) colors from src colormap into dest.
77  */
78 HIDDEN void
79 _X_copy_cmap(Display *dpy, Colormap dest, Colormap src, int low, int hi, int store)
80 {
81  int i;
82  int ncolors;
83  XColor *colors;
84 
85  ncolors = hi - low;
86  colors = (XColor *)bu_calloc(ncolors, sizeof(XColor), "dm_load_cmap: colors");
87 
88  for (i = low; i < hi; ++i)
89  colors[i].pixel = i;
90  XQueryColors(dpy, src, colors, ncolors);
91 
92  if (store) {
93  XStoreColors(dpy, dest, colors, ncolors);
94  } else {
95  for (i = 0; i < ncolors; ++i) {
96  XAllocColor(dpy, dest, &colors[i]);
97  }
98  }
99 
100  bu_free((void *)colors, "dm_load_cmap: colors");
101 }
102 
103 void
104 X_allocate_color_cube(Display *dpy, Colormap cmap, long unsigned int *pixels, int cd, int cmap_base, int store)
105 
106 
107  /* cube dimension */
108 
109 
110 {
111  XColor color;
112  Colormap default_cmap;
113  int i;
114  int r, g, b;
115  int incr; /* increment */
116 
117  INIT_XCOLOR(&color);
118 
119  /*
120  * Copy default colors below cmap_base to private colormap to help
121  * reduce flashing. Assuming cmap is private and empty, we can be
122  * fairly certain to get the colors we want in the right order even
123  * though cmap may be shared.
124  */
125  default_cmap = DefaultColormap(dpy, DefaultScreen(dpy));
126  _X_copy_cmap(dpy, cmap, default_cmap, 0, cmap_base, store);
127 
128  incr = 65535 / (cd - 1);
129 
130  /* store color cube at cmap_base and above */
131  for (i = r = 0; r < 65536; r = r + incr)
132  for (g = 0; g < 65536; g = g + incr)
133  for (b = 0; b < 65536; b = b + incr, ++i) {
134  color.red = (unsigned short)r;
135  color.green = (unsigned short)g;
136  color.blue = (unsigned short)b;
137 
138  if (store) {
139  color.flags = DoRed|DoGreen|DoBlue;
140  pixels[i] = color.pixel = i + cmap_base;
141  XStoreColor(dpy, cmap, &color);
142  } else {
143  XAllocColor(dpy, cmap, &color);
144  pixels[i] = color.pixel;
145  }
146  }
147 }
148 
149 
150 #endif /* DM_X */
151 
152 /*
153  * Local Variables:
154  * mode: C
155  * tab-width: 8
156  * indent-tabs-mode: t
157  * c-file-style: "stroustrup"
158  * End:
159  * ex: shiftwidth=4 tabstop=8
160  */
long Display
Definition: dm_xvars.h:49
Header file for the BRL-CAD common definitions.
#define HIDDEN
Definition: common.h:86
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
long Colormap
Definition: dm_xvars.h:51
#define INIT_XCOLOR(c)
Definition: dm_xvars.h:56
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
double fastf_t
Definition: defines.h:300