r_color.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include "sys-defines.h"
  16. #include "extern.h"
  17. #define ONEBYTE 0xff
  18. #define REGIS_NUM_STD_COLORS 8
  19. /* standard ReGIS colors */
  20. static const plColor regis_stdcolors[REGIS_NUM_STD_COLORS] =
  21. {
  22. {0xff, 0x00, 0x00}, /* Red */
  23. {0x00, 0xff, 0x00}, /* Green */
  24. {0x00, 0x00, 0xff}, /* Blue */
  25. {0x00, 0xff, 0xff}, /* Cyan */
  26. {0xff, 0x00, 0xff}, /* Magenta */
  27. {0xff, 0xff, 0x00}, /* Yellow */
  28. {0x00, 0x00, 0x00}, /* Black */
  29. {0xff, 0xff, 0xff} /* White */
  30. };
  31. /* corresponding one-letter abbreviations (in same order as preceding) */
  32. static const char regis_color_chars[REGIS_NUM_STD_COLORS] =
  33. { 'r', 'g', 'b', 'c', 'm', 'y', 'd', 'w' };
  34. /* forward references */
  35. static int rgb_to_best_stdcolor (plColor rgb);
  36. void
  37. _pl_r_set_pen_color(S___(Plotter *_plotter))
  38. {
  39. int new_color;
  40. new_color = rgb_to_best_stdcolor (_plotter->drawstate->fgcolor);
  41. if (_plotter->regis_fgcolor_is_unknown
  42. || _plotter->regis_fgcolor != new_color)
  43. {
  44. char tmpbuf[32];
  45. sprintf (tmpbuf, "W(I(%c))\n",
  46. regis_color_chars[new_color]);
  47. _write_string (_plotter->data, tmpbuf);
  48. _plotter->regis_fgcolor = new_color;
  49. _plotter->regis_fgcolor_is_unknown = false;
  50. }
  51. }
  52. void
  53. _pl_r_set_fill_color(S___(Plotter *_plotter))
  54. {
  55. int new_color;
  56. /* sanity check */
  57. if (_plotter->drawstate->fill_type == 0)
  58. return;
  59. new_color = rgb_to_best_stdcolor (_plotter->drawstate->fillcolor);
  60. if (_plotter->regis_fgcolor_is_unknown
  61. || _plotter->regis_fgcolor != new_color)
  62. {
  63. char tmpbuf[32];
  64. sprintf (tmpbuf, "W(I(%c))\n",
  65. regis_color_chars[new_color]);
  66. _write_string (_plotter->data, tmpbuf);
  67. _plotter->regis_fgcolor = new_color;
  68. _plotter->regis_fgcolor_is_unknown = false;
  69. }
  70. }
  71. void
  72. _pl_r_set_bg_color(S___(Plotter *_plotter))
  73. {
  74. int new_color;
  75. new_color = rgb_to_best_stdcolor (_plotter->drawstate->bgcolor);
  76. if (_plotter->regis_bgcolor_is_unknown
  77. || _plotter->regis_bgcolor != new_color)
  78. {
  79. char tmpbuf[32];
  80. sprintf (tmpbuf, "S(I(%c))\n",
  81. regis_color_chars[new_color]);
  82. _write_string (_plotter->data, tmpbuf);
  83. _plotter->regis_bgcolor = new_color;
  84. _plotter->regis_bgcolor_is_unknown = false;
  85. /* note: must do an erase, for the just-set background color to show
  86. up on the ReGIS display */
  87. }
  88. }
  89. /* compute best approximation, in color table, to a specified 48-bit color */
  90. static int
  91. rgb_to_best_stdcolor (plColor rgb)
  92. {
  93. int red, green, blue;
  94. unsigned long int difference = INT_MAX;
  95. int i, best = 0; /* keep compiler happy */
  96. /* convert from 48-bit color to 24-bit */
  97. red = rgb.red;
  98. green = rgb.green;
  99. blue = rgb.blue;
  100. red = (red >> 8) & ONEBYTE;
  101. green = (green >> 8) & ONEBYTE;
  102. blue = (blue >> 8) & ONEBYTE;
  103. for (i = 0; i < REGIS_NUM_STD_COLORS; i++)
  104. {
  105. unsigned long int newdifference;
  106. newdifference = (((regis_stdcolors[i].red - red)
  107. * (regis_stdcolors[i].red - red))
  108. + ((regis_stdcolors[i].green - green)
  109. * (regis_stdcolors[i].green - green))
  110. + ((regis_stdcolors[i].blue - blue)
  111. * (regis_stdcolors[i].blue - blue)));
  112. if (newdifference < difference)
  113. {
  114. difference = newdifference;
  115. best = i;
  116. }
  117. }
  118. return best;
  119. }