color-internal.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. Internal stuff of color setup
  3. Copyright (C) 1994-2024
  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. /*** forward declarations (file scope functions) *************************************************/
  39. /*** file scope variables ************************************************************************/
  40. static mc_tty_color_table_t const color_table[] = {
  41. {"black", COLOR_BLACK},
  42. {"gray", COLOR_BLACK + COLOR_INTENSITY},
  43. {"red", COLOR_RED},
  44. {"brightred", COLOR_RED + COLOR_INTENSITY},
  45. {"green", COLOR_GREEN},
  46. {"brightgreen", COLOR_GREEN + COLOR_INTENSITY},
  47. {"brown", COLOR_YELLOW},
  48. {"yellow", COLOR_YELLOW + COLOR_INTENSITY},
  49. {"blue", COLOR_BLUE},
  50. {"brightblue", COLOR_BLUE + COLOR_INTENSITY},
  51. {"magenta", COLOR_MAGENTA},
  52. {"brightmagenta", COLOR_MAGENTA + COLOR_INTENSITY},
  53. {"cyan", COLOR_CYAN},
  54. {"brightcyan", COLOR_CYAN + COLOR_INTENSITY},
  55. {"lightgray", COLOR_WHITE},
  56. {"white", COLOR_WHITE + COLOR_INTENSITY},
  57. {"default", -1}, /* default color of the terminal */
  58. /* special colors */
  59. {"A_REVERSE", SPEC_A_REVERSE},
  60. {"A_BOLD", SPEC_A_BOLD},
  61. {"A_BOLD_REVERSE", SPEC_A_BOLD_REVERSE},
  62. {"A_UNDERLINE", SPEC_A_UNDERLINE},
  63. /* End of list */
  64. {NULL, 0}
  65. };
  66. static mc_tty_color_table_t const attributes_table[] = {
  67. {"bold", A_BOLD},
  68. #ifdef A_ITALIC /* available since ncurses-5.9-20130831 / slang-pre2.3.0-107 */
  69. {"italic", A_ITALIC},
  70. #endif /* A_ITALIC */
  71. {"underline", A_UNDERLINE},
  72. {"reverse", A_REVERSE},
  73. {"blink", A_BLINK},
  74. /* End of list */
  75. {NULL, 0}
  76. };
  77. /* --------------------------------------------------------------------------------------------- */
  78. /*** file scope functions ************************************************************************/
  79. /* --------------------------------------------------------------------------------------------- */
  80. static inline int
  81. parse_hex_digit (char c)
  82. {
  83. if (c >= '0' && c <= '9')
  84. return c - '0';
  85. c |= 0x20;
  86. if (c >= 'a' && c <= 'f')
  87. return c - 'a' + 10;
  88. return -1;
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. static int
  92. parse_256_or_true_color_name (const char *color_name)
  93. {
  94. int i;
  95. char dummy;
  96. /* cppcheck-suppress invalidscanf */
  97. if (sscanf (color_name, "color%d%c", &i, &dummy) == 1 && i >= 0 && i < 256)
  98. {
  99. return i;
  100. }
  101. /* cppcheck-suppress invalidscanf */
  102. if (sscanf (color_name, "gray%d%c", &i, &dummy) == 1 && i >= 0 && i < 24)
  103. {
  104. return 232 + i;
  105. }
  106. if (strncmp (color_name, "rgb", 3) == 0 &&
  107. color_name[3] >= '0' && color_name[3] < '6' &&
  108. color_name[4] >= '0' && color_name[4] < '6' &&
  109. color_name[5] >= '0' && color_name[5] < '6' && color_name[6] == '\0')
  110. {
  111. return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0');
  112. }
  113. if (color_name[0] == '#')
  114. {
  115. int len;
  116. color_name++;
  117. len = (int) strlen (color_name);
  118. if (len == 3 || len == 6)
  119. {
  120. int h[6];
  121. for (i = 0; i < len; i++)
  122. {
  123. h[i] = parse_hex_digit (color_name[i]);
  124. if (h[i] == -1)
  125. return -1;
  126. }
  127. if (i == 3)
  128. i = (h[0] << 20) | (h[0] << 16) | (h[1] << 12) | (h[1] << 8) | (h[2] << 4) | h[2];
  129. else
  130. i = (h[0] << 20) | (h[1] << 16) | (h[2] << 12) | (h[3] << 8) | (h[4] << 4) | h[5];
  131. return (1 << 24) | i;
  132. }
  133. }
  134. return -1;
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. /*** public functions ****************************************************************************/
  138. /* --------------------------------------------------------------------------------------------- */
  139. const char *
  140. tty_color_get_name_by_index (int idx)
  141. {
  142. int i;
  143. /* Find the real English name of the first 16 colors, */
  144. /* as well as the A_* special values. */
  145. for (i = 0; color_table[i].name != NULL; i++)
  146. if (idx == color_table[i].value)
  147. return color_table[i].name;
  148. /* Create and return the strings in "colorNNN" or "#rrggbb" format. */
  149. if ((idx >= 16 && idx < 256) || (idx & (1 << 24)) != 0)
  150. {
  151. char name[9];
  152. if (idx < 256)
  153. g_snprintf (name, sizeof (name), "color%d", idx);
  154. else
  155. g_snprintf (name, sizeof (name), "#%06X", (unsigned int) idx & 0xFFFFFF);
  156. return g_intern_string (name);
  157. }
  158. return "default";
  159. }
  160. /* --------------------------------------------------------------------------------------------- */
  161. int
  162. tty_color_get_index_by_name (const char *color_name)
  163. {
  164. if (color_name != NULL)
  165. {
  166. size_t i;
  167. for (i = 0; color_table[i].name != NULL; i++)
  168. if (strcmp (color_name, color_table[i].name) == 0)
  169. return color_table[i].value;
  170. return parse_256_or_true_color_name (color_name);
  171. }
  172. return -1;
  173. }
  174. /* --------------------------------------------------------------------------------------------- */
  175. int
  176. tty_attr_get_bits (const char *attrs)
  177. {
  178. int attr_bits = 0;
  179. if (attrs != NULL)
  180. {
  181. gchar **attr_list;
  182. int i;
  183. attr_list = g_strsplit (attrs, "+", -1);
  184. for (i = 0; attr_list[i] != NULL; i++)
  185. {
  186. int j;
  187. for (j = 0; attributes_table[j].name != NULL; j++)
  188. {
  189. if (strcmp (attr_list[i], attributes_table[j].name) == 0)
  190. {
  191. attr_bits |= attributes_table[j].value;
  192. break;
  193. }
  194. }
  195. }
  196. g_strfreev (attr_list);
  197. }
  198. return attr_bits;
  199. }
  200. /* --------------------------------------------------------------------------------------------- */