color.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /****************************************************************************
  2. *
  3. * FILE: d.paint.labels/color.c
  4. * AUTHOR(S): Hamish Bowman, Dunedin New Zealand
  5. * PURPOSE: lib fns for working with RGBA_Color struct.
  6. * Inspired by ps.map's ps_colors.c
  7. * COPYRIGHT: (C) 2007 by Hamish Bowman, and the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. *****************************************************************************/
  14. /** ??? move these into libgis ??? (libraster for set_color_from_RGBA()) **/
  15. #include <grass/gis.h>
  16. #include <grass/colors.h>
  17. #include <grass/raster.h>
  18. #include <grass/display.h>
  19. #include <grass/glocale.h>
  20. /* fill RGBA array from RGB components (0-255) */
  21. void set_RGBA_from_components(RGBA_Color * color,
  22. const unsigned char r,
  23. const unsigned char g, const unsigned char b)
  24. {
  25. color->a = RGBA_COLOR_OPAQUE;
  26. color->r = r;
  27. color->g = g;
  28. color->b = b;
  29. }
  30. /*
  31. * \brief Parses color string and fill RGBA array
  32. *
  33. * If the color is valid the color's alpha value is set to RGBA_COLOR_OPAQUE.
  34. * The 'none' case is handed by setting the alpha channel to RGBA_COLOR_NONE.
  35. *
  36. * Returns: 1 - OK
  37. * 2 - NONE
  38. * 0 - Error
  39. *
  40. * \param color_string
  41. * \param RGBA_Color struct to be populated
  42. * \return int
  43. *
  44. */
  45. int set_RGBA_from_str(RGBA_Color * color, const char *clr_str)
  46. {
  47. int r, g, b;
  48. int ret;
  49. ret = G_str_to_color(clr_str, &r, &g, &b);
  50. if (ret == 1) {
  51. color->a = RGBA_COLOR_OPAQUE;
  52. color->r = (unsigned char)r;
  53. color->g = (unsigned char)g;
  54. color->b = (unsigned char)b;
  55. }
  56. else if (ret == 2)
  57. color->a = RGBA_COLOR_NONE;
  58. else
  59. G_fatal_error(_("[%s]: No such color"), clr_str);
  60. return ret;
  61. }
  62. /* set RGBA "color=none" flag */
  63. void unset_RGBA(RGBA_Color * color)
  64. {
  65. color->a = RGBA_COLOR_NONE;
  66. }
  67. /* tests if RGBA "color=none" */
  68. int RGBA_has_color(const RGBA_Color * color)
  69. {
  70. if (color->a != RGBA_COLOR_NONE)
  71. return TRUE;
  72. else
  73. return FALSE;
  74. }
  75. /* set active display color from values in the RGBA array */
  76. void set_color_from_RGBA(const RGBA_Color * color)
  77. {
  78. if (RGBA_has_color(color)) {
  79. G_debug(5, "setting display color to [%d:%d:%d]",
  80. color->r, color->g, color->b);
  81. D_RGB_color(color->r, color->g, color->b);
  82. }
  83. else
  84. G_debug(5, "skipped setting display color as it was set to \"none\"");
  85. }