You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

152 lines
4.1 KiB

  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This library is free software. It comes without any warranty, to
  10. * the extent permitted by applicable law. You can redistribute it
  11. * and/or modify it under the terms of the Do What The Fuck You Want
  12. * To Public License, Version 2, as published by Sam Hocevar. See
  13. * http://sam.zoy.org/wtfpl/COPYING for more details.
  14. */
  15. /*
  16. * color.c: color parsing functions
  17. */
  18. #include "config.h"
  19. #include "common.h"
  20. /*
  21. * libpipi Pathetic image processing interface library
  22. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  23. * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org
  24. * All Rights Reserved
  25. *
  26. * $Id$
  27. *
  28. * This library is free software. It comes without any warranty, to
  29. * the extent permitted by applicable law. You can redistribute it
  30. * and/or modify it under the terms of the Do What The Fuck You Want
  31. * To Public License, Version 2, as published by Sam Hocevar. See
  32. * http://sam.zoy.org/wtfpl/COPYING for more details.
  33. */
  34. /*
  35. * colorstring.c: color from string functions
  36. */
  37. #include "config.h"
  38. #include "common.h"
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <stdarg.h>
  42. #include <stdint.h>
  43. #include <string.h>
  44. #include "pipi.h"
  45. #include "pipi_internals.h"
  46. struct color_table
  47. {
  48. char name[255];
  49. float a,r,g,b;
  50. };
  51. struct color_table color_table[] =
  52. {
  53. {.name = "black" , 1, 0, 0, 0},
  54. {.name = "white" , 1, 1, 1, 1},
  55. {.name = "red" , 1, 1, 0, 0},
  56. {.name = "green" , 1, 0, 1, 0},
  57. {.name = "blue" , 1, 0, 0, 1},
  58. {.name = "yellow" , 1, 1, 1, 0},
  59. {.name = "cyan" , 1, 0, 1, 1},
  60. {.name = "magenta", 1, 1, 0, 1},
  61. {.name = "grey" , 1, 0.5, 0.5, 0.5},
  62. {.name = "gray" , 1, 0.5, 0.5, 0.5},
  63. {.name = "grey50" , 1, 0.5, 0.5, 0.5},
  64. {.name = "gray50" , 1, 0.5, 0.5, 0.5},
  65. {.name = "grey25" , 1, 0.25, 0.25, 0.25},
  66. {.name = "gray25" , 1, 0.25, 0.25, 0.25},
  67. };
  68. pipi_pixel_t *pipi_get_color_from_string(const char* s)
  69. {
  70. if(!s) return NULL;
  71. pipi_pixel_t *color = malloc(sizeof(pipi_pixel_t));
  72. if(s[0] == '#')
  73. {
  74. uint32_t c = 0;
  75. sscanf(s, "%x", &c);
  76. color->pixel_float.a = ((c&0xFF000000)>>24) / 255.0f;
  77. color->pixel_float.r = ((c&0x00FF0000)>>16) / 255.0f;
  78. color->pixel_float.g = ((c&0x0000FF00)>>8) / 255.0f;
  79. color->pixel_float.b = ((c&0x000000FF)>>0) / 255.0f;
  80. }
  81. else if(!strncmp(s, "rgb(", 4))
  82. {
  83. uint32_t r ,g ,b;
  84. sscanf(s, "rgb(%u,%u,%u)", &r, &g, &b);
  85. color->pixel_float.r = r / 255.0f;
  86. color->pixel_float.g = g / 255.0f;
  87. color->pixel_float.b = b / 255.0f;
  88. }
  89. else if(!strncmp(s, "frgb(", 5))
  90. {
  91. float r ,g ,b;
  92. sscanf(s, "frgb(%f,%f,%f)", &r, &g, &b);
  93. color->pixel_float.r = r;
  94. color->pixel_float.g = g;
  95. color->pixel_float.b = b;
  96. }
  97. else if(!strncmp(s, "argb(", 4))
  98. {
  99. uint32_t a, r ,g ,b;
  100. sscanf(s, "argb(%u,%u,%u,%u)", &a, &r, &g, &b);
  101. color->pixel_float.a = a / 255.0f;
  102. color->pixel_float.r = r / 255.0f;
  103. color->pixel_float.g = g / 255.0f;
  104. color->pixel_float.b = b / 255.0f;
  105. }
  106. else if(!strncmp(s, "fargb(", 5))
  107. {
  108. float a, r ,g ,b;
  109. sscanf(s, "fargb(%f, %f,%f,%f)", &a, &r, &g, &b);
  110. color->pixel_float.a = a;
  111. color->pixel_float.r = r;
  112. color->pixel_float.g = g;
  113. color->pixel_float.b = b;
  114. }
  115. else
  116. {
  117. unsigned int i;
  118. for(i=0; i<sizeof(color_table); i++)
  119. {
  120. if(!strcasecmp(color_table[i].name,s))
  121. {
  122. color->pixel_float.a = color_table[i].a;
  123. color->pixel_float.r = color_table[i].r;
  124. color->pixel_float.g = color_table[i].g;
  125. color->pixel_float.b = color_table[i].b;
  126. break;
  127. }
  128. }
  129. }
  130. return color;
  131. }