From 07bf78c7bb52a3ab3afb11af37614c9750a7026e Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Sat, 3 Aug 2019 12:42:32 -0400 Subject: [PATCH] More color prep: define color aspects of a cell This change does away with separate attributes foreground and background, opting for an array of color "aspects", where the aspect can vary over an enum which can either be FOREGROUND or BACKGROUND. This will allow writing a single set of color-editing code which can work either on the foreground or background color of a cell. --- src/common/cell.c | 19 ++++++++----------- src/common/cell.h | 9 +++++---- src/common/default.h | 6 ------ src/display.c | 8 ++++---- src/fteapot.fl | 10 +++++----- 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/common/cell.c b/src/common/cell.c index efb0c8e..57bf9a1 100644 --- a/src/common/cell.c +++ b/src/common/cell.c @@ -7,6 +7,9 @@ #include "eval.h" #include "main.h" +const ColorNum DefaultCN[] = + { [FOREGROUND] = 0, [BACKGROUND] = 16, [NUM_COLOR_ASPECTS] = 255 }; + /* initcellcontents - make a fresh cell into the "empty" one; don't worry about freeing anything there, that will have been handled. */ void initcellcontents(Cell *fresh) @@ -18,8 +21,8 @@ void initcellcontents(Cell *fresh) fresh->shadowed=0; fresh->bold=0; fresh->underline=0; - fresh->foreground = DEF_FG_NUM; - fresh->background = DEF_BG_NUM; + for (ColorAspect a = FOREGROUND; a < NUM_COLOR_ASPECTS; ++a) + fresh->aspect[a] = DefaultCN[a]; fresh->scientific=DEF_SCIENTIFIC; fresh->value.type=EMPTY; fresh->resvalue.type=EMPTY; @@ -60,16 +63,10 @@ bool isbold(const Cell* cell) return (cell != NULLCELL) && cell->bold; } -/* cell_bg -- background of cell */ -ColorNum cell_bg(const Cell* cell) +/* getcolor -- a color associated to cell */ +ColorNum getcolor(const Cell* cell, ColorAspect aspect) { - return cell == NULLCELL ? DEF_BG_NUM : cell->background; -} - -/* cell_fg -- foreground of cell */ -ColorNum cell_fg(const Cell* cell) -{ - return cell == NULLCELL ? DEF_FG_NUM : cell->foreground; + return cell == NULLCELL ? DefaultCN[aspect] : cell->aspect[aspect]; } /* isunderline -- is cell underlined? */ diff --git a/src/common/cell.h b/src/common/cell.h index b3a267c..85cfc28 100644 --- a/src/common/cell.h +++ b/src/common/cell.h @@ -15,6 +15,9 @@ typedef unsigned char ColorNum; #define MAX_MAX_COLORS UCHAR_MAX; +typedef enum { FOREGROUND = 0, BACKGROUND, NUM_COLOR_ASPECTS } ColorAspect; +extern const ColorNum DefaultCN[]; + typedef struct { Token **contents[CONTINGENT]; @@ -23,8 +26,7 @@ typedef struct Token resvalue; Adjust adjust; int precision; - ColorNum foreground; - ColorNum background; + ColorNum aspect[NUM_COLOR_ASPECTS]; unsigned int updated:1; unsigned int shadowed:1; unsigned int scientific:1; @@ -45,8 +47,7 @@ Adjust getadjust(const Cell *cell); bool shadowed(const Cell *cell); bool isbold(const Cell *cell); bool underlined(const Cell *cell); -ColorNum cell_bg(const Cell *cell); -ColorNum cell_fg(const Cell *cell); +ColorNum getcolor(const Cell *cell, ColorAspect aspect); bool locked(const Cell *cell); bool transparent(const Cell *cell); bool ignored(const Cell *cell); diff --git a/src/common/default.h b/src/common/default.h index 332109b..90777e0 100644 --- a/src/common/default.h +++ b/src/common/default.h @@ -12,12 +12,6 @@ /* default is no scientific notation for numbers */ #define DEF_SCIENTIFIC 0 -/* Default ColorNum for foreground */ -#define DEF_FG_NUM 0 - -/* Default ColorNum for background */ -#define DEF_BG_NUM 16 - /* character attribute for cell and row numbers */ #define DEF_NUMBER A_BOLD diff --git a/src/display.c b/src/display.c index 736a19b..0ae3a09 100644 --- a/src/display.c +++ b/src/display.c @@ -366,8 +366,8 @@ void display_init(Sheet *cursheet, int always_redraw) { initscr(); start_color(); - init_color(DEF_BG_NUM, CHANNEL_MAX, CHANNEL_MAX, CHANNEL_MAX); - assume_default_colors(COLOR_BLACK, DEF_BG_NUM); + init_color(DefaultCN[BACKGROUND], CHANNEL_MAX, CHANNEL_MAX, CHANNEL_MAX); + assume_default_colors(COLOR_BLACK, DefaultCN[BACKGROUND]); if (debug_level > 1) printf("Terminal has colors: %d, #colors:%d, #pairs:%d", has_colors(), COLORS, COLOR_PAIRS); @@ -386,7 +386,7 @@ void display_init(Sheet *cursheet, int always_redraw) cursheet->palette = (void *)malloc(cursheet->max_colors*sizeof(CursesColor)); memset(cursheet->palette, '\0', cursheet->max_colors*sizeof(CursesColor)); CursesColor *palt = (CursesColor *)(cursheet->palette); - for (size_t i = 0; i <= DEF_BG_NUM; ++i) + for (size_t i = 0; i <= DefaultCN[BACKGROUND]; ++i) (void)color_content(i, &(palt[i][0]), &(palt[i][1]), &(palt[i][2])); } @@ -453,7 +453,7 @@ void redraw_sheet(Sheet *sheet) } while (again); unsigned short curcp = 1; - init_pair(curcp, DEF_FG_NUM, COLOR_YELLOW); + init_pair(curcp, DefaultCN[FOREGROUND], COLOR_YELLOW); if (header) { (void)wattron(stdscr,DEF_NUMBER); diff --git a/src/fteapot.fl b/src/fteapot.fl index 1907bb1..f5a245a 100644 --- a/src/fteapot.fl +++ b/src/fteapot.fl @@ -14,7 +14,7 @@ decl {\#include } {private global} decl {\#include } {private global} decl {\#include } {private global} -decl {\#define SKIPDEFCOLS(c) while (c == DEF_BG_NUM || c == DEF_FG_NUM) ++c} +decl {\#define SKIPDEFCOLS(c) while (c == DefaultCN[FOREGROUND] || c == DefaultCN[BACKGROUND]) ++c} {private global} decl {\#define shadow _shadow} {private global} decl {\#define transparent _transparent} {private global} @@ -144,7 +144,7 @@ class TeapotTable {open : {public Fl_Table}} bool iscurrent = (C == cursheet->cur[X] && R == cursheet->cur[Y]); Cell *cell = safe_cell_at(cursheet, test); Fl_Color cellbg = - ((Fl_Color *)(cursheet->palette))[cell_bg(cell)]; + ((Fl_Color *)(cursheet->palette))[getcolor(cell, BACKGROUND)]; if (selected) cellbg = tpt_selection_version(cellbg); if (!LOC_WITHIN(cursheet,test)) cellbg = fl_color_average(cellbg, FL_BLACK, 0.97); @@ -156,7 +156,7 @@ class TeapotTable {open : {public Fl_Table}} fl_push_clip(xx+3, yy+3, W-6, H-6); Fl_Color cellfg = - ((Fl_Color *)(cursheet->palette))[cell_fg(cell)]; + ((Fl_Color *)(cursheet->palette))[getcolor(cell, FOREGROUND)]; fl_color(cellfg); fl_font(FL_HELVETICA | (isbold(cell) ? FL_BOLD : 0), 14); @@ -914,8 +914,8 @@ Function /* allocate and initialize the palette */ Fl_Color* palt = new Fl_Color[sheet->max_colors]; sheet->palette = (void *) palt; - palt[DEF_FG_NUM] = FL_FOREGROUND_COLOR; - palt[DEF_BG_NUM] = FL_BACKGROUND2_COLOR; + palt[DefaultCN[FOREGROUND]] = FL_FOREGROUND_COLOR; + palt[DefaultCN[BACKGROUND]] = FL_BACKGROUND2_COLOR; ColorNum c = 0; SKIPDEFCOLS(c); palt[c++] = FL_BACKGROUND_COLOR; SKIPDEFCOLS(c); palt[c++] = FL_INACTIVE_COLOR; SKIPDEFCOLS(c);