summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-11-23 17:14:07 +0200
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-11-23 17:14:07 +0200
commit4463f4c079d4592ace5ed6406c521782482d3840 (patch)
treea90c321e4e88c6421dbbcc23db31d2a97753d7f0
parent931b59a1cfbf504a390654c7baed5eba928dc044 (diff)
Color: Improve legibility of ANSI foreground colors
On colored theme backgrounds, take great liberties to modify the ANSI foreground color so it remains legible.
-rw-r--r--res/about/help.gmi1
-rw-r--r--src/ui/color.c37
-rw-r--r--src/ui/color.h1
3 files changed, 21 insertions, 18 deletions
diff --git a/res/about/help.gmi b/res/about/help.gmi
index 3c332f81..a25f556e 100644
--- a/res/about/help.gmi
+++ b/res/about/help.gmi
@@ -423,6 +423,7 @@ Sometimes these codes are used for things like colored ASCII art. However, Lagra
423The "ANSI escapes" setting controls which ANSI escape codes are enabled: 423The "ANSI escapes" setting controls which ANSI escape codes are enabled:
424 424
425* "FG Color" enables changes to text foreground color. 425* "FG Color" enables changes to text foreground color.
426* "BG Color" enables changes to text background color.
426* "Font Style" enables changing the font style: bold, italic, or monospace. (These correspond to codes 1, 2, and 11.) 427* "Font Style" enables changing the font style: bold, italic, or monospace. (These correspond to codes 1, 2, and 11.)
427 428
428A warning banner is displayed if any codes are detected on a page. This helps you be aware of potential visual artifacts, like text color that is being forced to black regardless of the page background color. Click on the warning to dismiss it on a per-site basis. 429A warning banner is displayed if any codes are detected on a page. This helps you be aware of potential visual artifacts, like text color that is being forced to black regardless of the page background color. Click on the warning to dismiss it on a per-site basis.
diff --git a/src/ui/color.c b/src/ui/color.c
index adbe444b..0b927fd6 100644
--- a/src/ui/color.c
+++ b/src/ui/color.c
@@ -460,6 +460,10 @@ iColor rgb_HSLColor(iHSLColor hsl) {
460 return toColor_(init_F4(r, g, b, hsl.a)); 460 return toColor_(init_F4(r, g, b, hsl.a));
461} 461}
462 462
463float luma_Color(iColor color) {
464 return 0.299f * color.r / 255.0f + 0.587f * color.g / 255.0f + 0.114f * color.b / 255.0f;
465}
466
463const char *escape_Color(int color) { 467const char *escape_Color(int color) {
464 static const char *esc[] = { 468 static const char *esc[] = {
465 black_ColorEscape, 469 black_ColorEscape,
@@ -851,26 +855,23 @@ void ansiColors_Color(iRangecc escapeSequence, int fgDefault, int bgDefault,
851 break; 855 break;
852 } 856 }
853 } 857 }
854 /* Ensure legibility if only one of the colors is set. */ 858 /* Ensure legibility if only the foreground color is set. */
855 /* TODO: Force darkening of the background color, unless it is also specified. */ 859 if (fg.a) {
856#if 0 860 const iHSLColor themeBg = get_HSLColor(tmBackground_ColorId);
857 if (bg.a == 0 && !equal_Color(fg, get_Color(fgDefault))) { 861 const float bgLuminance = luma_Color(get_Color(tmBackground_ColorId));
858 if (delta_Color(fg, get_Color(tmBackground_ColorId)) < 64) { 862 if (bgLuminance > 0.4f) {
859 const iHSLColor fgHsl = hsl_Color(fg); 863 float dim = (bgLuminance - 0.4f);
860 iHSLColor legibleBg = get_HSLColor(tmBackground_ColorId); 864 fg.r *= 0.5f * dim;
861 if () 865 fg.g *= 0.5f * dim;
862 bg = rgb_HSLColor(bgHsl); 866 fg.b *= 0.5f * dim;
863 } 867 }
868 if (themeBg.sat > 0.15f && themeBg.lum >= 0.5f) {
869 iHSLColor fgHsl = hsl_Color(fg);
870 fgHsl.hue = themeBg.hue;
871 fgHsl.lum = themeBg.lum * 0.5f;
872 fg = rgb_HSLColor(fgHsl);
864 } 873 }
865 } 874 }
866#endif
867 /*
868 if (!bg_out || get_HSLColor(tmBackground_ColorId).lum > 0.5f) {
869 fg.r /= 2;
870 fg.g /= 2;
871 fg.b /= 2;
872 }
873 */
874 if (fg.a && fg_out) { 875 if (fg.a && fg_out) {
875 *fg_out = fg; 876 *fg_out = fg;
876 } 877 }
diff --git a/src/ui/color.h b/src/ui/color.h
index 6602b226..0b3f8bed 100644
--- a/src/ui/color.h
+++ b/src/ui/color.h
@@ -233,6 +233,7 @@ struct Impl_HSLColor {
233 233
234iHSLColor hsl_Color (iColor); 234iHSLColor hsl_Color (iColor);
235iColor rgb_HSLColor (iHSLColor); 235iColor rgb_HSLColor (iHSLColor);
236float luma_Color (iColor);
236 237
237iHSLColor setSat_HSLColor (iHSLColor, float sat); 238iHSLColor setSat_HSLColor (iHSLColor, float sat);
238iHSLColor setLum_HSLColor (iHSLColor, float lum); 239iHSLColor setLum_HSLColor (iHSLColor, float lum);