color-internal.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. /*** file scope type declarations ****************************************************************/
  30. typedef struct mc_tty_color_table_struct
  31. {
  32. const char *name;
  33. int value;
  34. } mc_tty_color_table_t;
  35. /*** file scope variables ************************************************************************/
  36. mc_tty_color_table_t const color_table[] = {
  37. {"black", COLOR_BLACK},
  38. {"gray", COLOR_BLACK + 8},
  39. {"red", COLOR_RED},
  40. {"brightred", COLOR_RED + 8},
  41. {"green", COLOR_GREEN},
  42. {"brightgreen", COLOR_GREEN + 8},
  43. {"brown", COLOR_YELLOW},
  44. {"yellow", COLOR_YELLOW + 8},
  45. {"blue", COLOR_BLUE},
  46. {"brightblue", COLOR_BLUE + 8},
  47. {"magenta", COLOR_MAGENTA},
  48. {"brightmagenta", COLOR_MAGENTA + 8},
  49. {"cyan", COLOR_CYAN},
  50. {"brightcyan", COLOR_CYAN + 8},
  51. {"lightgray", COLOR_WHITE},
  52. {"white", COLOR_WHITE + 8},
  53. {"default", -1}, /* default color of the terminal */
  54. /* special colors */
  55. {"A_REVERSE", SPEC_A_REVERSE},
  56. {"A_BOLD", SPEC_A_BOLD},
  57. {"A_BOLD_REVERSE", SPEC_A_BOLD_REVERSE},
  58. {"A_UNDERLINE", SPEC_A_UNDERLINE},
  59. /* End of list */
  60. {NULL, 0}
  61. };
  62. mc_tty_color_table_t const attributes_table[] = {
  63. {"bold", A_BOLD},
  64. {"underline", A_UNDERLINE},
  65. {"reverse", A_REVERSE},
  66. {"blink", A_BLINK},
  67. /* End of list */
  68. {NULL, 0}
  69. };
  70. /*** file scope functions ************************************************************************/
  71. /* --------------------------------------------------------------------------------------------- */
  72. static int
  73. parse_256_color_name (const char *color_name)
  74. {
  75. int i;
  76. char dummy;
  77. if (sscanf (color_name, "color%d%c", &i, &dummy) == 1 && i >= 0 && i < 256)
  78. {
  79. return i;
  80. }
  81. if (sscanf (color_name, "gray%d%c", &i, &dummy) == 1 && i >= 0 && i < 24)
  82. {
  83. return 232 + i;
  84. }
  85. if (strncmp (color_name, "rgb", 3) == 0 &&
  86. color_name[3] >= '0' && color_name[3] < '6' &&
  87. color_name[4] >= '0' && color_name[4] < '6' &&
  88. color_name[5] >= '0' && color_name[5] < '6' && color_name[6] == '\0')
  89. {
  90. return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0');
  91. }
  92. return -1;
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. /*** public functions ****************************************************************************/
  96. /* --------------------------------------------------------------------------------------------- */
  97. const char *
  98. tty_color_get_name_by_index (int idx)
  99. {
  100. static char **color_N_names = NULL;
  101. int i;
  102. /* Find the real English name of the first 16 colors, */
  103. /* as well as the A_* special values. */
  104. for (i = 0; color_table[i].name != NULL; i++)
  105. if (idx == color_table[i].value)
  106. return color_table[i].name;
  107. /* Create and return the strings "color16" to "color255". */
  108. if (idx >= 16 && idx < 256)
  109. {
  110. if (color_N_names == NULL)
  111. {
  112. color_N_names = g_try_malloc0 (240 * sizeof (char *));
  113. }
  114. if (color_N_names[idx - 16] == NULL)
  115. {
  116. color_N_names[idx - 16] = g_try_malloc (9);
  117. sprintf (color_N_names[idx - 16], "color%d", idx);
  118. }
  119. return color_N_names[idx - 16];
  120. }
  121. return "default";
  122. }
  123. /* --------------------------------------------------------------------------------------------- */
  124. int
  125. tty_color_get_index_by_name (const char *color_name)
  126. {
  127. if (color_name != NULL)
  128. {
  129. size_t i;
  130. for (i = 0; color_table[i].name != NULL; i++)
  131. if (strcmp (color_name, color_table[i].name) == 0)
  132. return color_table[i].value;
  133. return parse_256_color_name (color_name);
  134. }
  135. return -1;
  136. }
  137. /* --------------------------------------------------------------------------------------------- */
  138. int
  139. tty_attr_get_bits (const char *attrs)
  140. {
  141. int attr_bits = 0;
  142. gchar **attr_list;
  143. int i, j;
  144. if (attrs != NULL)
  145. {
  146. attr_list = g_strsplit (attrs, "+", -1);
  147. for (i = 0; attr_list[i] != NULL; i++)
  148. {
  149. for (j = 0; attributes_table[j].name != NULL; j++)
  150. {
  151. if (strcmp (attr_list[i], attributes_table[j].name) == 0)
  152. {
  153. attr_bits |= attributes_table[j].value;
  154. break;
  155. }
  156. }
  157. }
  158. g_strfreev (attr_list);
  159. }
  160. return attr_bits;
  161. }
  162. /* --------------------------------------------------------------------------------------------- */