Merge branch '2206_jump_line'
[midnight-commander.git] / lib / tty / color-internal.c
blob70c965a76bbc9fc6a8ceb7001ecfe3512e86bdc6
1 /*
2 Internal stuff of color setup
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2008, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Andrew Borodin <aborodin@vmail.ru>, 2009
10 Slava Zanko <slavazanko@gmail.com>, 2009
11 Egmont Koblinger <egmont@gmail.com>, 2010
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file color-internal.c
30 * \brief Source: Internal stuff of color setup
33 #include <config.h>
35 #include <string.h> /* strcmp */
37 #include "color.h" /* colors and attributes */
38 #include "color-internal.h"
40 /*** global variables ****************************************************************************/
42 gboolean mc_tty_color_disable;
44 /*** file scope macro definitions ****************************************************************/
46 #define COLOR_INTENSITY 8
48 /*** file scope type declarations ****************************************************************/
50 typedef struct mc_tty_color_table_struct
52 const char *name;
53 int value;
54 } mc_tty_color_table_t;
56 /*** file scope variables ************************************************************************/
58 mc_tty_color_table_t const color_table[] = {
59 {"black", COLOR_BLACK},
60 {"gray", COLOR_BLACK + COLOR_INTENSITY},
61 {"red", COLOR_RED},
62 {"brightred", COLOR_RED + COLOR_INTENSITY},
63 {"green", COLOR_GREEN},
64 {"brightgreen", COLOR_GREEN + COLOR_INTENSITY},
65 {"brown", COLOR_YELLOW},
66 {"yellow", COLOR_YELLOW + COLOR_INTENSITY},
67 {"blue", COLOR_BLUE},
68 {"brightblue", COLOR_BLUE + COLOR_INTENSITY},
69 {"magenta", COLOR_MAGENTA},
70 {"brightmagenta", COLOR_MAGENTA + COLOR_INTENSITY},
71 {"cyan", COLOR_CYAN},
72 {"brightcyan", COLOR_CYAN + COLOR_INTENSITY},
73 {"lightgray", COLOR_WHITE},
74 {"white", COLOR_WHITE + COLOR_INTENSITY},
75 {"default", -1}, /* default color of the terminal */
76 /* special colors */
77 {"A_REVERSE", SPEC_A_REVERSE},
78 {"A_BOLD", SPEC_A_BOLD},
79 {"A_BOLD_REVERSE", SPEC_A_BOLD_REVERSE},
80 {"A_UNDERLINE", SPEC_A_UNDERLINE},
81 /* End of list */
82 {NULL, 0}
85 mc_tty_color_table_t const attributes_table[] = {
86 {"bold", A_BOLD},
87 {"underline", A_UNDERLINE},
88 {"reverse", A_REVERSE},
89 {"blink", A_BLINK},
90 /* End of list */
91 {NULL, 0}
94 /*** file scope functions ************************************************************************/
95 /* --------------------------------------------------------------------------------------------- */
97 static int
98 parse_256_color_name (const char *color_name)
100 int i;
101 char dummy;
102 if (sscanf (color_name, "color%d%c", &i, &dummy) == 1 && i >= 0 && i < 256)
104 return i;
106 if (sscanf (color_name, "gray%d%c", &i, &dummy) == 1 && i >= 0 && i < 24)
108 return 232 + i;
110 if (strncmp (color_name, "rgb", 3) == 0 &&
111 color_name[3] >= '0' && color_name[3] < '6' &&
112 color_name[4] >= '0' && color_name[4] < '6' &&
113 color_name[5] >= '0' && color_name[5] < '6' && color_name[6] == '\0')
115 return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0');
117 return -1;
120 /* --------------------------------------------------------------------------------------------- */
121 /*** public functions ****************************************************************************/
122 /* --------------------------------------------------------------------------------------------- */
124 const char *
125 tty_color_get_name_by_index (int idx)
127 static char **color_N_names = NULL;
128 int i;
130 /* Find the real English name of the first 16 colors, */
131 /* as well as the A_* special values. */
132 for (i = 0; color_table[i].name != NULL; i++)
133 if (idx == color_table[i].value)
134 return color_table[i].name;
135 /* Create and return the strings "color16" to "color255". */
136 if (idx >= 16 && idx < 256)
138 if (color_N_names == NULL)
140 color_N_names = g_try_malloc0 (240 * sizeof (char *));
142 if (color_N_names[idx - 16] == NULL)
144 color_N_names[idx - 16] = g_try_malloc (9);
145 sprintf (color_N_names[idx - 16], "color%d", idx);
147 return color_N_names[idx - 16];
149 return "default";
152 /* --------------------------------------------------------------------------------------------- */
155 tty_color_get_index_by_name (const char *color_name)
158 if (color_name != NULL)
160 size_t i;
161 for (i = 0; color_table[i].name != NULL; i++)
162 if (strcmp (color_name, color_table[i].name) == 0)
163 return color_table[i].value;
164 return parse_256_color_name (color_name);
166 return -1;
169 /* --------------------------------------------------------------------------------------------- */
172 tty_attr_get_bits (const char *attrs)
174 int attr_bits = 0;
175 gchar **attr_list;
176 int i, j;
178 if (attrs != NULL)
180 attr_list = g_strsplit (attrs, "+", -1);
181 for (i = 0; attr_list[i] != NULL; i++)
183 for (j = 0; attributes_table[j].name != NULL; j++)
185 if (strcmp (attr_list[i], attributes_table[j].name) == 0)
187 attr_bits |= attributes_table[j].value;
188 break;
192 g_strfreev (attr_list);
194 return attr_bits;
197 /* --------------------------------------------------------------------------------------------- */