2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Internal stuff of color setup
|
|
|
|
|
2021-01-01 14:21:28 +03:00
|
|
|
Copyright (C) 1994-2021
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2009-06-12 14:55:06 +04:00
|
|
|
Written by:
|
2010-12-29 12:58:10 +03:00
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2009
|
2013-10-15 10:34:04 +04:00
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2009, 2013
|
2010-12-29 12:58:10 +03:00
|
|
|
Egmont Koblinger <egmont@gmail.com>, 2010
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
|
|
|
The Midnight Commander is free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
2009-06-12 14:55:06 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
2009-06-12 14:55:06 +04:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2009-05-10 19:01:15 +04:00
|
|
|
|
|
|
|
/** \file color-internal.c
|
|
|
|
* \brief Source: Internal stuff of color setup
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
#include <string.h> /* strcmp */
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
#include "color.h" /* colors and attributes */
|
2010-01-07 02:57:27 +03:00
|
|
|
#include "color-internal.h"
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
2009-09-09 18:56:52 +04:00
|
|
|
gboolean mc_tty_color_disable;
|
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
2011-01-11 17:11:08 +03:00
|
|
|
#define COLOR_INTENSITY 8
|
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
typedef struct mc_tty_color_table_struct
|
|
|
|
{
|
2009-09-04 18:22:49 +04:00
|
|
|
const char *name;
|
2009-09-17 02:07:04 +04:00
|
|
|
int value;
|
2009-09-04 18:22:49 +04:00
|
|
|
} mc_tty_color_table_t;
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
2015-05-10 17:55:54 +03:00
|
|
|
static mc_tty_color_table_t const color_table[] = {
|
2010-11-08 13:21:45 +03:00
|
|
|
{"black", COLOR_BLACK},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"gray", COLOR_BLACK + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"red", COLOR_RED},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"brightred", COLOR_RED + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"green", COLOR_GREEN},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"brightgreen", COLOR_GREEN + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"brown", COLOR_YELLOW},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"yellow", COLOR_YELLOW + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"blue", COLOR_BLUE},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"brightblue", COLOR_BLUE + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"magenta", COLOR_MAGENTA},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"brightmagenta", COLOR_MAGENTA + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"cyan", COLOR_CYAN},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"brightcyan", COLOR_CYAN + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"lightgray", COLOR_WHITE},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"white", COLOR_WHITE + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"default", -1}, /* default color of the terminal */
|
2009-09-09 18:56:52 +04:00
|
|
|
/* special colors */
|
2010-11-08 13:21:45 +03:00
|
|
|
{"A_REVERSE", SPEC_A_REVERSE},
|
|
|
|
{"A_BOLD", SPEC_A_BOLD},
|
|
|
|
{"A_BOLD_REVERSE", SPEC_A_BOLD_REVERSE},
|
|
|
|
{"A_UNDERLINE", SPEC_A_UNDERLINE},
|
2009-09-09 18:56:52 +04:00
|
|
|
/* End of list */
|
2010-11-08 13:21:45 +03:00
|
|
|
{NULL, 0}
|
2009-05-10 19:01:15 +04:00
|
|
|
};
|
|
|
|
|
2015-05-10 17:55:54 +03:00
|
|
|
static mc_tty_color_table_t const attributes_table[] = {
|
2010-12-29 12:58:10 +03:00
|
|
|
{"bold", A_BOLD},
|
2015-02-17 15:22:53 +03:00
|
|
|
#ifdef A_ITALIC /* available since ncurses-5.9-20130831 / slang-pre2.3.0-107 */
|
|
|
|
{"italic", A_ITALIC},
|
|
|
|
#endif /* A_ITALIC */
|
2010-12-29 12:58:10 +03:00
|
|
|
{"underline", A_UNDERLINE},
|
|
|
|
{"reverse", A_REVERSE},
|
|
|
|
{"blink", A_BLINK},
|
|
|
|
/* End of list */
|
|
|
|
{NULL, 0}
|
|
|
|
};
|
|
|
|
|
2016-11-02 14:07:33 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2016-11-02 14:07:33 +03:00
|
|
|
static inline int
|
|
|
|
parse_hex_digit (char c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return c - '0';
|
|
|
|
c |= 0x20;
|
|
|
|
if (c >= 'a' && c <= 'f')
|
|
|
|
return c - 'a' + 10;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
static int
|
2016-11-02 14:07:33 +03:00
|
|
|
parse_256_or_true_color_name (const char *color_name)
|
2010-12-29 12:58:10 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char dummy;
|
2013-10-10 16:21:26 +04:00
|
|
|
|
|
|
|
/* cppcheck-suppress invalidscanf */
|
2010-12-29 12:58:10 +03:00
|
|
|
if (sscanf (color_name, "color%d%c", &i, &dummy) == 1 && i >= 0 && i < 256)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
2013-10-10 16:21:26 +04:00
|
|
|
/* cppcheck-suppress invalidscanf */
|
2010-12-29 12:58:10 +03:00
|
|
|
if (sscanf (color_name, "gray%d%c", &i, &dummy) == 1 && i >= 0 && i < 24)
|
|
|
|
{
|
|
|
|
return 232 + i;
|
|
|
|
}
|
|
|
|
if (strncmp (color_name, "rgb", 3) == 0 &&
|
|
|
|
color_name[3] >= '0' && color_name[3] < '6' &&
|
|
|
|
color_name[4] >= '0' && color_name[4] < '6' &&
|
|
|
|
color_name[5] >= '0' && color_name[5] < '6' && color_name[6] == '\0')
|
|
|
|
{
|
|
|
|
return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0');
|
|
|
|
}
|
2016-11-02 14:07:33 +03:00
|
|
|
if (color_name[0] == '#')
|
|
|
|
{
|
2016-11-13 15:47:57 +03:00
|
|
|
int len;
|
2016-11-02 14:07:33 +03:00
|
|
|
int h[6];
|
|
|
|
|
|
|
|
color_name++;
|
2016-11-13 15:47:57 +03:00
|
|
|
len = (int) strlen (color_name);
|
|
|
|
if (len != 3 && len != 6)
|
2016-11-02 14:07:33 +03:00
|
|
|
return -1;
|
|
|
|
|
2016-11-13 15:47:57 +03:00
|
|
|
for (i = 0; i < len; i++)
|
2016-11-02 14:07:33 +03:00
|
|
|
{
|
|
|
|
h[i] = parse_hex_digit (color_name[i]);
|
|
|
|
if (h[i] == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 3)
|
|
|
|
i = (h[0] << 20) | (h[0] << 16) | (h[1] << 12) | (h[1] << 8) | (h[2] << 4) | h[2];
|
|
|
|
else
|
|
|
|
i = (h[0] << 20) | (h[1] << 16) | (h[2] << 12) | (h[3] << 8) | (h[4] << 4) | h[5];
|
|
|
|
return (1 << 24) | i;
|
|
|
|
}
|
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
const char *
|
2010-12-29 12:58:10 +03:00
|
|
|
tty_color_get_name_by_index (int idx)
|
2009-05-10 19:01:15 +04:00
|
|
|
{
|
2010-12-29 12:58:10 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Find the real English name of the first 16 colors, */
|
|
|
|
/* as well as the A_* special values. */
|
|
|
|
for (i = 0; color_table[i].name != NULL; i++)
|
|
|
|
if (idx == color_table[i].value)
|
|
|
|
return color_table[i].name;
|
2016-11-02 14:07:33 +03:00
|
|
|
|
|
|
|
/* Create and return the strings in "colorNNN" or "#rrggbb" format. */
|
|
|
|
if ((idx >= 16 && idx < 256) || (idx & (1 << 24)) != 0)
|
2010-09-12 18:32:26 +04:00
|
|
|
{
|
2016-11-02 14:07:33 +03:00
|
|
|
char name[9];
|
2013-10-15 10:34:04 +04:00
|
|
|
|
2016-11-02 14:07:33 +03:00
|
|
|
if (idx < 256)
|
|
|
|
sprintf (name, "color%d", idx);
|
|
|
|
else
|
2016-12-06 11:08:47 +03:00
|
|
|
sprintf (name, "#%06X", (unsigned int) idx & 0xFFFFFF);
|
2016-11-02 14:07:33 +03:00
|
|
|
return g_intern_string (name);
|
2010-09-12 18:32:26 +04:00
|
|
|
}
|
2010-12-29 12:58:10 +03:00
|
|
|
return "default";
|
2009-05-10 19:01:15 +04:00
|
|
|
}
|
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_get_index_by_name (const char *color_name)
|
2009-05-10 19:01:15 +04:00
|
|
|
{
|
2009-09-14 17:43:03 +04:00
|
|
|
if (color_name != NULL)
|
2010-09-12 18:32:26 +04:00
|
|
|
{
|
|
|
|
size_t i;
|
2013-10-10 16:21:26 +04:00
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
for (i = 0; color_table[i].name != NULL; i++)
|
|
|
|
if (strcmp (color_name, color_table[i].name) == 0)
|
|
|
|
return color_table[i].value;
|
2016-11-02 14:07:33 +03:00
|
|
|
return parse_256_or_true_color_name (color_name);
|
2010-09-12 18:32:26 +04:00
|
|
|
}
|
2009-09-04 18:22:49 +04:00
|
|
|
return -1;
|
2009-05-10 19:01:15 +04:00
|
|
|
}
|
2009-09-04 18:22:49 +04:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-12-29 12:58:10 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
tty_attr_get_bits (const char *attrs)
|
|
|
|
{
|
|
|
|
int attr_bits = 0;
|
|
|
|
|
|
|
|
if (attrs != NULL)
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
gchar **attr_list;
|
|
|
|
int i;
|
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
attr_list = g_strsplit (attrs, "+", -1);
|
2013-10-15 10:34:04 +04:00
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
for (i = 0; attr_list[i] != NULL; i++)
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
int j;
|
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
for (j = 0; attributes_table[j].name != NULL; j++)
|
|
|
|
{
|
|
|
|
if (strcmp (attr_list[i], attributes_table[j].name) == 0)
|
|
|
|
{
|
|
|
|
attr_bits |= attributes_table[j].value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev (attr_list);
|
|
|
|
}
|
|
|
|
return attr_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|