Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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