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.

rgbcolors.cc 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. Color utilities.
  3. Copyright (C) 2005 Robert Lipe, robertlipe+source@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include "defs.h"
  17. #include <stdlib.h>
  18. /*
  19. * Colors derived from http://www.w3.org/TR/SVG/types.html#ColorKeywords
  20. * which is also referenced in the CSS3 spec from w3.org as being the
  21. * same colors supported by X11.
  22. */
  23. static const struct {
  24. const char* cn;
  25. unsigned char r;
  26. unsigned char g;
  27. unsigned char b;
  28. } color_table[] = {
  29. {"aliceblue", 240, 248, 255},
  30. {"antiquewhite", 250, 235, 215},
  31. {"aqua", 0, 255, 255},
  32. {"aquamarine", 127, 255, 212},
  33. {"azure", 240, 255, 255},
  34. {"beige", 245, 245, 220},
  35. {"bisque", 255, 228, 196},
  36. {"black", 0, 0, 0},
  37. {"blanchedalmond", 255, 235, 205},
  38. {"blue", 0, 0, 255},
  39. {"blueviolet", 138, 43, 226},
  40. {"brown", 165, 42, 42},
  41. {"burlywood", 222, 184, 135},
  42. {"cadetblue", 95, 158, 160},
  43. {"chartreuse", 127, 255, 0},
  44. {"chocolate", 210, 105, 30},
  45. {"coral", 255, 127, 80},
  46. {"cornflowerblue", 100, 149, 237},
  47. {"cornsilk", 255, 248, 220},
  48. {"crimson", 220, 20, 60},
  49. {"cyan", 0, 255, 255},
  50. {"darkblue", 0, 0, 139},
  51. {"darkcyan", 0, 139, 139},
  52. {"darkgoldenrod", 184, 134, 11},
  53. {"darkgray", 169, 169, 169},
  54. {"darkgreen", 0, 100, 0},
  55. {"darkgrey", 169, 169, 169},
  56. {"darkkhaki", 189, 183, 107},
  57. {"darkmagenta", 139, 0, 139},
  58. {"darkolivegreen", 85, 107, 47},
  59. {"darkorange", 255, 140, 0},
  60. {"darkorchid", 153, 50, 204},
  61. {"darkred", 139, 0, 0},
  62. {"darksalmon", 233, 150, 122},
  63. {"darkseagreen", 143, 188, 143},
  64. {"darkslateblue", 72, 61, 139},
  65. {"darkslategray", 47, 79, 79},
  66. {"darkslategrey", 47, 79, 79},
  67. {"darkturquoise", 0, 206, 209},
  68. {"darkviolet", 148, 0, 211},
  69. {"deeppink", 255, 20, 147},
  70. {"deepskyblue", 0, 191, 255},
  71. {"dimgray", 105, 105, 105},
  72. {"dimgrey", 105, 105, 105},
  73. {"dodgerblue", 30, 144, 255},
  74. {"firebrick", 178, 34, 34},
  75. {"floralwhite", 255, 250, 240},
  76. {"forestgreen", 34, 139, 34},
  77. {"fuchsia", 255, 0, 255},
  78. {"gainsboro", 220, 220, 220},
  79. {"ghostwhite", 248, 248, 255},
  80. {"gold", 255, 215, 0},
  81. {"goldenrod", 218, 165, 32},
  82. {"gray", 128, 128, 128},
  83. {"grey", 128, 128, 128},
  84. {"green", 0, 128, 0},
  85. {"greenyellow", 173, 255, 47},
  86. {"honeydew", 240, 255, 240},
  87. {"hotpink", 255, 105, 180},
  88. {"indianred", 205, 92, 92},
  89. {"indigo", 75, 0, 130},
  90. {"ivory", 255, 255, 240},
  91. {"khaki", 240, 230, 140},
  92. {"lavender", 230, 230, 250},
  93. {"lavenderblush", 255, 240, 245},
  94. {"lawngreen", 124, 252, 0},
  95. {"lemonchiffon", 255, 250, 205},
  96. {"lightblue", 173, 216, 230},
  97. {"lightcoral", 240, 128, 128},
  98. {"lightcyan", 224, 255, 255},
  99. {"lightgoldenrodyellow", 250, 250, 210},
  100. {"lightgray", 211, 211, 211},
  101. {"lightgreen", 144, 238, 144},
  102. {"lightgrey", 211, 211, 211},
  103. {"lightpink", 255, 182, 193},
  104. {"lightsalmon", 255, 160, 122},
  105. {"lightseagreen", 32, 178, 170},
  106. {"lightskyblue", 135, 206, 250},
  107. {"lightslategray", 119, 136, 153},
  108. {"lightslategrey", 119, 136, 153},
  109. {"lightsteelblue", 176, 196, 222},
  110. {"lightyellow", 255, 255, 224},
  111. {"lime", 0, 255, 0},
  112. {"limegreen", 50, 205, 50},
  113. {"linen", 250, 240, 230},
  114. {"magenta", 255, 0, 255},
  115. {"maroon", 128, 0, 0},
  116. {"mediumaquamarine", 102, 205, 170},
  117. {"mediumblue", 0, 0, 205},
  118. {"mediumorchid", 186, 85, 211},
  119. {"mediumpurple", 147, 112, 219},
  120. {"mediumseagreen", 60, 179, 113},
  121. {"mediumslateblue", 123, 104, 238},
  122. {"mediumspringgreen", 0, 250, 154},
  123. {"mediumturquoise", 72, 209, 204},
  124. {"mediumvioletred", 199, 21, 133},
  125. {"midnightblue", 25, 25, 112},
  126. {"mintcream", 245, 255, 250},
  127. {"mistyrose", 255, 228, 225},
  128. {"moccasin", 255, 228, 181},
  129. {"navajowhite", 255, 222, 173},
  130. {"navy", 0, 0, 128},
  131. {"oldlace", 253, 245, 230},
  132. {"olive", 128, 128, 0},
  133. {"olivedrab", 107, 142, 35},
  134. {"orange", 255, 165, 0},
  135. {"orangered", 255, 69, 0},
  136. {"orchid", 218, 112, 214},
  137. {"palegoldenrod", 238, 232, 170},
  138. {"palegreen", 152, 251, 152},
  139. {"paleturquoise", 175, 238, 238},
  140. {"palevioletred", 219, 112, 147},
  141. {"papayawhip", 255, 239, 213},
  142. {"peachpuff", 255, 218, 185},
  143. {"peru", 205, 133, 63},
  144. {"pink", 255, 192, 203},
  145. {"plum", 221, 160, 221},
  146. {"powderblue", 176, 224, 230},
  147. {"purple", 128, 0, 128},
  148. {"red", 255, 0, 0},
  149. {"rosybrown", 188, 143, 143},
  150. {"royalblue", 65, 105, 225},
  151. {"saddlebrown", 139, 69, 19},
  152. {"salmon", 250, 128, 114},
  153. {"sandybrown", 244, 164, 96},
  154. {"seagreen", 46, 139, 87},
  155. {"seashell", 255, 245, 238},
  156. {"sienna", 160, 82, 45},
  157. {"silver", 192, 192, 192},
  158. {"skyblue", 135, 206, 235},
  159. {"slateblue", 106, 90, 205},
  160. {"slategray", 112, 128, 144},
  161. {"slategrey", 112, 128, 144},
  162. {"snow", 255, 250, 250},
  163. {"springgreen", 0, 255, 127},
  164. {"steelblue", 70, 130, 180},
  165. {"tan", 210, 180, 140},
  166. {"teal", 0, 128, 128},
  167. {"thistle", 216, 191, 216},
  168. {"tomato", 255, 99, 71},
  169. {"turquoise", 64, 224, 208},
  170. {"violet", 238, 130, 238},
  171. {"wheat", 245, 222, 179},
  172. {"white", 255, 255, 255},
  173. {"whitesmoke", 245, 245, 245},
  174. {"yellow", 255, 255, 0},
  175. {"yellowgreen", 154, 205, 50},
  176. };
  177. /*
  178. * Functions for converting human-readable colors to BBGGRR value.
  179. * Substantial optimization opportunities remain.
  180. */
  181. static int HexDigit(char hex)
  182. {
  183. const char* Digits = "0123456789ABCDEF";
  184. const char* digits = "0123456789abcdef";
  185. const char* ofs = strchr(digits, hex);
  186. if (ofs) {
  187. return ofs-digits;
  188. }
  189. ofs = strchr(Digits, hex);
  190. if (ofs) {
  191. return ofs-Digits;
  192. }
  193. return 0;
  194. }
  195. static int HexByte(const char* hex)
  196. {
  197. int b = (HexDigit(hex[0])<<4)+HexDigit(hex[1]);
  198. return b;
  199. }
  200. /*
  201. * Given an input of the form:
  202. * #<hex number for RGB value>
  203. * <decimal nummber for BBGGRR value>
  204. * <color named in CSS1 spec>
  205. * return the BBGGRR value for it.
  206. */
  207. int
  208. color_to_bbggrr(const char* opt_color)
  209. {
  210. int color_num;
  211. unsigned int i;
  212. char* ep;
  213. color_num = strtol(opt_color, &ep, 10);
  214. if (ep != opt_color) {
  215. return color_num;
  216. }
  217. if (opt_color[0] == '#') {
  218. color_num = (HexByte(opt_color+1)) + // red
  219. (HexByte(opt_color+3)<<8) + // green
  220. (HexByte(opt_color+5)<<16); // blue
  221. return color_num;
  222. }
  223. for (i = 0; i < sizeof(color_table) / sizeof(color_table[0]); i++) {
  224. if (0 == case_ignore_strcmp(opt_color, color_table[i].cn)) {
  225. return (color_table[i].b << 16) +
  226. (color_table[i].g << 8) +
  227. color_table[i].r;
  228. }
  229. }
  230. fatal("unrecognized color name %s\n", opt_color);
  231. return -1;
  232. }