color-internal.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. Internal stuff of color setup
  3. Copyright (C) 1994-2017
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2009
  7. Slava Zanko <slavazanko@gmail.com>, 2009, 2013
  8. Egmont Koblinger <egmont@gmail.com>, 2010
  9. This file is part of the Midnight Commander.
  10. The Midnight Commander is free software: you can redistribute it
  11. and/or modify it under the terms of the GNU General Public License as
  12. published by the Free Software Foundation, either version 3 of the License,
  13. or (at your option) any later version.
  14. The Midnight Commander is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /** \file color-internal.c
  22. * \brief Source: Internal stuff of color setup
  23. */
  24. #include <config.h>
  25. #include <string.h> /* strcmp */
  26. #include "color.h" /* colors and attributes */
  27. #include "color-internal.h"
  28. /*** global variables ****************************************************************************/
  29. gboolean mc_tty_color_disable;
  30. /*** file scope macro definitions ****************************************************************/
  31. #define COLOR_INTENSITY 8
  32. /*** file scope type declarations ****************************************************************/
  33. typedef struct mc_tty_color_table_struct
  34. {
  35. const char *name;
  36. int value;
  37. } mc_tty_color_table_t;
  38. /*** file scope variables ************************************************************************/
  39. static mc_tty_color_table_t const color_table[] = {
  40. {"black", COLOR_BLACK},
  41. {"gray", COLOR_BLACK + COLOR_INTENSITY},
  42. {"red", COLOR_RED},
  43. {"brightred", COLOR_RED + COLOR_INTENSITY},
  44. {"green", COLOR_GREEN},
  45. {"brightgreen", COLOR_GREEN + COLOR_INTENSITY},
  46. {"brown", COLOR_YELLOW},
  47. {"yellow", COLOR_YELLOW + COLOR_INTENSITY},
  48. {"blue", COLOR_BLUE},
  49. {"brightblue", COLOR_BLUE + COLOR_INTENSITY},
  50. {"magenta", COLOR_MAGENTA},
  51. {"brightmagenta", COLOR_MAGENTA + COLOR_INTENSITY},
  52. {"cyan", COLOR_CYAN},
  53. {"brightcyan", COLOR_CYAN + COLOR_INTENSITY},
  54. {"lightgray", COLOR_WHITE},
  55. {"white", COLOR_WHITE + COLOR_INTENSITY},
  56. {"default", -1}, /* default color of the terminal */
  57. /* special colors */
  58. {"A_REVERSE", SPEC_A_REVERSE},
  59. {"A_BOLD", SPEC_A_BOLD},
  60. {"A_BOLD_REVERSE", SPEC_A_BOLD_REVERSE},
  61. {"A_UNDERLINE", SPEC_A_UNDERLINE},
  62. /* End of list */
  63. {NULL, 0}
  64. };
  65. static mc_tty_color_table_t const attributes_table[] = {
  66. {"bold", A_BOLD},
  67. #ifdef A_ITALIC /* available since ncurses-5.9-20130831 / slang-pre2.3.0-107 */
  68. {"italic", A_ITALIC},
  69. #endif /* A_ITALIC */
  70. {"underline", A_UNDERLINE},
  71. {"reverse", A_REVERSE},
  72. {"blink", A_BLINK},
  73. /* End of list */
  74. {NULL, 0}
  75. };
  76. /* --------------------------------------------------------------------------------------------- */
  77. /*** file scope functions ************************************************************************/
  78. /* --------------------------------------------------------------------------------------------- */
  79. static inline int
  80. parse_hex_digit (char c)
  81. {
  82. if (c >= '0' && c <= '9')
  83. return c - '0';
  84. c |= 0x20;
  85. if (c >= 'a' && c <= 'f')
  86. return c - 'a' + 10;
  87. return -1;
  88. }
  89. /* --------------------------------------------------------------------------------------------- */
  90. static int
  91. parse_256_or_true_color_name (const char *color_name)
  92. {
  93. int i;
  94. char dummy;
  95. /* cppcheck-suppress invalidscanf */
  96. if (sscanf (color_name, "color%d%c", &i, &dummy) == 1 && i >= 0 && i < 256)
  97. {
  98. return i;
  99. }
  100. /* cppcheck-suppress invalidscanf */
  101. if (sscanf (color_name, "gray%d%c", &i, &dummy) == 1 && i >= 0 && i < 24)
  102. {
  103. return 232 + i;
  104. }
  105. if (strncmp (color_name, "rgb", 3) == 0 &&
  106. color_name[3] >= '0' && color_name[3] < '6' &&
  107. color_name[4] >= '0' && color_name[4] < '6' &&
  108. color_name[5] >= '0' && color_name[5] < '6' && color_name[6] == '\0')
  109. {
  110. return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0');
  111. }
  112. if (color_name[0] == '#')
  113. {
  114. int len;
  115. int h[6];
  116. color_name++;
  117. len = (int) strlen (color_name);
  118. if (len != 3 && len != 6)
  119. return -1;
  120. for (i = 0; i < len; i++)
  121. {
  122. h[i] = parse_hex_digit (color_name[i]);
  123. if (h[i] == -1)
  124. return -1;
  125. }
  126. if (i == 3)
  127. i = (h[0] << 20) | (h[0] << 16) | (h[1] << 12) | (h[1] << 8) | (h[2] << 4) | h[2];
  128. else
  129. i = (h[0] << 20) | (h[1] << 16) | (h[2] << 12) | (h[3] << 8) | (h[4] << 4) | h[5];
  130. return (1 << 24) | i;
  131. }
  132. return -1;
  133. }
  134. /* --------------------------------------------------------------------------------------------- */
  135. /*** public functions ****************************************************************************/
  136. /* --------------------------------------------------------------------------------------------- */
  137. const char *
  138. tty_color_get_name_by_index (int idx)
  139. {
  140. int i;
  141. /* Find the real English name of the first 16 colors, */
  142. /* as well as the A_* special values. */
  143. for (i = 0; color_table[i].name != NULL; i++)
  144. if (idx == color_table[i].value)
  145. return color_table[i].name;
  146. /* Create and return the strings in "colorNNN" or "#rrggbb" format. */
  147. if ((idx >= 16 && idx < 256) || (idx & (1 << 24)) != 0)
  148. {
  149. char name[9];
  150. if (idx < 256)
  151. sprintf (name, "color%d", idx);
  152. else
  153. sprintf (name, "#%06X", (unsigned int) idx & 0xFFFFFF);
  154. return g_intern_string (name);
  155. }
  156. return "default";
  157. }
  158. /* --------------------------------------------------------------------------------------------- */
  159. int
  160. tty_color_get_index_by_name (const char *color_name)
  161. {
  162. if (color_name != NULL)
  163. {
  164. size_t i;
  165. for (i = 0; color_table[i].name != NULL; i++)
  166. if (strcmp (color_name, color_table[i].name) == 0)
  167. return color_table[i].value;
  168. return parse_256_or_true_color_name (color_name);
  169. }
  170. return -1;
  171. }
  172. /* --------------------------------------------------------------------------------------------- */
  173. int
  174. tty_attr_get_bits (const char *attrs)
  175. {
  176. int attr_bits = 0;
  177. if (attrs != NULL)
  178. {
  179. gchar **attr_list;
  180. int i;
  181. attr_list = g_strsplit (attrs, "+", -1);
  182. for (i = 0; attr_list[i] != NULL; i++)
  183. {
  184. int j;
  185. for (j = 0; attributes_table[j].name != NULL; j++)
  186. {
  187. if (strcmp (attr_list[i], attributes_table[j].name) == 0)
  188. {
  189. attr_bits |= attributes_table[j].value;
  190. break;
  191. }
  192. }
  193. }
  194. g_strfreev (attr_list);
  195. }
  196. return attr_bits;
  197. }
  198. /* --------------------------------------------------------------------------------------------- */