Merge branch '2621_path_encoding_and_extfs'
[midnight-commander.git] / lib / tty / color-internal.c
blobbebffc848eb8a871f29430dea16ac6d748b23d82
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.
5 Written by:
6 Andrew Borodin <aborodin@vmail.ru>, 2009
7 Slava Zanko <slavazanko@gmail.com>, 2009
8 Egmont Koblinger <egmont@gmail.com>, 2010
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program 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.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
24 /** \file color-internal.c
25 * \brief Source: Internal stuff of color setup
28 #include <config.h>
30 #include <string.h> /* strcmp */
32 #include "color.h" /* colors and attributes */
33 #include "color-internal.h"
35 /*** global variables ****************************************************************************/
37 gboolean mc_tty_color_disable;
39 /*** file scope macro definitions ****************************************************************/
41 #define COLOR_INTENSITY 8
43 /*** file scope type declarations ****************************************************************/
45 typedef struct mc_tty_color_table_struct
47 const char *name;
48 int value;
49 } mc_tty_color_table_t;
51 /*** file scope variables ************************************************************************/
53 mc_tty_color_table_t const color_table[] = {
54 {"black", COLOR_BLACK},
55 {"gray", COLOR_BLACK + COLOR_INTENSITY},
56 {"red", COLOR_RED},
57 {"brightred", COLOR_RED + COLOR_INTENSITY},
58 {"green", COLOR_GREEN},
59 {"brightgreen", COLOR_GREEN + COLOR_INTENSITY},
60 {"brown", COLOR_YELLOW},
61 {"yellow", COLOR_YELLOW + COLOR_INTENSITY},
62 {"blue", COLOR_BLUE},
63 {"brightblue", COLOR_BLUE + COLOR_INTENSITY},
64 {"magenta", COLOR_MAGENTA},
65 {"brightmagenta", COLOR_MAGENTA + COLOR_INTENSITY},
66 {"cyan", COLOR_CYAN},
67 {"brightcyan", COLOR_CYAN + COLOR_INTENSITY},
68 {"lightgray", COLOR_WHITE},
69 {"white", COLOR_WHITE + COLOR_INTENSITY},
70 {"default", -1}, /* default color of the terminal */
71 /* special colors */
72 {"A_REVERSE", SPEC_A_REVERSE},
73 {"A_BOLD", SPEC_A_BOLD},
74 {"A_BOLD_REVERSE", SPEC_A_BOLD_REVERSE},
75 {"A_UNDERLINE", SPEC_A_UNDERLINE},
76 /* End of list */
77 {NULL, 0}
80 mc_tty_color_table_t const attributes_table[] = {
81 {"bold", A_BOLD},
82 {"underline", A_UNDERLINE},
83 {"reverse", A_REVERSE},
84 {"blink", A_BLINK},
85 /* End of list */
86 {NULL, 0}
89 /*** file scope functions ************************************************************************/
90 /* --------------------------------------------------------------------------------------------- */
92 static int
93 parse_256_color_name (const char *color_name)
95 int i;
96 char dummy;
97 if (sscanf (color_name, "color%d%c", &i, &dummy) == 1 && i >= 0 && i < 256)
99 return i;
101 if (sscanf (color_name, "gray%d%c", &i, &dummy) == 1 && i >= 0 && i < 24)
103 return 232 + i;
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')
110 return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0');
112 return -1;
115 /* --------------------------------------------------------------------------------------------- */
116 /*** public functions ****************************************************************************/
117 /* --------------------------------------------------------------------------------------------- */
119 const char *
120 tty_color_get_name_by_index (int idx)
122 static char **color_N_names = NULL;
123 int i;
125 /* Find the real English name of the first 16 colors, */
126 /* as well as the A_* special values. */
127 for (i = 0; color_table[i].name != NULL; i++)
128 if (idx == color_table[i].value)
129 return color_table[i].name;
130 /* Create and return the strings "color16" to "color255". */
131 if (idx >= 16 && idx < 256)
133 if (color_N_names == NULL)
135 color_N_names = g_try_malloc0 (240 * sizeof (char *));
137 if (color_N_names[idx - 16] == NULL)
139 color_N_names[idx - 16] = g_try_malloc (9);
140 sprintf (color_N_names[idx - 16], "color%d", idx);
142 return color_N_names[idx - 16];
144 return "default";
147 /* --------------------------------------------------------------------------------------------- */
150 tty_color_get_index_by_name (const char *color_name)
153 if (color_name != NULL)
155 size_t i;
156 for (i = 0; color_table[i].name != NULL; i++)
157 if (strcmp (color_name, color_table[i].name) == 0)
158 return color_table[i].value;
159 return parse_256_color_name (color_name);
161 return -1;
164 /* --------------------------------------------------------------------------------------------- */
167 tty_attr_get_bits (const char *attrs)
169 int attr_bits = 0;
170 gchar **attr_list;
171 int i, j;
173 if (attrs != NULL)
175 attr_list = g_strsplit (attrs, "+", -1);
176 for (i = 0; attr_list[i] != NULL; i++)
178 for (j = 0; attributes_table[j].name != NULL; j++)
180 if (strcmp (attr_list[i], attributes_table[j].name) == 0)
182 attr_bits |= attributes_table[j].value;
183 break;
187 g_strfreev (attr_list);
189 return attr_bits;
192 /* --------------------------------------------------------------------------------------------- */