properly color progbar #1202

This commit is contained in:
nick black 2020-12-13 08:54:59 -05:00 committed by Nick Black
parent d56ea8b5e7
commit 827d758c19
4 changed files with 9 additions and 30 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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;