From 827d758c19cced0ae4d2b7803d53873e403e1c1a Mon Sep 17 00:00:00 2001 From: nick black Date: Sun, 13 Dec 2020 08:54:59 -0500 Subject: [PATCH] properly color progbar #1202 --- include/notcurses/notcurses.h | 6 +----- src/lib/internal.h | 3 +-- src/lib/progbar.c | 29 ++++++----------------------- src/poc/progbar.c | 1 + 4 files changed, 9 insertions(+), 30 deletions(-) diff --git a/include/notcurses/notcurses.h b/include/notcurses/notcurses.h index 40e4c1922..234e76f7f 100644 --- a/include/notcurses/notcurses.h +++ b/include/notcurses/notcurses.h @@ -3032,11 +3032,7 @@ API int ncmenu_destroy(struct ncmenu* n); #define NCPROGBAR_OPTION_RETROGRADE 0x0001u // proceed left/down typedef struct ncprogbar_options { - // channels for the maximum and minimum points. linear interpolation will be - // applied across the domain between these two. for both channels, both min - // and max must either be RGB, or both default, and alphas must match. - uint64_t maxchannels; - uint64_t minchannels; + uint64_t channels; // channels for the progress bar uint64_t flags; } ncprogbar_options; diff --git a/src/lib/internal.h b/src/lib/internal.h index c8dc310c2..926779052 100644 --- a/src/lib/internal.h +++ b/src/lib/internal.h @@ -224,8 +224,7 @@ typedef struct ncmenu { typedef struct ncprogbar { ncplane* ncp; double progress; // on the range [0, 1] - uint64_t maxchannels; - uint64_t minchannels; + uint64_t channels; // channels for the drawn bar bool retrograde; } ncprogbar; diff --git a/src/lib/progbar.c b/src/lib/progbar.c index c3df8f0a0..a9ed270d2 100644 --- a/src/lib/progbar.c +++ b/src/lib/progbar.c @@ -5,21 +5,14 @@ ncprogbar* ncprogbar_create(ncplane* n, const ncprogbar_options* opts){ ncprogbar_options default_opts; if(opts == NULL){ memset(&default_opts, 0, sizeof(default_opts)); - default_opts.minchannels = CHANNELS_RGB_INITIALIZER(0x3d, 0x3d, 0x3d, 0, 0, 0); - default_opts.maxchannels = CHANNELS_RGB_INITIALIZER(0xe0, 0xee, 0xe0, 0, 0, 0); opts = &default_opts; } - if(check_gradient_args(opts->minchannels, opts->maxchannels, opts->minchannels, opts->maxchannels)){ - logerror(ncplane_notcurses(n), "Illegal progbar colors\n"); - return NULL; - } if(opts->flags > (NCPROGBAR_OPTION_RETROGRADE << 1u)){ logwarn(ncplane_notcurses(n), "Invalid flags %016lx\n", opts->flags); } ncprogbar* ret = malloc(sizeof(*ret)); ret->ncp = n; - ret->maxchannels = opts->maxchannels; - ret->minchannels = opts->minchannels; + ret->channels = opts->channels; ret->retrograde = opts->flags & NCPROGBAR_OPTION_RETROGRADE; return ret; } @@ -30,35 +23,25 @@ ncplane* ncprogbar_plane(ncprogbar* n){ static int progbar_redraw(ncprogbar* n){ - enum { - DIR_UP, - DIR_RIGHT, - DIR_DOWN, - DIR_LEFT - } direction; // get current dimensions; they might have changed int dimy, dimx; ncplane_dim_yx(ncprogbar_plane(n), &dimy, &dimx); - if(dimx > dimy){ - direction = n->retrograde ? DIR_LEFT : DIR_RIGHT; - }else{ - direction = n->retrograde ? DIR_DOWN : DIR_UP; - } - const bool horizontal = (direction == DIR_LEFT || direction == DIR_RIGHT); + const bool horizontal = dimx > dimy; int delt, range; if(horizontal){ range = dimx; - delt = n->retrograde ? 1 : -1; + delt = -1; }else{ range = dimy; - delt = n->retrograde ? -1 : 1; + delt = 1; } double progress = n->progress * range; if(n->retrograde){ progress = range - progress; + delt *= -1; } + ncplane_set_channels(ncprogbar_plane(n), n->channels); while(progress > 0 && progress < range){ - // FIXME lerp min->max if(ncplane_putegc_yx(ncprogbar_plane(n), 0, progress, "█", NULL) <= 0){ return -1; } diff --git a/src/poc/progbar.c b/src/poc/progbar.c index aead90471..40125443d 100644 --- a/src/poc/progbar.c +++ b/src/poc/progbar.c @@ -54,6 +54,7 @@ pbar_make(struct notcurses* nc, uint64_t flags){ struct ncprogbar_options popts = { .flags = flags, }; + channels_set_fg_rgb8(&popts.channels, 0x80, 0x22, 0x22); struct ncprogbar* ncp = ncprogbar_create(pbar, &popts); if(ncp == NULL){ return NULL;