diff --git a/data/blackmage.png b/data/blackmage.png new file mode 100644 index 000000000..6fd789af0 Binary files /dev/null and b/data/blackmage.png differ diff --git a/include/notcurses.h b/include/notcurses.h index 0284bda1e..81b55699c 100644 --- a/include/notcurses.h +++ b/include/notcurses.h @@ -2005,6 +2005,9 @@ palette256_get(const palette256* p, int idx, unsigned* RESTRICT r, unsigned* RES // Free the palette store 'p'. API void palette256_free(palette256* p); +// Convert the plane's content to greyscale. +API void ncplane_greyscale(struct ncplane* n); + #undef API #ifdef __cplusplus diff --git a/src/demo/panelreel.c b/src/demo/panelreel.c index 21566fdb3..5fa200249 100644 --- a/src/demo/panelreel.c +++ b/src/demo/panelreel.c @@ -380,6 +380,7 @@ panelreel_demo_core(struct notcurses* nc, int efdr, int efdw){ int panelreel_demo(struct notcurses* nc){ int pipes[2]; + ncplane_greyscale(notcurses_stdplane(nc)); // freebsd doesn't have eventfd :/ if(pipe2(pipes, O_CLOEXEC | O_NONBLOCK)){ fprintf(stderr, "Error creating pipe (%s)\n", strerror(errno)); diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index 0a5dc9465..d9d19fa33 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -1703,3 +1703,39 @@ int palette256_use(notcurses* nc, const palette256* p){ void palette256_free(palette256* p){ free(p); } + +// Given r, g, and b values 0..255, do a weighted average per Rec. 601, and +// return the 8-bit greyscale value (this value will be the r, g, and b value +// for the new color). +static inline int +rgb_greyscale(int r, int g, int b){ + if(r < 0 || r > 255){ + return -1; + } + if(g < 0 || g > 255){ + return -1; + } + if(b < 0 || b > 255){ + return -1; + } + float fg = (0.2126 * (r / 255.0) + 0.7152 * (g / 255.0) + 0.0722 * (b / 255.0)); + return fg * 255; +} + +void ncplane_greyscale(ncplane *n){ + ncplane_lock(n); + for(int y = 0 ; y < n->leny ; ++y){ + for(int x = 0 ; x < n->lenx ; ++x){ + cell* c = &n->fb[nfbcellidx(n, y, x)]; + unsigned r, g, b; + cell_fg_rgb(c, &r, &g, &b); + // Use Rec. 601 scaling + int gy = rgb_greyscale(r, g, b); + cell_set_fg_rgb(c, gy, gy, gy); + cell_bg_rgb(c, &r, &g, &b); + gy = rgb_greyscale(r, g, b); + cell_set_bg_rgb(c, gy, gy, gy); + } + } + ncplane_unlock(n); +}