color-internal.c 6.1 KB

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