x_color.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file contains device-specific color computation routines. These
  16. routines are called by various XDrawablePlotter (and XPlotter)
  17. methods. */
  18. #include "sys-defines.h"
  19. #include "extern.h"
  20. /* we call this routine to set the foreground color in the X GC used for
  21. drawing, only when needed (just before an object is written out) */
  22. void
  23. _pl_x_set_pen_color(S___(Plotter *_plotter))
  24. {
  25. plColor old, new1;
  26. XColor rgb;
  27. new1 = _plotter->drawstate->fgcolor;
  28. old = _plotter->drawstate->x_current_fgcolor; /* i.e. as stored in gc */
  29. if (new1.red == old.red && new1.green == old.green && new1.blue == old.blue
  30. && _plotter->drawstate->x_gc_fgcolor_status)
  31. /* can use current color cell */
  32. return;
  33. rgb.red = new1.red;
  34. rgb.green = new1.green;
  35. rgb.blue = new1.blue;
  36. /* retrieve matching color cell, if possible */
  37. if (_pl_x_retrieve_color (R___(_plotter) &rgb) == false)
  38. return;
  39. /* select pen color as foreground color in GC used for drawing */
  40. XSetForeground (_plotter->x_dpy, _plotter->drawstate->x_gc_fg, rgb.pixel);
  41. /* save the new pixel value */
  42. _plotter->drawstate->x_gc_fgcolor = rgb.pixel;
  43. /* flag this as a genuine pixel value */
  44. _plotter->drawstate->x_gc_fgcolor_status = true;
  45. /* update non-opaque representation of stored foreground color */
  46. _plotter->drawstate->x_current_fgcolor = new1;
  47. }
  48. /* we call this routine to set the foreground color in the X GC used for
  49. filling, only when needed (just before an object is written out) */
  50. void
  51. _pl_x_set_fill_color(S___(Plotter *_plotter))
  52. {
  53. plColor old, new1;
  54. XColor rgb;
  55. if (_plotter->drawstate->fill_type == 0) /* transparent */
  56. /* don't do anything, fill color will be ignored when writing objects*/
  57. return;
  58. new1 = _plotter->drawstate->fillcolor;
  59. old = _plotter->drawstate->x_current_fillcolor; /* as used in GC */
  60. if (new1.red == old.red && new1.green == old.green && new1.blue == old.blue
  61. && _plotter->drawstate->x_gc_fillcolor_status)
  62. /* can use current color cell */
  63. return;
  64. rgb.red = (short)_plotter->drawstate->fillcolor.red;
  65. rgb.green = (short)_plotter->drawstate->fillcolor.green;
  66. rgb.blue = (short)_plotter->drawstate->fillcolor.blue;
  67. /* retrieve matching color cell, if possible */
  68. if (_pl_x_retrieve_color (R___(_plotter) &rgb) == false)
  69. return;
  70. /* select fill color as foreground color in GC used for filling */
  71. XSetForeground (_plotter->x_dpy, _plotter->drawstate->x_gc_fill, rgb.pixel);
  72. /* save the new pixel value */
  73. _plotter->drawstate->x_gc_fillcolor = rgb.pixel;
  74. /* flag this as a genuine pixel value */
  75. _plotter->drawstate->x_gc_fillcolor_status = true;
  76. /* update non-opaque representation of stored fill color */
  77. _plotter->drawstate->x_current_fillcolor = new1;
  78. }
  79. /* we call this routine to set the foreground color in the X GC used for
  80. erasing, only when needed (just before an erasure takes place) */
  81. void
  82. _pl_x_set_bg_color(S___(Plotter *_plotter))
  83. {
  84. plColor old, new1;
  85. XColor rgb;
  86. new1 = _plotter->drawstate->bgcolor;
  87. old = _plotter->drawstate->x_current_bgcolor; /* i.e. as stored in gc */
  88. if (new1.red == old.red && new1.green == old.green && new1.blue == old.blue
  89. && _plotter->drawstate->x_gc_bgcolor_status)
  90. /* can use current color cell */
  91. return;
  92. rgb.red = new1.red;
  93. rgb.green = new1.green;
  94. rgb.blue = new1.blue;
  95. /* retrieve matching color cell, if possible */
  96. if (_pl_x_retrieve_color (R___(_plotter) &rgb) == false)
  97. return;
  98. /* select background color as foreground color in GC used for erasing */
  99. XSetForeground (_plotter->x_dpy, _plotter->drawstate->x_gc_bg, rgb.pixel);
  100. /* save the new pixel value */
  101. _plotter->drawstate->x_gc_bgcolor = rgb.pixel;
  102. /* flag this as a genuine pixel value */
  103. _plotter->drawstate->x_gc_bgcolor_status = true;
  104. /* update non-opaque representation of stored background color */
  105. _plotter->drawstate->x_current_bgcolor = new1;
  106. }
  107. /* This is the internal X color retrieval routine. If the visual class
  108. is known and is TrueColor, it computes the X pixel value from a 48-bit
  109. RGB without invoking XAllocColor(), which would require a round trip
  110. to the server.
  111. Otherwise, it first searches for a specified RGB in a cache of
  112. previously retrieved color cells, and if that fails, tries to allocate a
  113. new color cell by calling XAllocColor(). If that fails, and a new
  114. colormap can be switched to, it switches to a new colormap and tries
  115. again. If that attempt also fails, it searches the cache for the
  116. colorcell with an RGB that's closest to the specified RGB. Only if that
  117. fails as well (i.e. the cache is empty), does it return false.
  118. Cache is maintained as a linked list (not optimal, but it facilitates
  119. color cell management; see comment in x_erase.c). */
  120. bool
  121. _pl_x_retrieve_color (R___(Plotter *_plotter) XColor *rgb_ptr)
  122. {
  123. plColorRecord *cptr;
  124. int rgb_red = rgb_ptr->red;
  125. int rgb_green = rgb_ptr->green;
  126. int rgb_blue = rgb_ptr->blue;
  127. int xretval;
  128. #ifdef LIBPLOTTER
  129. if (_plotter->x_visual && _plotter->x_visual->c_class == TrueColor)
  130. #else
  131. #ifdef __cplusplus
  132. if (_plotter->x_visual && _plotter->x_visual->c_class == TrueColor)
  133. #else
  134. if (_plotter->x_visual && _plotter->x_visual->class == TrueColor)
  135. #endif
  136. #endif
  137. /* can compute pixel value from RGB without calling XAllocColor(), by
  138. bit-twiddling */
  139. {
  140. unsigned long red_mask, green_mask, blue_mask;
  141. int red_shift, green_shift, blue_shift;
  142. int red_bits, green_bits, blue_bits;
  143. /* first, compute {R,G,B}_bits and {R,G,B}_shift (should be precomputed) */
  144. red_mask = _plotter->x_visual->red_mask; red_shift = red_bits = 0;
  145. while (!(red_mask & 1))
  146. {
  147. red_mask >>= 1; red_shift++;
  148. }
  149. while (red_mask & 1)
  150. {
  151. red_mask >>= 1; red_bits++;
  152. }
  153. green_mask = _plotter->x_visual->green_mask; green_shift = green_bits = 0;
  154. while (!(green_mask & 1))
  155. {
  156. green_mask >>= 1; green_shift++;
  157. }
  158. while (green_mask & 1)
  159. {
  160. green_mask >>= 1; green_bits++;
  161. }
  162. blue_mask = _plotter->x_visual->blue_mask; blue_shift = blue_bits = 0;
  163. while (!(blue_mask & 1))
  164. {
  165. blue_mask >>= 1; blue_shift++;
  166. }
  167. while (blue_mask & 1)
  168. {
  169. blue_mask >>= 1; blue_bits++;
  170. }
  171. /* compute and pass back pixel, as a 32-bit unsigned long */
  172. rgb_red = rgb_red >> (16 - red_bits);
  173. rgb_green = rgb_green >> (16 - green_bits);
  174. rgb_blue = rgb_blue >> (16 - blue_bits);
  175. rgb_ptr->pixel = ((rgb_red << red_shift) & _plotter->x_visual->red_mask)
  176. | ((rgb_green << green_shift) & _plotter->x_visual->green_mask)
  177. | ((rgb_blue << blue_shift) & _plotter->x_visual->blue_mask);
  178. #if 0
  179. fprintf (stderr, "pixel=0x%lx, R=0x%hx, G=0x%hx, B=0x%hx\n",
  180. rgb_ptr->pixel, rgb_ptr->red, rgb_ptr->green, rgb_ptr->blue);
  181. #endif
  182. return true;
  183. }
  184. /* If we got here, we weren't able to compute the pixel value from the
  185. RGB without calling XAllocColor(). So may have to do that, but first
  186. we consult a list of previously allocated color cells. */
  187. /* search cache list */
  188. for (cptr = _plotter->x_colorlist; cptr; cptr = cptr->next)
  189. {
  190. XColor cached_rgb;
  191. cached_rgb = cptr->rgb;
  192. if (cached_rgb.red == rgb_red
  193. && cached_rgb.green == rgb_green
  194. && cached_rgb.blue == rgb_blue)
  195. /* found in cache */
  196. {
  197. /* keep track of page, frame number in which cell was most
  198. recently accessed */
  199. cptr->page_number = _plotter->data->page_number;
  200. cptr->frame_number = _plotter->data->frame_number;
  201. /* return stored pixel value */
  202. *rgb_ptr = cached_rgb;
  203. return true;
  204. }
  205. }
  206. /* not in cache, so try to allocate a new color cell, if colormap hasn't
  207. been flagged as bad (i.e. full) */
  208. if (_plotter->x_cmap_type != X_CMAP_BAD)
  209. {
  210. xretval = XAllocColor (_plotter->x_dpy, _plotter->x_cmap, rgb_ptr);
  211. if (xretval == 0)
  212. /* failure */
  213. {
  214. if (_plotter->x_cmap_type == X_CMAP_ORIG)
  215. /* colormap is the one we started with, so try switching and
  216. reallocating */
  217. {
  218. /* Which method is invoked here depends on the type of
  219. Plotter. If this is an X Plotter, replace its colormap by
  220. a copied, private colormap if we can; otherwise we flag
  221. the colormap as bad (i.e. filled up). If this is an
  222. XDrawable Plotter, this method doesn't do anything, so
  223. colormap just gets flagged as bad. */
  224. _maybe_get_new_colormap (S___(_plotter));
  225. if (_plotter->x_cmap_type != X_CMAP_NEW)
  226. _plotter->x_cmap_type = X_CMAP_BAD;
  227. if (_plotter->x_cmap_type != X_CMAP_BAD)
  228. /* got a new colormap; try again to allocate color cell */
  229. xretval = XAllocColor (_plotter->x_dpy, _plotter->x_cmap, rgb_ptr);
  230. }
  231. }
  232. }
  233. else
  234. /* colormap is bad, i.e. full; no hope of allocating a new colorcell */
  235. xretval = 0;
  236. if (xretval == 0)
  237. /* allocation failed, and no switching or further switching of
  238. colormaps is possible; so simply search cache list for closest
  239. color, among previously allocated cells */
  240. {
  241. XColor cached_rgb;
  242. plColorRecord *best_cptr = NULL;
  243. double distance = DBL_MAX;
  244. /* flag colormap as bad, i.e. full; no further color cell allocations
  245. will be attempted */
  246. _plotter->x_cmap_type = X_CMAP_BAD;
  247. if (_plotter->x_colormap_warning_issued == false)
  248. {
  249. _plotter->warning(R___(_plotter)
  250. "color supply exhausted, can't create new colors");
  251. _plotter->x_colormap_warning_issued = true;
  252. }
  253. for (cptr = _plotter->x_colorlist; cptr; cptr = cptr->next)
  254. {
  255. double newdistance;
  256. cached_rgb = cptr->rgb;
  257. newdistance = (((rgb_red - cached_rgb.red)
  258. * (rgb_red - cached_rgb.red))
  259. + ((rgb_green - cached_rgb.green)
  260. * (rgb_green - cached_rgb.green))
  261. + ((rgb_blue - cached_rgb.blue)
  262. * (rgb_blue - cached_rgb.blue)));
  263. if (newdistance < distance)
  264. {
  265. distance = newdistance;
  266. best_cptr = cptr;
  267. }
  268. }
  269. if (best_cptr != (plColorRecord *)NULL)
  270. {
  271. /* keep track of page, frame number in which cell was most
  272. recently accessed */
  273. best_cptr->page_number = _plotter->data->page_number;
  274. best_cptr->frame_number = _plotter->data->frame_number;
  275. /* return pixel value via pointer */
  276. *rgb_ptr = best_cptr->rgb;
  277. return true;
  278. }
  279. else
  280. /* cache must be empty; bad news */
  281. return false;
  282. }
  283. else
  284. /* allocation succeeded, add new color cell to head of cache list */
  285. {
  286. cptr = (plColorRecord *)_pl_xmalloc (sizeof (plColorRecord));
  287. cptr->rgb = *rgb_ptr;
  288. /* include unquantized RGB values */
  289. cptr->rgb.red = rgb_red;
  290. cptr->rgb.green = rgb_green;
  291. cptr->rgb.blue = rgb_blue;
  292. cptr->allocated = true; /* vestigial field */
  293. /* keep track of page, frame number in which cell was allocated */
  294. cptr->page_number = _plotter->data->page_number;
  295. cptr->frame_number = _plotter->data->frame_number;
  296. cptr->next = _plotter->x_colorlist;
  297. _plotter->x_colorlist = cptr;
  298. #if 0
  299. fprintf (stderr, "pixel=0x%lx, R=0x%hx, G=0x%hx, B=0x%hx\n",
  300. cptr->rgb.pixel, cptr->rgb.red, cptr->rgb.green, cptr->rgb.blue);
  301. #endif
  302. return true;
  303. }
  304. }