summaryrefslogtreecommitdiff
path: root/src
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 /src
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.
Diffstat (limited to 'src')
-rw-r--r--src/ui/color.c37
-rw-r--r--src/ui/color.h1
2 files changed, 20 insertions, 18 deletions
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);