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.

colorstring.c 3.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * colorstring.c: color from string functions
  17. */
  18. #include "config.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <stdarg.h>
  22. #include <string.h>
  23. #include "pipi.h"
  24. #include "pipi_internals.h"
  25. struct color_table
  26. {
  27. char name[255];
  28. float a,r,g,b;
  29. };
  30. struct color_table color_table[] =
  31. {
  32. {.name = "black" , 1, 0, 0, 0},
  33. {.name = "white" , 1, 1, 1, 1},
  34. {.name = "red" , 1, 1, 0, 0},
  35. {.name = "green" , 1, 0, 1, 0},
  36. {.name = "blue" , 1, 0, 0, 1},
  37. {.name = "yellow" , 1, 1, 1, 0},
  38. {.name = "cyan" , 1, 0, 1, 1},
  39. {.name = "magenta", 1, 1, 0, 1},
  40. {.name = "grey" , 1, 0.5, 0.5, 0.5},
  41. {.name = "gray" , 1, 0.5, 0.5, 0.5},
  42. {.name = "grey50" , 1, 0.5, 0.5, 0.5},
  43. {.name = "gray50" , 1, 0.5, 0.5, 0.5},
  44. {.name = "grey25" , 1, 0.25, 0.25, 0.25},
  45. {.name = "gray25" , 1, 0.25, 0.25, 0.25},
  46. };
  47. pipi_pixel_t *pipi_get_color_from_string(const char* s)
  48. {
  49. if(!s) return NULL;
  50. pipi_pixel_t *color = malloc(sizeof(pipi_pixel_t));
  51. if(s[0] == '#')
  52. {
  53. uint32_t c = 0;
  54. sscanf(s, "%x", &c);
  55. color->pixel_float.a = ((c&0xFF000000)>>24) / 255.0f;
  56. color->pixel_float.r = ((c&0x00FF0000)>>16) / 255.0f;
  57. color->pixel_float.g = ((c&0x0000FF00)>>8) / 255.0f;
  58. color->pixel_float.b = ((c&0x000000FF)>>0) / 255.0f;
  59. }
  60. else if(!strncmp(s, "rgb(", 4))
  61. {
  62. uint32_t r ,g ,b;
  63. sscanf(s, "rgb(%u,%u,%u)", &r, &g, &b);
  64. color->pixel_float.r = r / 255.0f;
  65. color->pixel_float.g = g / 255.0f;
  66. color->pixel_float.b = b / 255.0f;
  67. }
  68. else if(!strncmp(s, "frgb(", 5))
  69. {
  70. float r ,g ,b;
  71. sscanf(s, "frgb(%f,%f,%f)", &r, &g, &b);
  72. color->pixel_float.r = r;
  73. color->pixel_float.g = g;
  74. color->pixel_float.b = b;
  75. }
  76. else if(!strncmp(s, "argb(", 4))
  77. {
  78. uint32_t a, r ,g ,b;
  79. sscanf(s, "argb(%u,%u,%u,%u)", &a, &r, &g, &b);
  80. color->pixel_float.a = a / 255.0f;
  81. color->pixel_float.r = r / 255.0f;
  82. color->pixel_float.g = g / 255.0f;
  83. color->pixel_float.b = b / 255.0f;
  84. }
  85. else if(!strncmp(s, "fargb(", 5))
  86. {
  87. float a, r ,g ,b;
  88. sscanf(s, "fargb(%f, %f,%f,%f)", &a, &r, &g, &b);
  89. color->pixel_float.a = a;
  90. color->pixel_float.r = r;
  91. color->pixel_float.g = g;
  92. color->pixel_float.b = b;
  93. }
  94. else
  95. {
  96. unsigned int i;
  97. for(i=0; i<sizeof(color_table); i++)
  98. {
  99. if(!strcasecmp(color_table[i].name,s))
  100. {
  101. color->pixel_float.a = color_table[i].a;
  102. color->pixel_float.r = color_table[i].r;
  103. color->pixel_float.g = color_table[i].g;
  104. color->pixel_float.b = color_table[i].b;
  105. break;
  106. }
  107. }
  108. }
  109. return color;
  110. }