color: default to dark color theme
Subsystems (3)
PATCH v2 1/1 color: default to dark color theme
From: Matteo Croce <[email protected]>
The majority of Linux terminals are using a dark background. iproute2 tries to detect the color theme via the COLORFGBG environment variable, and defaults to light background if not set.
Change the default behaviour to dark background, and while at it change the current logic which assumes that the color code is a single digit.
--- lib/color.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
lib/color.c +9 -7
@@ -72,7 +72,7 @@ static enum color attr_colors_dark[] = { C_CLEAR }; -static int is_dark_bg; +static int is_light_bg;static int color_is_enabled;static void enable_color (void )
@@ -128,10 +128,12 @@ static void set_color_palette(void)* values separated by semicolons; we want the last value in either case. * If this value is 0-6 or 8, background is dark. */ -if (p && (p =strrchr (p,';' )) !=NULL - && ((p[1 ] >='0' && p[1 ] <='6' ) || p[1 ] =='8' ) - && p[2 ] =='\0' ) - is_dark_bg =1 ; +if (p && (p =strrchr (p,';' )) !=NULL ) { +int bg =atoi (p +1 ); + +if (bg ==7 || (bg >=9 && bg <=15 )) + is_light_bg =1 ; + } } __attribute__((format(printf, 3, 4)))
@@ -150,8 +152,8 @@ int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...) goto end; } - ret +=fprintf (fp,"%s" , color_codes[is_dark_bg ? - attr_colors_dark[attr] : attr_colors_light[attr]]); + ret +=fprintf (fp,"%s" , color_codes[is_light_bg ? + attr_colors_light[attr] : attr_colors_dark[attr]]); ret +=vfprintf (fp, fmt, args); ret +=fprintf (fp,"%s" , color_codes[C_CLEAR ]);
Signature · 1 line
2.48.1
Quoted 6 lines · On Mon, 10 Mar 2025 21:36:09 +0100 Matteo Croce <[email protected]> wrote:
From: Matteo Croce <[email protected]>
The majority of Linux terminals are using a dark background. iproute2 tries to detect the color theme via the
COLORFGBGenvironment variable, and defaults to light background if not set.
This is not true. The default gnome terminal color palette is not dark.
Quoted 4 lines
Change the default behaviour to dark background, and while at it change the current logic which assumes that the color code is a single digit.
Signed-off-by Matteo Croce <[email protected]>
The code was added to follow the conventions of other Linux packages. Probably best to do something smarter (like util-linux) or more exactly follow what systemd or vim are doing.
Quoted 11 lines · Stephen Hemminger
Quoted 6 lines · On Mon, 10 Mar 2025 21:36:09 +0100 Matteo Croce <[email protected]> wrote:
From: Matteo Croce <[email protected]>
The majority of Linux terminals are using a dark background. iproute2 tries to detect the color theme via the
COLORFGBGenvironment variable, and defaults to light background if not set.This is not true. The default gnome terminal color palette is not dark.
ACK. Ever since that famous movie I stick to the real(TM) programmer colors of green on black[1], but about half of all the blue pill takers probably don't.
Quoted 8 lines
Quoted 4 lines
Change the default behaviour to dark background, and while at it change the current logic which assumes that the color code is a single digit.
Signed-off-by Matteo Croce <[email protected]>The code was added to follow the conventions of other Linux packages. Probably best to do something smarter (like util-linux) or more exactly follow what systemd or vim are doing.
I can't recall a single system on which I didn't have to 'set bg=dark' in .vimrc explicitly, so this makes me curious: Could you name a concrete example of working auto color adjustment to given terminal background?
Looking at vim-9.1.0794 source code, I see:
| char_u *
| term_bg_default(void)
| {
| #if defined(MSWIN)
| // DOS console is nearly always black
| return (char_u *)"dark";
| #else
| char_u *p;
|
| if (STRCMP(T_NAME, "linux") == 0
| || STRCMP(T_NAME, "screen.linux") == 0
| || STRNCMP(T_NAME, "cygwin", 6) == 0
| || STRNCMP(T_NAME, "putty", 5) == 0
| || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL
| && (p = vim_strrchr(p, ';')) != NULL
| && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
| && p[2] == NUL))
| return (char_u *)"dark";
| return (char_u *)"light";
| #endif
| }So apart from a little guesswork based on terminal names, this does the same as iproute currently (in his commit 54eab4c79a608 implementing set_color_palette(), Petr Vorel even admitted where he had copied the code from). No hidden gems to be found in vim sources, at least!
Cheers, Phil
[1] And have the screen rotated 90 degrees to make it more realistic, but that's off topic.
Il giorno gio 13 mar 2025 alle ore 12:28 Phil Sutter <[email protected]> ha scritto:
Quoted 61 lines · Stephen Hemminger
Quoted 11 lines
Quoted 6 lines · On Mon, 10 Mar 2025 21:36:09 +0100 Matteo Croce <[email protected]> wrote:
From: Matteo Croce <[email protected]>
The majority of Linux terminals are using a dark background. iproute2 tries to detect the color theme via the
COLORFGBGenvironment variable, and defaults to light background if not set.This is not true. The default gnome terminal color palette is not dark.
ACK. Ever since that famous movie I stick to the real(TM) programmer colors of green on black[1], but about half of all the blue pill takers probably don't.
Quoted 8 lines
Quoted 4 lines
Change the default behaviour to dark background, and while at it change the current logic which assumes that the color code is a single digit.
Signed-off-by Matteo Croce <[email protected]>The code was added to follow the conventions of other Linux packages. Probably best to do something smarter (like util-linux) or more exactly follow what systemd or vim are doing.
I can't recall a single system on which I didn't have to 'set bg=dark' in .vimrc explicitly, so this makes me curious: Could you name a concrete example of working auto color adjustment to given terminal background?
Looking at vim-9.1.0794 source code, I see:
| char_u * | term_bg_default(void) | { | #if defined(MSWIN) | // DOS console is nearly always black | return (char_u *)"dark"; | #else | char_u *p; | | if (STRCMP(T_NAME, "linux") == 0 | || STRCMP(T_NAME, "screen.linux") == 0 | || STRNCMP(T_NAME, "cygwin", 6) == 0 | || STRNCMP(T_NAME, "putty", 5) == 0 | || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL | && (p = vim_strrchr(p, ';')) != NULL | && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8') | && p[2] == NUL)) | return (char_u *)"dark"; | return (char_u *)"light"; | #endif | }So apart from a little guesswork based on terminal names, this does the same as iproute currently (in his commit
54eab4c79a608implementingset_color_palette(), Petr Vorel even admitted where he had copied the code from). No hidden gems to be found in vim sources, at least!
Quoted 4 lines
Cheers, Phil
[1] And have the screen rotated 90 degrees to make it more realistic, but that's off topic.
I think that we could use the OSC command 11 to query the color:
# black background $ echo -ne '\e]11;?\a' 11;rgb:0000/0000/0000
# white background $ echo -ne '\e]11;?\a' 11;rgb:ffff/ffff/ffff
Signature · 3 lines
Matteo Croce
perl -e 'for($t=0;;$t++){print chr($t*($t>>8|$t>>13)&255)}' |aplayQuoted 69 lines · Matteo Croce
Il giorno gio 13 mar 2025 alle ore 12:28 Phil Sutter <[email protected]> ha scritto:
Quoted 61 lines · Stephen Hemminger
Quoted 11 lines
Quoted 6 lines · On Mon, 10 Mar 2025 21:36:09 +0100 Matteo Croce <[email protected]> wrote:
From: Matteo Croce <[email protected]>
The majority of Linux terminals are using a dark background. iproute2 tries to detect the color theme via the
COLORFGBGenvironment variable, and defaults to light background if not set.This is not true. The default gnome terminal color palette is not dark.
ACK. Ever since that famous movie I stick to the real(TM) programmer colors of green on black[1], but about half of all the blue pill takers probably don't.
Quoted 8 lines
Quoted 4 lines
Change the default behaviour to dark background, and while at it change the current logic which assumes that the color code is a single digit.
Signed-off-by Matteo Croce <[email protected]>The code was added to follow the conventions of other Linux packages. Probably best to do something smarter (like util-linux) or more exactly follow what systemd or vim are doing.
I can't recall a single system on which I didn't have to 'set bg=dark' in .vimrc explicitly, so this makes me curious: Could you name a concrete example of working auto color adjustment to given terminal background?
Looking at vim-9.1.0794 source code, I see:
| char_u * | term_bg_default(void) | { | #if defined(MSWIN) | // DOS console is nearly always black | return (char_u *)"dark"; | #else | char_u *p; | | if (STRCMP(T_NAME, "linux") == 0 | || STRCMP(T_NAME, "screen.linux") == 0 | || STRNCMP(T_NAME, "cygwin", 6) == 0 | || STRNCMP(T_NAME, "putty", 5) == 0 | || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL | && (p = vim_strrchr(p, ';')) != NULL | && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8') | && p[2] == NUL)) | return (char_u *)"dark"; | return (char_u *)"light"; | #endif | }So apart from a little guesswork based on terminal names, this does the same as iproute currently (in his commit
54eab4c79a608implementingset_color_palette(), Petr Vorel even admitted where he had copied the code from). No hidden gems to be found in vim sources, at least!Quoted 4 lines
Cheers, Phil
[1] And have the screen rotated 90 degrees to make it more realistic, but that's off topic.
I think that we could use the OSC command 11 to query the color:
Quoted 7 lines
# black background $ echo -ne '\e]11;?\a' 11;rgb:0000/0000/0000# white background $ echo -ne '\e]11;?\a' 11;rgb:ffff/ffff/ffff
Maybe a better technique than checking $COLORFGBG. Note that:
- This may return rgba and a transparency value
- In 'xterm -bg green', it returns '11;rgb:0000/ffff/0000'
So the value may not be as clear as in the above cases.
Cheers, Phil
Quoted 78 lines · On Thu, 13 Mar 2025 13:05:19 +0100 Phil Sutter <[email protected]> wrote:
Quoted 69 lines · Matteo Croce
Il giorno gio 13 mar 2025 alle ore 12:28 Phil Sutter <[email protected]> ha scritto:
Quoted 61 lines · Stephen Hemminger
Quoted 11 lines
Quoted 6 lines · On Mon, 10 Mar 2025 21:36:09 +0100 Matteo Croce <[email protected]> wrote:
From: Matteo Croce <[email protected]>
The majority of Linux terminals are using a dark background. iproute2 tries to detect the color theme via the
COLORFGBGenvironment variable, and defaults to light background if not set.This is not true. The default gnome terminal color palette is not dark.
ACK. Ever since that famous movie I stick to the real(TM) programmer colors of green on black[1], but about half of all the blue pill takers probably don't.
Quoted 8 lines
Quoted 4 lines
Change the default behaviour to dark background, and while at it change the current logic which assumes that the color code is a single digit.
Signed-off-by Matteo Croce <[email protected]>The code was added to follow the conventions of other Linux packages. Probably best to do something smarter (like util-linux) or more exactly follow what systemd or vim are doing.
I can't recall a single system on which I didn't have to 'set bg=dark' in .vimrc explicitly, so this makes me curious: Could you name a concrete example of working auto color adjustment to given terminal background?
Looking at vim-9.1.0794 source code, I see:
| char_u * | term_bg_default(void) | { | #if defined(MSWIN) | // DOS console is nearly always black | return (char_u *)"dark"; | #else | char_u *p; | | if (STRCMP(T_NAME, "linux") == 0 | || STRCMP(T_NAME, "screen.linux") == 0 | || STRNCMP(T_NAME, "cygwin", 6) == 0 | || STRNCMP(T_NAME, "putty", 5) == 0 | || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL | && (p = vim_strrchr(p, ';')) != NULL | && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8') | && p[2] == NUL)) | return (char_u *)"dark"; | return (char_u *)"light"; | #endif | }So apart from a little guesswork based on terminal names, this does the same as iproute currently (in his commit
54eab4c79a608implementingset_color_palette(), Petr Vorel even admitted where he had copied the code from). No hidden gems to be found in vim sources, at least!Quoted 4 lines
Cheers, Phil
[1] And have the screen rotated 90 degrees to make it more realistic, but that's off topic.
I think that we could use the OSC command 11 to query the color:
Quoted 7 lines
# black background $ echo -ne '\e]11;?\a' 11;rgb:0000/0000/0000# white background $ echo -ne '\e]11;?\a' 11;rgb:ffff/ffff/ffff
Quoted 8 lines
Maybe a better technique than checking $COLORFGBG. Note that:
- This may return rgba and a transparency value
- In 'xterm -bg green', it returns '11;rgb:0000/ffff/0000'So the value may not be as clear as in the above cases.
Cheers, Phil
Rather than hard coding color palettes it would be better to use some form of environment or config file to allow user to choose.
Quoted 86 lines · Stephen Hemminger
Quoted 83 lines · On Thu, 13 Mar 2025 13:05:19 +0100 Phil Sutter <[email protected]> wrote:
Quoted 69 lines · Matteo Croce
Il giorno gio 13 mar 2025 alle ore 12:28 Phil Sutter <[email protected]> ha scritto:
Quoted 61 lines · Stephen Hemminger
Quoted 11 lines
Quoted 6 lines · On Mon, 10 Mar 2025 21:36:09 +0100 Matteo Croce <[email protected]> wrote:
From: Matteo Croce <[email protected]>
The majority of Linux terminals are using a dark background. iproute2 tries to detect the color theme via the
COLORFGBGenvironment variable, and defaults to light background if not set.This is not true. The default gnome terminal color palette is not dark.
ACK. Ever since that famous movie I stick to the real(TM) programmer colors of green on black[1], but about half of all the blue pill takers probably don't.
Quoted 8 lines
Quoted 4 lines
Change the default behaviour to dark background, and while at it change the current logic which assumes that the color code is a single digit.
Signed-off-by Matteo Croce <[email protected]>The code was added to follow the conventions of other Linux packages. Probably best to do something smarter (like util-linux) or more exactly follow what systemd or vim are doing.
I can't recall a single system on which I didn't have to 'set bg=dark' in .vimrc explicitly, so this makes me curious: Could you name a concrete example of working auto color adjustment to given terminal background?
Looking at vim-9.1.0794 source code, I see:
| char_u * | term_bg_default(void) | { | #if defined(MSWIN) | // DOS console is nearly always black | return (char_u *)"dark"; | #else | char_u *p; | | if (STRCMP(T_NAME, "linux") == 0 | || STRCMP(T_NAME, "screen.linux") == 0 | || STRNCMP(T_NAME, "cygwin", 6) == 0 | || STRNCMP(T_NAME, "putty", 5) == 0 | || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL | && (p = vim_strrchr(p, ';')) != NULL | && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8') | && p[2] == NUL)) | return (char_u *)"dark"; | return (char_u *)"light"; | #endif | }So apart from a little guesswork based on terminal names, this does the same as iproute currently (in his commit
54eab4c79a608implementingset_color_palette(), Petr Vorel even admitted where he had copied the code from). No hidden gems to be found in vim sources, at least!Quoted 4 lines
Cheers, Phil
[1] And have the screen rotated 90 degrees to make it more realistic, but that's off topic.
I think that we could use the OSC command 11 to query the color:
Quoted 7 lines
# black background $ echo -ne '\e]11;?\a' 11;rgb:0000/0000/0000# white background $ echo -ne '\e]11;?\a' 11;rgb:ffff/ffff/ffffMaybe a better technique than checking $COLORFGBG. Note that:
- This may return rgba and a transparency value
- In 'xterm -bg green', it returns '11;rgb:0000/ffff/0000'
Quoted 6 lines
Quoted 3 lines
So the value may not be as clear as in the above cases.
Cheers, Phil
Rather than hard coding color palettes it would be better to use some form of environment or config file to allow user to choose.
I think we have that already. Quoting from ip(8):
-c[color][={always|auto|never}
[...]
Used color palette can be influenced by COLORFGBG environment variable (see ENVIRONMENT).
[...]
ENVIRONMENT
COLORFGBG
If set, it’s value is used for detection whether background is dark or light and use contrast colors for it.
COLORFGBG environment variable usually contains either two or three values separated by semicolons; we want the last value in either case. If this value is 0-6 or 8, chose colors suitable for dark background:
COLORFGBG=";0" ip -c a
Cheers, Phil
Quoted 106 lines · Phil Sutter
Quoted 86 lines · Stephen Hemminger
Quoted 83 lines · On Thu, 13 Mar 2025 13:05:19 +0100 Phil Sutter <[email protected]> wrote:
Quoted 69 lines · Matteo Croce
Il giorno gio 13 mar 2025 alle ore 12:28 Phil Sutter <[email protected]> ha scritto:
Quoted 61 lines · Stephen Hemminger
Quoted 11 lines
Quoted 6 lines · On Mon, 10 Mar 2025 21:36:09 +0100 Matteo Croce <[email protected]> wrote:
From: Matteo Croce <[email protected]>
The majority of Linux terminals are using a dark background. iproute2 tries to detect the color theme via the
COLORFGBGenvironment variable, and defaults to light background if not set.This is not true. The default gnome terminal color palette is not dark.
ACK. Ever since that famous movie I stick to the real(TM) programmer colors of green on black[1], but about half of all the blue pill takers probably don't.
Quoted 8 lines
Quoted 4 lines
Change the default behaviour to dark background, and while at it change the current logic which assumes that the color code is a single digit.
Signed-off-by Matteo Croce <[email protected]>The code was added to follow the conventions of other Linux packages. Probably best to do something smarter (like util-linux) or more exactly follow what systemd or vim are doing.
I can't recall a single system on which I didn't have to 'set bg=dark' in .vimrc explicitly, so this makes me curious: Could you name a concrete example of working auto color adjustment to given terminal background?
Looking at vim-9.1.0794 source code, I see:
| char_u * | term_bg_default(void) | { | #if defined(MSWIN) | // DOS console is nearly always black | return (char_u *)"dark"; | #else | char_u *p; | | if (STRCMP(T_NAME, "linux") == 0 | || STRCMP(T_NAME, "screen.linux") == 0 | || STRNCMP(T_NAME, "cygwin", 6) == 0 | || STRNCMP(T_NAME, "putty", 5) == 0 | || ((p = mch_getenv((char_u *)"COLORFGBG")) != NULL | && (p = vim_strrchr(p, ';')) != NULL | && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8') | && p[2] == NUL)) | return (char_u *)"dark"; | return (char_u *)"light"; | #endif | }So apart from a little guesswork based on terminal names, this does the same as iproute currently (in his commit
54eab4c79a608implementingset_color_palette(), Petr Vorel even admitted where he had copied the code from). No hidden gems to be found in vim sources, at least!Quoted 4 lines
Cheers, Phil
[1] And have the screen rotated 90 degrees to make it more realistic, but that's off topic.
I think that we could use the OSC command 11 to query the color:
Quoted 7 lines
# black background $ echo -ne '\e]11;?\a' 11;rgb:0000/0000/0000# white background $ echo -ne '\e]11;?\a' 11;rgb:ffff/ffff/ffffMaybe a better technique than checking $COLORFGBG. Note that:
- This may return rgba and a transparency value
- In 'xterm -bg green', it returns '11;rgb:0000/ffff/0000'Quoted 6 lines
Quoted 3 lines
So the value may not be as clear as in the above cases.
Cheers, Phil
Rather than hard coding color palettes it would be better to use some form of environment or config file to allow user to choose.
I think we have that already. Quoting from ip(8):
-c[color][={always|auto|never}
[...]
Used color palette can be influenced by COLORFGBG environment variable (see ENVIRONMENT).
[...]
ENVIRONMENT
COLORFGBG
If set, it’s value is used for detection whether background is dark or light and use contrast colors for it.
Quoted 6 lines
COLORFGBG environment variable usually contains either two or three values separated by semicolons; we want the last value in either case. If this value is 0-6 or 8, chose colors suitable for dark background:
COLORFGBG=";0" ip -c a
Assuming not every terminal sets $COLORFGBG, I guess what Matteo suggests should aid as a fallback for those cases. This would retain the existing behaviour wrt. COLORFGBG but improve the situation when user/terminal don't provide this hint.
Cheers, Phil