From 285e2fddbf0af06dc6902ab34ace90744a7ce035 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Thu, 5 Nov 2020 08:59:00 +0200 Subject: Full 8-bit ANSI palette for foreground colors --- src/ui/color.c | 307 +++++++++++++++++++++++++++++++++++++++++++++++++++------ src/ui/color.h | 4 +- src/ui/text.c | 7 +- 3 files changed, 282 insertions(+), 36 deletions(-) (limited to 'src/ui') diff --git a/src/ui/color.c b/src/ui/color.c index 2cf7ebbd..332ef696 100644 --- a/src/ui/color.c +++ b/src/ui/color.c @@ -425,7 +425,269 @@ iHSLColor addSatLum_HSLColor(iHSLColor d, float sat, float lum) { return d; } -iColor ansi_Color(iRangecc escapeSequence, int fallback) { +static const iColor ansi8BitColors_[256] = { + { 0, 0, 0, 255 }, + { 170, 0, 0, 255 }, + { 0, 170, 0, 255 }, + { 170, 85, 0, 255 }, + { 0, 0, 170, 255 }, + { 170, 0, 170, 255 }, + { 0, 170, 170, 255 }, + { 170, 170, 170, 255 }, + + { 85, 85, 85, 255 }, + { 255, 85, 85, 255 }, + { 85, 255, 85, 255 }, + { 255, 255, 85, 255 }, + { 85, 85, 255, 255 }, + { 255, 85, 255, 255 }, + { 85, 255, 255, 255 }, + { 255, 255, 255, 255 }, + + { 0, 0, 0, 255 }, + { 0, 0, 95, 255 }, + { 0, 0, 135, 255 }, + { 0, 0, 175, 255 }, + { 0, 0, 215, 255 }, + { 0, 0, 255, 255 }, + { 0, 95, 0, 255 }, + { 0, 95, 95, 255 }, + { 0, 95, 135, 255 }, + { 0, 95, 175, 255 }, + { 0, 95, 215, 255 }, + { 0, 95, 255, 255 }, + { 0, 135, 0, 255 }, + { 0, 135, 95, 255 }, + { 0, 135, 135, 255 }, + { 0, 135, 175, 255 }, + { 0, 135, 215, 255 }, + { 0, 135, 255, 255 }, + { 0, 175, 0, 255 }, + { 0, 175, 95, 255 }, + { 0, 175, 135, 255 }, + { 0, 175, 175, 255 }, + { 0, 175, 215, 255 }, + { 0, 175, 255, 255 }, + { 0, 215, 0, 255 }, + { 0, 215, 95, 255 }, + { 0, 215, 135, 255 }, + { 0, 215, 175, 255 }, + { 0, 215, 215, 255 }, + { 0, 215, 255, 255 }, + { 0, 255, 0, 255 }, + { 0, 255, 95, 255 }, + { 0, 255, 135, 255 }, + { 0, 255, 175, 255 }, + { 0, 255, 215, 255 }, + { 0, 255, 255, 255 }, + { 95, 0, 0, 255 }, + { 95, 0, 95, 255 }, + { 95, 0, 135, 255 }, + { 95, 0, 175, 255 }, + { 95, 0, 215, 255 }, + { 95, 0, 255, 255 }, + { 95, 95, 0, 255 }, + { 95, 95, 95, 255 }, + { 95, 95, 135, 255 }, + { 95, 95, 175, 255 }, + { 95, 95, 215, 255 }, + { 95, 95, 255, 255 }, + { 95, 135, 0, 255 }, + { 95, 135, 95, 255 }, + { 95, 135, 135, 255 }, + { 95, 135, 175, 255 }, + { 95, 135, 215, 255 }, + { 95, 135, 255, 255 }, + { 95, 175, 0, 255 }, + { 95, 175, 95, 255 }, + { 95, 175, 135, 255 }, + { 95, 175, 175, 255 }, + { 95, 175, 215, 255 }, + { 95, 175, 255, 255 }, + { 95, 215, 0, 255 }, + { 95, 215, 95, 255 }, + { 95, 215, 135, 255 }, + { 95, 215, 175, 255 }, + { 95, 215, 215, 255 }, + { 95, 215, 255, 255 }, + { 95, 255, 0, 255 }, + { 95, 255, 95, 255 }, + { 95, 255, 135, 255 }, + { 95, 255, 175, 255 }, + { 95, 255, 215, 255 }, + { 95, 255, 255, 255 }, + { 135, 0, 0, 255 }, + { 135, 0, 95, 255 }, + { 135, 0, 135, 255 }, + { 135, 0, 175, 255 }, + { 135, 0, 215, 255 }, + { 135, 0, 255, 255 }, + { 135, 95, 0, 255 }, + { 135, 95, 95, 255 }, + { 135, 95, 135, 255 }, + { 135, 95, 175, 255 }, + { 135, 95, 215, 255 }, + { 135, 95, 255, 255 }, + { 135, 135, 0, 255 }, + { 135, 135, 95, 255 }, + { 135, 135, 135, 255 }, + { 135, 135, 175, 255 }, + { 135, 135, 215, 255 }, + { 135, 135, 255, 255 }, + { 135, 175, 0, 255 }, + { 135, 175, 95, 255 }, + { 135, 175, 135, 255 }, + { 135, 175, 175, 255 }, + { 135, 175, 215, 255 }, + { 135, 175, 255, 255 }, + { 135, 215, 0, 255 }, + { 135, 215, 95, 255 }, + { 135, 215, 135, 255 }, + { 135, 215, 175, 255 }, + { 135, 215, 215, 255 }, + { 135, 215, 255, 255 }, + { 135, 255, 0, 255 }, + { 135, 255, 95, 255 }, + { 135, 255, 135, 255 }, + { 135, 255, 175, 255 }, + { 135, 255, 215, 255 }, + { 135, 255, 255, 255 }, + { 175, 0, 0, 255 }, + { 175, 0, 95, 255 }, + { 175, 0, 135, 255 }, + { 175, 0, 175, 255 }, + { 175, 0, 215, 255 }, + { 175, 0, 255, 255 }, + { 175, 95, 0, 255 }, + { 175, 95, 95, 255 }, + { 175, 95, 135, 255 }, + { 175, 95, 175, 255 }, + { 175, 95, 215, 255 }, + { 175, 95, 255, 255 }, + { 175, 135, 0, 255 }, + { 175, 135, 95, 255 }, + { 175, 135, 135, 255 }, + { 175, 135, 175, 255 }, + { 175, 135, 215, 255 }, + { 175, 135, 255, 255 }, + { 175, 175, 0, 255 }, + { 175, 175, 95, 255 }, + { 175, 175, 135, 255 }, + { 175, 175, 175, 255 }, + { 175, 175, 215, 255 }, + { 175, 175, 255, 255 }, + { 175, 215, 0, 255 }, + { 175, 215, 95, 255 }, + { 175, 215, 135, 255 }, + { 175, 215, 175, 255 }, + { 175, 215, 215, 255 }, + { 175, 215, 255, 255 }, + { 175, 255, 0, 255 }, + { 175, 255, 95, 255 }, + { 175, 255, 135, 255 }, + { 175, 255, 175, 255 }, + { 175, 255, 215, 255 }, + { 175, 255, 255, 255 }, + { 215, 0, 0, 255 }, + { 215, 0, 95, 255 }, + { 215, 0, 135, 255 }, + { 215, 0, 175, 255 }, + { 215, 0, 215, 255 }, + { 215, 0, 255, 255 }, + { 215, 95, 0, 255 }, + { 215, 95, 95, 255 }, + { 215, 95, 135, 255 }, + { 215, 95, 175, 255 }, + { 215, 95, 215, 255 }, + { 215, 95, 255, 255 }, + { 215, 135, 0, 255 }, + { 215, 135, 95, 255 }, + { 215, 135, 135, 255 }, + { 215, 135, 175, 255 }, + { 215, 135, 215, 255 }, + { 215, 135, 255, 255 }, + { 215, 175, 0, 255 }, + { 215, 175, 95, 255 }, + { 215, 175, 135, 255 }, + { 215, 175, 175, 255 }, + { 215, 175, 215, 255 }, + { 215, 175, 255, 255 }, + { 215, 215, 0, 255 }, + { 215, 215, 95, 255 }, + { 215, 215, 135, 255 }, + { 215, 215, 175, 255 }, + { 215, 215, 215, 255 }, + { 215, 215, 255, 255 }, + { 215, 255, 0, 255 }, + { 215, 255, 95, 255 }, + { 215, 255, 135, 255 }, + { 215, 255, 175, 255 }, + { 215, 255, 215, 255 }, + { 215, 255, 255, 255 }, + { 255, 0, 0, 255 }, + { 255, 0, 95, 255 }, + { 255, 0, 135, 255 }, + { 255, 0, 175, 255 }, + { 255, 0, 215, 255 }, + { 255, 0, 255, 255 }, + { 255, 95, 0, 255 }, + { 255, 95, 95, 255 }, + { 255, 95, 135, 255 }, + { 255, 95, 175, 255 }, + { 255, 95, 215, 255 }, + { 255, 95, 255, 255 }, + { 255, 135, 0, 255 }, + { 255, 135, 95, 255 }, + { 255, 135, 135, 255 }, + { 255, 135, 175, 255 }, + { 255, 135, 215, 255 }, + { 255, 135, 255, 255 }, + { 255, 175, 0, 255 }, + { 255, 175, 95, 255 }, + { 255, 175, 135, 255 }, + { 255, 175, 175, 255 }, + { 255, 175, 215, 255 }, + { 255, 175, 255, 255 }, + { 255, 215, 0, 255 }, + { 255, 215, 95, 255 }, + { 255, 215, 135, 255 }, + { 255, 215, 175, 255 }, + { 255, 215, 215, 255 }, + { 255, 215, 255, 255 }, + { 255, 255, 0, 255 }, + { 255, 255, 95, 255 }, + { 255, 255, 135, 255 }, + { 255, 255, 175, 255 }, + { 255, 255, 215, 255 }, + { 255, 255, 255, 255 }, + + { 0, 0, 0, 255 }, + { 11, 11, 11, 255 }, + { 22, 22, 22, 255 }, + { 33, 33, 33, 255 }, + { 44, 44, 44, 255 }, + { 55, 55, 55, 255 }, + { 67, 67, 67, 255 }, + { 78, 78, 78, 255 }, + { 89, 89, 89, 255 }, + { 100, 100, 100, 255 }, + { 111, 111, 111, 255 }, + { 122, 122, 122, 255 }, + { 133, 133, 133, 255 }, + { 144, 144, 144, 255 }, + { 155, 155, 155, 255 }, + { 166, 166, 166, 255 }, + { 177, 177, 177, 255 }, + { 188, 188, 188, 255 }, + { 200, 200, 200, 255 }, + { 211, 211, 211, 255 }, + { 222, 222, 222, 255 }, + { 233, 233, 233, 255 }, + { 244, 244, 244, 255 }, + { 255, 255, 255, 255 } +}; + +iColor ansiForeground_Color(iRangecc escapeSequence, int fallback) { iColor clr = get_Color(fallback); for (const char *ch = escapeSequence.start; ch < escapeSequence.end; ch++) { char *endPtr; @@ -435,52 +697,35 @@ iColor ansi_Color(iRangecc escapeSequence, int fallback) { default: break; case 30: - clr = (iColor){ 0, 0, 0, 255 }; - break; case 31: - clr = (iColor){ 170, 0, 0, 255 }; - break; case 32: - clr = (iColor){ 0, 170, 0, 255 }; - break; case 33: - clr = (iColor){ 170, 85, 0, 255 }; - break; case 34: - clr = (iColor){ 0, 0, 170, 255 }; - break; case 35: - clr = (iColor){ 170, 0, 170, 255 }; - break; case 36: - clr = (iColor){ 0, 170, 170, 255 }; - break; case 37: - clr = (iColor){ 170, 170, 170, 255 }; + clr = ansi8BitColors_[arg - 30]; break; - case 90: - clr = (iColor){ 85, 85, 85, 255 }; + case 38: { + /* Extended foreground color. */ + arg = strtoul(ch + 1, &endPtr, 10); + ch = endPtr; + if (arg == 5) /* 8-bit palette */ { + arg = strtoul(ch + 1, &endPtr, 10); + ch = endPtr; + clr = ansi8BitColors_[iClamp(arg, 0, 255)]; + } break; + } + case 90: case 91: - clr = (iColor){ 255, 85, 85, 255 }; - break; case 92: - clr = (iColor){ 85, 255, 85, 255 }; - break; case 93: - clr = (iColor){ 255, 255, 85, 255 }; - break; case 94: - clr = (iColor){ 85, 85, 255, 255 }; - break; case 95: - clr = (iColor){ 255, 85, 255, 255 }; - break; case 96: - clr = (iColor){ 85, 255, 255, 255 }; - break; case 97: - clr = (iColor){ 255, 255, 255, 255 }; + clr = ansi8BitColors_[8 + arg - 90]; break; } } diff --git a/src/ui/color.h b/src/ui/color.h index 2b7fd735..94c38aec 100644 --- a/src/ui/color.h +++ b/src/ui/color.h @@ -227,5 +227,5 @@ iLocalDef void setHsl_Color(int color, iHSLColor hsl) { void setThemePalette_Color (enum iColorTheme theme); -iColor ansi_Color (iRangecc escapeSequence, int fallback); -const char * escape_Color (int color); +iColor ansiForeground_Color (iRangecc escapeSequence, int fallback); +const char * escape_Color (int color); diff --git a/src/ui/text.c b/src/ui/text.c index d4a6b61d..3d208b68 100644 --- a/src/ui/text.c +++ b/src/ui/text.c @@ -80,9 +80,9 @@ iChar char_Glyph(const iGlyph *d) { iDefineTypeConstructionArgs(Glyph, (iChar ch), ch) - /*-----------------------------------------------------------------------------------------------*/ +/*-----------------------------------------------------------------------------------------------*/ - struct Impl_Font { +struct Impl_Font { iBlock * data; stbtt_fontinfo font; float scale; @@ -628,7 +628,8 @@ static iRect run_Font_(iFont *d, enum iRunMode mode, iRangecc text, size_t maxLe if (match_RegExp(text_.ansiEscape, chPos, text.end - chPos, &m)) { if (mode == draw_RunMode) { /* Change the color. */ - const iColor clr = ansi_Color(capturedRange_RegExpMatch(&m, 1), tmParagraph_ColorId); + const iColor clr = + ansiForeground_Color(capturedRange_RegExpMatch(&m, 1), tmParagraph_ColorId); SDL_SetTextureColorMod(text_.cache, clr.r, clr.g, clr.b); } chPos = end_RegExpMatch(&m); -- cgit v1.2.3