color-internal.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Internal stuff of color setup
  3. Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2007, 2008, 2009, 2010, 2011
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Andrew Borodin <aborodin@vmail.ru>, 2009
  8. Slava Zanko <slavazanko@gmail.com>, 2009
  9. Egmont Koblinger <egmont@gmail.com>, 2010
  10. This file is part of the Midnight Commander.
  11. The Midnight Commander is free software: you can redistribute it
  12. and/or modify it under the terms of the GNU General Public License as
  13. published by the Free Software Foundation, either version 3 of the License,
  14. or (at your option) any later version.
  15. The Midnight Commander is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /** \file color-internal.c
  23. * \brief Source: Internal stuff of color setup
  24. */
  25. #include <config.h>
  26. #include <string.h> /* strcmp */
  27. #include "color.h" /* colors and attributes */
  28. #include "color-internal.h"
  29. /*** global variables ****************************************************************************/
  30. gboolean mc_tty_color_disable;
  31. /*** file scope macro definitions ****************************************************************/
  32. #define COLOR_INTENSITY 8
  33. /*** file scope type declarations ****************************************************************/
  34. typedef struct mc_tty_color_table_struct
  35. {
  36. const char *name;
  37. int value;
  38. } mc_tty_color_table_t;
  39. /*** file scope variables ************************************************************************/
  40. 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. mc_tty_color_table_t const attributes_table[] = {
  67. {"bold", A_BOLD},
  68. {"underline", A_UNDERLINE},
  69. {"reverse", A_REVERSE},
  70. {"blink", A_BLINK},
  71. /* End of list */
  72. {NULL, 0}
  73. };
  74. /*** file scope functions ************************************************************************/
  75. /* --------------------------------------------------------------------------------------------- */
  76. static int
  77. parse_256_color_name (const char *color_name)
  78. {
  79. int i;
  80. char dummy;
  81. if (sscanf (color_name, "color%d%c", &i, &dummy) == 1 && i >= 0 && i < 256)
  82. {
  83. return i;
  84. }
  85. if (sscanf (color_name, "gray%d%c", &i, &dummy) == 1 && i >= 0 && i < 24)
  86. {
  87. return 232 + i;
  88. }
  89. if (strncmp (color_name, "rgb", 3) == 0 &&
  90. color_name[3] >= '0' && color_name[3] < '6' &&
  91. color_name[4] >= '0' && color_name[4] < '6' &&
  92. color_name[5] >= '0' && color_name[5] < '6' && color_name[6] == '\0')
  93. {
  94. return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0');
  95. }
  96. return -1;
  97. }
  98. /* --------------------------------------------------------------------------------------------- */
  99. /*** public functions ****************************************************************************/
  100. /* --------------------------------------------------------------------------------------------- */
  101. const char *
  102. tty_color_get_name_by_index (int idx)
  103. {
  104. static char **color_N_names = NULL;
  105. int i;
  106. /* Find the real English name of the first 16 colors, */
  107. /* as well as the A_* special values. */
  108. for (i = 0; color_table[i].name != NULL; i++)
  109. if (idx == color_table[i].value)
  110. return color_table[i].name;
  111. /* Create and return the strings "color16" to "color255". */
  112. if (idx >= 16 && idx < 256)
  113. {
  114. if (color_N_names == NULL)
  115. {
  116. color_N_names = g_try_malloc0 (240 * sizeof (char *));
  117. }
  118. if (color_N_names[idx - 16] == NULL)
  119. {
  120. color_N_names[idx - 16] = g_try_malloc (9);
  121. sprintf (color_N_names[idx - 16], "color%d", idx);
  122. }
  123. return color_N_names[idx - 16];
  124. }
  125. return "default";
  126. }
  127. /* --------------------------------------------------------------------------------------------- */
  128. int
  129. tty_color_get_index_by_name (const char *color_name)
  130. {
  131. if (color_name != NULL)
  132. {
  133. size_t i;
  134. for (i = 0; color_table[i].name != NULL; i++)
  135. if (strcmp (color_name, color_table[i].name) == 0)
  136. return color_table[i].value;
  137. return parse_256_color_name (color_name);
  138. }
  139. return -1;
  140. }
  141. /* --------------------------------------------------------------------------------------------- */
  142. int
  143. tty_attr_get_bits (const char *attrs)
  144. {
  145. int attr_bits = 0;
  146. gchar **attr_list;
  147. int i, j;
  148. if (attrs != NULL)
  149. {
  150. attr_list = g_strsplit (attrs, "+", -1);
  151. for (i = 0; attr_list[i] != NULL; i++)
  152. {
  153. for (j = 0; attributes_table[j].name != NULL; j++)
  154. {
  155. if (strcmp (attr_list[i], attributes_table[j].name) == 0)
  156. {
  157. attr_bits |= attributes_table[j].value;
  158. break;
  159. }
  160. }
  161. }
  162. g_strfreev (attr_list);
  163. }
  164. return attr_bits;
  165. }
  166. /* --------------------------------------------------------------------------------------------- */