Merge branch '4561_tar_segfault'
[midnight-commander.git] / lib / tty / color-internal.c
blob16b76efc41d2f10458da0a4533c95d5bcf0939d3
1 /*
2 Internal stuff of color setup
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2009
9 Slava Zanko <slavazanko@gmail.com>, 2009, 2013
10 Egmont Koblinger <egmont@gmail.com>, 2010
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file color-internal.c
29 * \brief Source: Internal stuff of color setup
32 #include <config.h>
34 #include <string.h> /* strcmp */
36 #include "color.h" /* colors and attributes */
37 #include "color-internal.h"
39 /*** global variables ****************************************************************************/
41 gboolean mc_tty_color_disable;
43 /*** file scope macro definitions ****************************************************************/
45 #define COLOR_INTENSITY 8
47 /*** file scope type declarations ****************************************************************/
49 typedef struct mc_tty_color_table_struct
51 const char *name;
52 int value;
53 } mc_tty_color_table_t;
55 /*** forward declarations (file scope functions) *************************************************/
57 /*** file scope variables ************************************************************************/
59 static mc_tty_color_table_t const color_table[] = {
60 {"black", COLOR_BLACK},
61 {"gray", COLOR_BLACK + COLOR_INTENSITY},
62 {"red", COLOR_RED},
63 {"brightred", COLOR_RED + COLOR_INTENSITY},
64 {"green", COLOR_GREEN},
65 {"brightgreen", COLOR_GREEN + COLOR_INTENSITY},
66 {"brown", COLOR_YELLOW},
67 {"yellow", COLOR_YELLOW + COLOR_INTENSITY},
68 {"blue", COLOR_BLUE},
69 {"brightblue", COLOR_BLUE + COLOR_INTENSITY},
70 {"magenta", COLOR_MAGENTA},
71 {"brightmagenta", COLOR_MAGENTA + COLOR_INTENSITY},
72 {"cyan", COLOR_CYAN},
73 {"brightcyan", COLOR_CYAN + COLOR_INTENSITY},
74 {"lightgray", COLOR_WHITE},
75 {"white", COLOR_WHITE + COLOR_INTENSITY},
76 {"default", -1}, /* default color of the terminal */
77 /* special colors */
78 {"A_REVERSE", SPEC_A_REVERSE},
79 {"A_BOLD", SPEC_A_BOLD},
80 {"A_BOLD_REVERSE", SPEC_A_BOLD_REVERSE},
81 {"A_UNDERLINE", SPEC_A_UNDERLINE},
82 /* End of list */
83 {NULL, 0}
86 static mc_tty_color_table_t const attributes_table[] = {
87 {"bold", A_BOLD},
88 #ifdef A_ITALIC /* available since ncurses-5.9-20130831 / slang-pre2.3.0-107 */
89 {"italic", A_ITALIC},
90 #endif /* A_ITALIC */
91 {"underline", A_UNDERLINE},
92 {"reverse", A_REVERSE},
93 {"blink", A_BLINK},
94 /* End of list */
95 {NULL, 0}
98 /* --------------------------------------------------------------------------------------------- */
99 /*** file scope functions ************************************************************************/
100 /* --------------------------------------------------------------------------------------------- */
102 static inline int
103 parse_hex_digit (char c)
105 if (c >= '0' && c <= '9')
106 return c - '0';
107 c |= 0x20;
108 if (c >= 'a' && c <= 'f')
109 return c - 'a' + 10;
110 return -1;
113 /* --------------------------------------------------------------------------------------------- */
115 static int
116 parse_256_or_true_color_name (const char *color_name)
118 int i;
119 char dummy;
121 /* cppcheck-suppress invalidscanf */
122 if (sscanf (color_name, "color%d%c", &i, &dummy) == 1 && i >= 0 && i < 256)
124 return i;
126 /* cppcheck-suppress invalidscanf */
127 if (sscanf (color_name, "gray%d%c", &i, &dummy) == 1 && i >= 0 && i < 24)
129 return 232 + i;
131 if (strncmp (color_name, "rgb", 3) == 0 &&
132 color_name[3] >= '0' && color_name[3] < '6' &&
133 color_name[4] >= '0' && color_name[4] < '6' &&
134 color_name[5] >= '0' && color_name[5] < '6' && color_name[6] == '\0')
136 return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0');
138 if (color_name[0] == '#')
140 int len;
142 color_name++;
143 len = (int) strlen (color_name);
144 if (len == 3 || len == 6)
146 int h[6];
148 for (i = 0; i < len; i++)
150 h[i] = parse_hex_digit (color_name[i]);
151 if (h[i] == -1)
152 return -1;
155 if (i == 3)
156 i = (h[0] << 20) | (h[0] << 16) | (h[1] << 12) | (h[1] << 8) | (h[2] << 4) | h[2];
157 else
158 i = (h[0] << 20) | (h[1] << 16) | (h[2] << 12) | (h[3] << 8) | (h[4] << 4) | h[5];
159 return (1 << 24) | i;
163 return -1;
166 /* --------------------------------------------------------------------------------------------- */
167 /*** public functions ****************************************************************************/
168 /* --------------------------------------------------------------------------------------------- */
170 const char *
171 tty_color_get_name_by_index (int idx)
173 int i;
175 /* Find the real English name of the first 16 colors, */
176 /* as well as the A_* special values. */
177 for (i = 0; color_table[i].name != NULL; i++)
178 if (idx == color_table[i].value)
179 return color_table[i].name;
181 /* Create and return the strings in "colorNNN" or "#rrggbb" format. */
182 if ((idx >= 16 && idx < 256) || (idx & (1 << 24)) != 0)
184 char name[9];
186 if (idx < 256)
187 g_snprintf (name, sizeof (name), "color%d", idx);
188 else
189 g_snprintf (name, sizeof (name), "#%06X", (unsigned int) idx & 0xFFFFFF);
190 return g_intern_string (name);
192 return "default";
195 /* --------------------------------------------------------------------------------------------- */
198 tty_color_get_index_by_name (const char *color_name)
200 if (color_name != NULL)
202 size_t i;
204 for (i = 0; color_table[i].name != NULL; i++)
205 if (strcmp (color_name, color_table[i].name) == 0)
206 return color_table[i].value;
207 return parse_256_or_true_color_name (color_name);
209 return -1;
212 /* --------------------------------------------------------------------------------------------- */
215 tty_attr_get_bits (const char *attrs)
217 int attr_bits = 0;
219 if (attrs != NULL)
221 gchar **attr_list;
222 int i;
224 attr_list = g_strsplit (attrs, "+", -1);
226 for (i = 0; attr_list[i] != NULL; i++)
228 int j;
230 for (j = 0; attributes_table[j].name != NULL; j++)
232 if (strcmp (attr_list[i], attributes_table[j].name) == 0)
234 attr_bits |= attributes_table[j].value;
235 break;
239 g_strfreev (attr_list);
241 return attr_bits;
244 /* --------------------------------------------------------------------------------------------- */