t_color.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /* For TekPlotter objects, setting the pen color or background color has no
  16. effect unless the plotter is using the Tektronix emulation of MS-DOS
  17. kermit. If so, we compute a quantized color and output the appropriate
  18. ANSI escape sequence, if the color is different from the ANSI.SYS color
  19. the emulation is currently using.
  20. When the TekPlotter is created, the current ANSI.SYS pen and bg colors
  21. are set to -1 (see t_defplot.c). That's a nonsensical value, equivalent
  22. to `unknown'. */
  23. #include "sys-defines.h"
  24. #include "extern.h"
  25. #define ONEBYTE (0xff)
  26. /* forward references */
  27. static int kermit_pseudocolor (int red, int green, int blue);
  28. void
  29. _pl_t_set_pen_color(S___(Plotter *_plotter))
  30. {
  31. if (_plotter->tek_display_type == TEK_DPY_KERMIT)
  32. {
  33. int new_kermit_fgcolor;
  34. new_kermit_fgcolor =
  35. kermit_pseudocolor (_plotter->drawstate->fgcolor.red,
  36. _plotter->drawstate->fgcolor.green,
  37. _plotter->drawstate->fgcolor.blue);
  38. if (new_kermit_fgcolor != _plotter->tek_kermit_fgcolor)
  39. {
  40. _write_string (_plotter->data,
  41. _pl_t_kermit_fgcolor_escapes[new_kermit_fgcolor]);
  42. _plotter->tek_kermit_fgcolor = new_kermit_fgcolor;
  43. }
  44. }
  45. }
  46. void
  47. _pl_t_set_bg_color(S___(Plotter *_plotter))
  48. {
  49. if (_plotter->tek_display_type == TEK_DPY_KERMIT)
  50. {
  51. int new_kermit_bgcolor;
  52. new_kermit_bgcolor =
  53. kermit_pseudocolor (_plotter->drawstate->bgcolor.red,
  54. _plotter->drawstate->bgcolor.green,
  55. _plotter->drawstate->bgcolor.blue);
  56. if (new_kermit_bgcolor != _plotter->tek_kermit_bgcolor)
  57. {
  58. _write_string (_plotter->data,
  59. _pl_t_kermit_bgcolor_escapes[new_kermit_bgcolor]);
  60. _plotter->tek_kermit_bgcolor = new_kermit_bgcolor;
  61. }
  62. }
  63. }
  64. /* kermit_pseudocolor quantizes to one of kermit's native 16 colors. (They
  65. provide a [rather strange] partition of the color cube; see
  66. t_color2.c.) */
  67. /* find closest known point within the RGB color cube, using Euclidean
  68. distance as our metric */
  69. static int
  70. kermit_pseudocolor (int red, int green, int blue)
  71. {
  72. unsigned long int difference = INT_MAX;
  73. int i;
  74. int best = 0;
  75. /* reduce to 24 bits */
  76. red = (red >> 8) & ONEBYTE;
  77. green = (green >> 8) & ONEBYTE;
  78. blue = (blue >> 8) & ONEBYTE;
  79. for (i = 0; i < TEK_NUM_ANSI_SYS_COLORS; i++)
  80. {
  81. unsigned long int newdifference;
  82. if (_pl_t_kermit_stdcolors[i].red == 0xff
  83. && _pl_t_kermit_stdcolors[i].green == 0xff
  84. && _pl_t_kermit_stdcolors[i].blue == 0xff)
  85. /* white is a possible quantization only for white itself (our
  86. convention) */
  87. {
  88. if (red == 0xff && green == 0xff && blue == 0xff)
  89. {
  90. difference = 0;
  91. best = i;
  92. }
  93. continue;
  94. }
  95. newdifference = (((_pl_t_kermit_stdcolors[i].red - red)
  96. * (_pl_t_kermit_stdcolors[i].red - red))
  97. + ((_pl_t_kermit_stdcolors[i].green - green)
  98. * (_pl_t_kermit_stdcolors[i].green - green))
  99. + ((_pl_t_kermit_stdcolors[i].blue - blue)
  100. * (_pl_t_kermit_stdcolors[i].blue - blue)));
  101. if (newdifference < difference)
  102. {
  103. difference = newdifference;
  104. best = i;
  105. }
  106. }
  107. return best;
  108. }