s_color.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 NUM_SVG_COLORNAMES 16
  18. static const plColorNameInfo _svg_colornames[NUM_SVG_COLORNAMES + 1] =
  19. {
  20. {"aqua", 0x00, 0xff, 0xff},
  21. {"black", 0x00, 0x00, 0x00},
  22. {"blue", 0x00, 0x00, 0xff},
  23. {"fuchsia", 0xff, 0x00, 0xff},
  24. {"gray", 0x80, 0x80, 0x80},
  25. {"green", 0x00, 0x80, 0x00},
  26. {"lime", 0x00, 0xff, 0x00},
  27. {"maroon", 0x80, 0x00, 0x00},
  28. {"navy", 0x00, 0x00, 0x80},
  29. {"olive", 0x80, 0x80, 0x00},
  30. {"purple", 0x80, 0x00, 0x80},
  31. {"red", 0xff, 0x00, 0x00},
  32. {"silver", 0xc0, 0xc0, 0xc0},
  33. {"teal", 0x00, 0x80, 0x80},
  34. {"white", 0xff, 0xff, 0xff},
  35. {"yellow", 0xff, 0xff, 0x00},
  36. {NULL, 0, 0, 0}
  37. };
  38. /* convert a 48-bit libplot color to a string resembling "#ffffff" for SVG;
  39. may also return a string in our database of SVG's builtin colors */
  40. const char *
  41. _libplot_color_to_svg_color (plColor color_48, char charbuf[8])
  42. {
  43. plColor color_24;
  44. int i;
  45. bool found = false;
  46. const char *svg_color;
  47. color_24.red = ((unsigned int)color_48.red) >> 8;
  48. color_24.green = ((unsigned int)color_48.green) >> 8;
  49. color_24.blue = ((unsigned int)color_48.blue) >> 8;
  50. for (i = 0; i < NUM_SVG_COLORNAMES; i++)
  51. {
  52. if (color_24.red == _svg_colornames[i].red
  53. && color_24.green == _svg_colornames[i].green
  54. && color_24.blue == _svg_colornames[i].blue)
  55. {
  56. found = true;
  57. break;
  58. }
  59. }
  60. if (found)
  61. svg_color = _svg_colornames[i].name;
  62. else
  63. {
  64. sprintf (charbuf, "#%02x%02x%02x",
  65. color_24.red, color_24.green, color_24.blue);
  66. svg_color = charbuf;
  67. }
  68. return svg_color;
  69. }