forked from aniani/vim
patch 8.2.3562: cannot add color names
Problem: Cannot add color names. Solution: Add the v:colornames dictionary. (Drew Vogel, closes #8761)
This commit is contained in:
committed by
Bram Moolenaar
parent
3c5904d2a5
commit
e30d10253f
199
src/term.c
199
src/term.c
@@ -1353,7 +1353,7 @@ termgui_get_color(char_u *name)
|
||||
t = termgui_mch_get_color(name);
|
||||
|
||||
if (t == INVALCOLOR)
|
||||
semsg(_(e_alloc_color), name);
|
||||
semsg(_(e_cannot_allocate_color_str), name);
|
||||
return t;
|
||||
}
|
||||
|
||||
@@ -6630,203 +6630,6 @@ swap_tcap(void)
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
|
||||
static int
|
||||
hex_digit(int c)
|
||||
{
|
||||
if (isdigit(c))
|
||||
return c - '0';
|
||||
c = TOLOWER_ASC(c);
|
||||
if (c >= 'a' && c <= 'f')
|
||||
return c - 'a' + 10;
|
||||
return 0x1ffffff;
|
||||
}
|
||||
|
||||
# ifdef VIMDLL
|
||||
static guicolor_T
|
||||
gui_adjust_rgb(guicolor_T c)
|
||||
{
|
||||
if (gui.in_use)
|
||||
return c;
|
||||
else
|
||||
return ((c & 0xff) << 16) | (c & 0x00ff00) | ((c >> 16) & 0xff);
|
||||
}
|
||||
# else
|
||||
# define gui_adjust_rgb(c) (c)
|
||||
# endif
|
||||
|
||||
guicolor_T
|
||||
gui_get_color_cmn(char_u *name)
|
||||
{
|
||||
// On MS-Windows an RGB macro is available and it produces 0x00bbggrr color
|
||||
// values as used by the MS-Windows GDI api. It should be used only for
|
||||
// MS-Windows GDI builds.
|
||||
# if defined(RGB) && defined(MSWIN) && !defined(FEAT_GUI)
|
||||
# undef RGB
|
||||
# endif
|
||||
# ifndef RGB
|
||||
# define RGB(r, g, b) ((r<<16) | (g<<8) | (b))
|
||||
# endif
|
||||
# define LINE_LEN 100
|
||||
FILE *fd;
|
||||
char line[LINE_LEN];
|
||||
char_u *fname;
|
||||
int r, g, b, i;
|
||||
guicolor_T color;
|
||||
|
||||
struct rgbcolor_table_S {
|
||||
char_u *color_name;
|
||||
guicolor_T color;
|
||||
};
|
||||
|
||||
// Only non X11 colors (not present in rgb.txt) and colors in
|
||||
// color_names[], useful when $VIMRUNTIME is not found,.
|
||||
static struct rgbcolor_table_S rgb_table[] = {
|
||||
{(char_u *)"black", RGB(0x00, 0x00, 0x00)},
|
||||
{(char_u *)"blue", RGB(0x00, 0x00, 0xFF)},
|
||||
{(char_u *)"brown", RGB(0xA5, 0x2A, 0x2A)},
|
||||
{(char_u *)"cyan", RGB(0x00, 0xFF, 0xFF)},
|
||||
{(char_u *)"darkblue", RGB(0x00, 0x00, 0x8B)},
|
||||
{(char_u *)"darkcyan", RGB(0x00, 0x8B, 0x8B)},
|
||||
{(char_u *)"darkgray", RGB(0xA9, 0xA9, 0xA9)},
|
||||
{(char_u *)"darkgreen", RGB(0x00, 0x64, 0x00)},
|
||||
{(char_u *)"darkgrey", RGB(0xA9, 0xA9, 0xA9)},
|
||||
{(char_u *)"darkmagenta", RGB(0x8B, 0x00, 0x8B)},
|
||||
{(char_u *)"darkred", RGB(0x8B, 0x00, 0x00)},
|
||||
{(char_u *)"darkyellow", RGB(0x8B, 0x8B, 0x00)}, // No X11
|
||||
{(char_u *)"gray", RGB(0xBE, 0xBE, 0xBE)},
|
||||
{(char_u *)"green", RGB(0x00, 0xFF, 0x00)},
|
||||
{(char_u *)"grey", RGB(0xBE, 0xBE, 0xBE)},
|
||||
{(char_u *)"grey40", RGB(0x66, 0x66, 0x66)},
|
||||
{(char_u *)"grey50", RGB(0x7F, 0x7F, 0x7F)},
|
||||
{(char_u *)"grey90", RGB(0xE5, 0xE5, 0xE5)},
|
||||
{(char_u *)"lightblue", RGB(0xAD, 0xD8, 0xE6)},
|
||||
{(char_u *)"lightcyan", RGB(0xE0, 0xFF, 0xFF)},
|
||||
{(char_u *)"lightgray", RGB(0xD3, 0xD3, 0xD3)},
|
||||
{(char_u *)"lightgreen", RGB(0x90, 0xEE, 0x90)},
|
||||
{(char_u *)"lightgrey", RGB(0xD3, 0xD3, 0xD3)},
|
||||
{(char_u *)"lightmagenta", RGB(0xFF, 0x8B, 0xFF)}, // No X11
|
||||
{(char_u *)"lightred", RGB(0xFF, 0x8B, 0x8B)}, // No X11
|
||||
{(char_u *)"lightyellow", RGB(0xFF, 0xFF, 0xE0)},
|
||||
{(char_u *)"magenta", RGB(0xFF, 0x00, 0xFF)},
|
||||
{(char_u *)"red", RGB(0xFF, 0x00, 0x00)},
|
||||
{(char_u *)"seagreen", RGB(0x2E, 0x8B, 0x57)},
|
||||
{(char_u *)"white", RGB(0xFF, 0xFF, 0xFF)},
|
||||
{(char_u *)"yellow", RGB(0xFF, 0xFF, 0x00)},
|
||||
};
|
||||
|
||||
static struct rgbcolor_table_S *colornames_table;
|
||||
static int size = 0;
|
||||
|
||||
if (name[0] == '#' && STRLEN(name) == 7)
|
||||
{
|
||||
// Name is in "#rrggbb" format
|
||||
color = RGB(((hex_digit(name[1]) << 4) + hex_digit(name[2])),
|
||||
((hex_digit(name[3]) << 4) + hex_digit(name[4])),
|
||||
((hex_digit(name[5]) << 4) + hex_digit(name[6])));
|
||||
if (color > 0xffffff)
|
||||
return INVALCOLOR;
|
||||
return gui_adjust_rgb(color);
|
||||
}
|
||||
|
||||
// Check if the name is one of the colors we know
|
||||
for (i = 0; i < (int)ARRAY_LENGTH(rgb_table); i++)
|
||||
if (STRICMP(name, rgb_table[i].color_name) == 0)
|
||||
return gui_adjust_rgb(rgb_table[i].color);
|
||||
|
||||
/*
|
||||
* Last attempt. Look in the file "$VIMRUNTIME/rgb.txt".
|
||||
*/
|
||||
if (size == 0)
|
||||
{
|
||||
int counting;
|
||||
|
||||
// colornames_table not yet initialized
|
||||
fname = expand_env_save((char_u *)"$VIMRUNTIME/rgb.txt");
|
||||
if (fname == NULL)
|
||||
return INVALCOLOR;
|
||||
|
||||
fd = fopen((char *)fname, "rt");
|
||||
vim_free(fname);
|
||||
if (fd == NULL)
|
||||
{
|
||||
if (p_verbose > 1)
|
||||
verb_msg(_("Cannot open $VIMRUNTIME/rgb.txt"));
|
||||
size = -1; // don't try again
|
||||
return INVALCOLOR;
|
||||
}
|
||||
|
||||
for (counting = 1; counting >= 0; --counting)
|
||||
{
|
||||
if (!counting)
|
||||
{
|
||||
colornames_table = ALLOC_MULT(struct rgbcolor_table_S, size);
|
||||
if (colornames_table == NULL)
|
||||
{
|
||||
fclose(fd);
|
||||
return INVALCOLOR;
|
||||
}
|
||||
rewind(fd);
|
||||
}
|
||||
size = 0;
|
||||
|
||||
while (!feof(fd))
|
||||
{
|
||||
size_t len;
|
||||
int pos;
|
||||
|
||||
vim_ignoredp = fgets(line, LINE_LEN, fd);
|
||||
len = strlen(line);
|
||||
|
||||
if (len <= 1 || line[len - 1] != '\n')
|
||||
continue;
|
||||
|
||||
line[len - 1] = '\0';
|
||||
|
||||
i = sscanf(line, "%d %d %d %n", &r, &g, &b, &pos);
|
||||
if (i != 3)
|
||||
continue;
|
||||
|
||||
if (!counting)
|
||||
{
|
||||
char_u *s = vim_strsave((char_u *)line + pos);
|
||||
|
||||
if (s == NULL)
|
||||
{
|
||||
fclose(fd);
|
||||
return INVALCOLOR;
|
||||
}
|
||||
colornames_table[size].color_name = s;
|
||||
colornames_table[size].color = (guicolor_T)RGB(r, g, b);
|
||||
}
|
||||
size++;
|
||||
|
||||
// The distributed rgb.txt has less than 1000 entries. Limit to
|
||||
// 10000, just in case the file was messed up.
|
||||
if (size == 10000)
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(fd);
|
||||
}
|
||||
|
||||
for (i = 0; i < size; i++)
|
||||
if (STRICMP(name, colornames_table[i].color_name) == 0)
|
||||
return gui_adjust_rgb(colornames_table[i].color);
|
||||
|
||||
return INVALCOLOR;
|
||||
}
|
||||
|
||||
guicolor_T
|
||||
gui_get_rgb_color_cmn(int r, int g, int b)
|
||||
{
|
||||
guicolor_T color = RGB(r, g, b);
|
||||
|
||||
if (color > 0xffffff)
|
||||
return INVALCOLOR;
|
||||
return gui_adjust_rgb(color);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if (defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))) || defined(FEAT_TERMINAL) \
|
||||
|| defined(PROTO)
|
||||
|
Reference in New Issue
Block a user