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.
This commit is contained in:
Glen Whitney 2019-08-03 12:42:32 -04:00
parent 91b5ae7e1f
commit 07bf78c7bb
5 changed files with 22 additions and 30 deletions

View File

@ -7,6 +7,9 @@
#include "eval.h" #include "eval.h"
#include "main.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 /* initcellcontents - make a fresh cell into the "empty" one; don't worry
about freeing anything there, that will have been handled. */ about freeing anything there, that will have been handled. */
void initcellcontents(Cell *fresh) void initcellcontents(Cell *fresh)
@ -18,8 +21,8 @@ void initcellcontents(Cell *fresh)
fresh->shadowed=0; fresh->shadowed=0;
fresh->bold=0; fresh->bold=0;
fresh->underline=0; fresh->underline=0;
fresh->foreground = DEF_FG_NUM; for (ColorAspect a = FOREGROUND; a < NUM_COLOR_ASPECTS; ++a)
fresh->background = DEF_BG_NUM; fresh->aspect[a] = DefaultCN[a];
fresh->scientific=DEF_SCIENTIFIC; fresh->scientific=DEF_SCIENTIFIC;
fresh->value.type=EMPTY; fresh->value.type=EMPTY;
fresh->resvalue.type=EMPTY; fresh->resvalue.type=EMPTY;
@ -60,16 +63,10 @@ bool isbold(const Cell* cell)
return (cell != NULLCELL) && cell->bold; return (cell != NULLCELL) && cell->bold;
} }
/* cell_bg -- background of cell */ /* getcolor -- a color associated to cell */
ColorNum cell_bg(const Cell* cell) ColorNum getcolor(const Cell* cell, ColorAspect aspect)
{ {
return cell == NULLCELL ? DEF_BG_NUM : cell->background; return cell == NULLCELL ? DefaultCN[aspect] : cell->aspect[aspect];
}
/* cell_fg -- foreground of cell */
ColorNum cell_fg(const Cell* cell)
{
return cell == NULLCELL ? DEF_FG_NUM : cell->foreground;
} }
/* isunderline -- is cell underlined? */ /* isunderline -- is cell underlined? */

View File

@ -15,6 +15,9 @@ typedef unsigned char ColorNum;
#define MAX_MAX_COLORS UCHAR_MAX; #define MAX_MAX_COLORS UCHAR_MAX;
typedef enum { FOREGROUND = 0, BACKGROUND, NUM_COLOR_ASPECTS } ColorAspect;
extern const ColorNum DefaultCN[];
typedef struct typedef struct
{ {
Token **contents[CONTINGENT]; Token **contents[CONTINGENT];
@ -23,8 +26,7 @@ typedef struct
Token resvalue; Token resvalue;
Adjust adjust; Adjust adjust;
int precision; int precision;
ColorNum foreground; ColorNum aspect[NUM_COLOR_ASPECTS];
ColorNum background;
unsigned int updated:1; unsigned int updated:1;
unsigned int shadowed:1; unsigned int shadowed:1;
unsigned int scientific:1; unsigned int scientific:1;
@ -45,8 +47,7 @@ Adjust getadjust(const Cell *cell);
bool shadowed(const Cell *cell); bool shadowed(const Cell *cell);
bool isbold(const Cell *cell); bool isbold(const Cell *cell);
bool underlined(const Cell *cell); bool underlined(const Cell *cell);
ColorNum cell_bg(const Cell *cell); ColorNum getcolor(const Cell *cell, ColorAspect aspect);
ColorNum cell_fg(const Cell *cell);
bool locked(const Cell *cell); bool locked(const Cell *cell);
bool transparent(const Cell *cell); bool transparent(const Cell *cell);
bool ignored(const Cell *cell); bool ignored(const Cell *cell);

View File

@ -12,12 +12,6 @@
/* default is no scientific notation for numbers */ /* default is no scientific notation for numbers */
#define DEF_SCIENTIFIC 0 #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 */ /* character attribute for cell and row numbers */
#define DEF_NUMBER A_BOLD #define DEF_NUMBER A_BOLD

View File

@ -366,8 +366,8 @@ void display_init(Sheet *cursheet, int always_redraw)
{ {
initscr(); initscr();
start_color(); start_color();
init_color(DEF_BG_NUM, CHANNEL_MAX, CHANNEL_MAX, CHANNEL_MAX); init_color(DefaultCN[BACKGROUND], CHANNEL_MAX, CHANNEL_MAX, CHANNEL_MAX);
assume_default_colors(COLOR_BLACK, DEF_BG_NUM); assume_default_colors(COLOR_BLACK, DefaultCN[BACKGROUND]);
if (debug_level > 1) if (debug_level > 1)
printf("Terminal has colors: %d, #colors:%d, #pairs:%d", printf("Terminal has colors: %d, #colors:%d, #pairs:%d",
has_colors(), COLORS, COLOR_PAIRS); 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)); cursheet->palette = (void *)malloc(cursheet->max_colors*sizeof(CursesColor));
memset(cursheet->palette, '\0', cursheet->max_colors*sizeof(CursesColor)); memset(cursheet->palette, '\0', cursheet->max_colors*sizeof(CursesColor));
CursesColor *palt = (CursesColor *)(cursheet->palette); 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])); (void)color_content(i, &(palt[i][0]), &(palt[i][1]), &(palt[i][2]));
} }
@ -453,7 +453,7 @@ void redraw_sheet(Sheet *sheet)
} while (again); } while (again);
unsigned short curcp = 1; unsigned short curcp = 1;
init_pair(curcp, DEF_FG_NUM, COLOR_YELLOW); init_pair(curcp, DefaultCN[FOREGROUND], COLOR_YELLOW);
if (header) { if (header) {
(void)wattron(stdscr,DEF_NUMBER); (void)wattron(stdscr,DEF_NUMBER);

View File

@ -14,7 +14,7 @@ decl {\#include <stdint.h>} {private global}
decl {\#include <limits.h>} {private global} decl {\#include <limits.h>} {private global}
decl {\#include <fcntl.h>} {private global} decl {\#include <fcntl.h>} {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} {private global}
decl {\#define shadow _shadow} {private global} decl {\#define shadow _shadow} {private global}
decl {\#define transparent _transparent} {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]); bool iscurrent = (C == cursheet->cur[X] && R == cursheet->cur[Y]);
Cell *cell = safe_cell_at(cursheet, test); Cell *cell = safe_cell_at(cursheet, test);
Fl_Color cellbg = 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 (selected) cellbg = tpt_selection_version(cellbg);
if (!LOC_WITHIN(cursheet,test)) if (!LOC_WITHIN(cursheet,test))
cellbg = fl_color_average(cellbg, FL_BLACK, 0.97); 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_push_clip(xx+3, yy+3, W-6, H-6);
Fl_Color cellfg = Fl_Color cellfg =
((Fl_Color *)(cursheet->palette))[cell_fg(cell)]; ((Fl_Color *)(cursheet->palette))[getcolor(cell, FOREGROUND)];
fl_color(cellfg); fl_color(cellfg);
fl_font(FL_HELVETICA | (isbold(cell) ? FL_BOLD : 0), 14); fl_font(FL_HELVETICA | (isbold(cell) ? FL_BOLD : 0), 14);
@ -914,8 +914,8 @@ Function
/* allocate and initialize the palette */ /* allocate and initialize the palette */
Fl_Color* palt = new Fl_Color[sheet->max_colors]; Fl_Color* palt = new Fl_Color[sheet->max_colors];
sheet->palette = (void *) palt; sheet->palette = (void *) palt;
palt[DEF_FG_NUM] = FL_FOREGROUND_COLOR; palt[DefaultCN[FOREGROUND]] = FL_FOREGROUND_COLOR;
palt[DEF_BG_NUM] = FL_BACKGROUND2_COLOR; palt[DefaultCN[BACKGROUND]] = FL_BACKGROUND2_COLOR;
ColorNum c = 0; SKIPDEFCOLS(c); ColorNum c = 0; SKIPDEFCOLS(c);
palt[c++] = FL_BACKGROUND_COLOR; SKIPDEFCOLS(c); palt[c++] = FL_BACKGROUND_COLOR; SKIPDEFCOLS(c);
palt[c++] = FL_INACTIVE_COLOR; SKIPDEFCOLS(c); palt[c++] = FL_INACTIVE_COLOR; SKIPDEFCOLS(c);