From 2ebc3e2bfef7e388a721113d0edccb551ae52694 Mon Sep 17 00:00:00 2001 From: nick black Date: Sun, 12 Jan 2020 07:42:54 -0500 Subject: [PATCH] blend: conditionally blend in alpha --- include/notcurses.h | 10 ++++++++-- src/demo/witherworm.c | 8 ++++++++ tests/channel.cpp | 7 ++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/include/notcurses.h b/include/notcurses.h index e766c63e5..1856a1be3 100644 --- a/include/notcurses.h +++ b/include/notcurses.h @@ -1117,13 +1117,19 @@ channels_blend(unsigned c1, unsigned c2, unsigned blends){ unsigned rsum, gsum, bsum; if(blends == 0){ // don't just return c2, or you set wide status and all kinds of crap - channel_rgb(c2, &rsum, &gsum, &bsum); - channel_set_rgb(&c1, rsum, gsum, bsum); + if(channel_default_p(c2)){ + channel_set_default(&c1); + }else{ + channel_rgb(c2, &rsum, &gsum, &bsum); + channel_set_rgb(&c1, rsum, gsum, bsum); + } + channel_set_alpha(&c1, channel_alpha(c2)); }else if(!channel_default_p(c2) && !channel_default_p(c1)){ rsum = (channel_r(c1) * blends + channel_r(c2)) / (blends + 1); gsum = (channel_g(c1) * blends + channel_g(c2)) / (blends + 1); bsum = (channel_b(c1) * blends + channel_b(c2)) / (blends + 1); channel_set_rgb(&c1, rsum, gsum, bsum); + channel_set_alpha(&c1, channel_alpha(c2)); } return c1; } diff --git a/src/demo/witherworm.c b/src/demo/witherworm.c index c71900044..4cd3618fb 100644 --- a/src/demo/witherworm.c +++ b/src/demo/witherworm.c @@ -17,6 +17,12 @@ mathplane(struct notcurses* nc){ const int HEIGHT = 9; const int WIDTH = dimx; struct ncplane* n = ncplane_new(nc, HEIGHT, WIDTH, dimy - HEIGHT - 1, dimx - WIDTH, NULL); + cell b = CELL_TRIVIAL_INITIALIZER; + cell_set_fg_rgb(&b, 0xff, 0xff, 0xff); + cell_set_fg_alpha(&b, CELL_ALPHA_BLEND); + cell_set_bg_alpha(&b, CELL_ALPHA_TRANSPARENT); + ncplane_set_base(n, &b); + cell_release(n, &b); ncplane_set_fg_rgb(n, 0xff, 0xff, 0xff); if(n){ ncplane_printf_aligned(n, 0, NCALIGN_RIGHT, "∮E⋅da=Q,n→∞,∑f(i)=∏g(i)⎧⎡⎛┌─────┐⎞⎤⎫"); @@ -67,6 +73,7 @@ surrounding_cells(struct ncplane* n, cell* cells, int y, int x){ static int lightup_surrounding_cells(struct ncplane* n, const cell* cells, int y, int x){ cell c = CELL_TRIVIAL_INITIALIZER; + /* cell_duplicate(n, &c, &cells[0]); lighten(n, &c, 2, y - 1, x - 1); cell_duplicate(n, &c, &cells[1]); @@ -91,6 +98,7 @@ lightup_surrounding_cells(struct ncplane* n, const cell* cells, int y, int x){ lighten(n, &c, 2, y, x - 2); cell_duplicate(n, &c, &cells[11]); lighten(n, &c, 2, y, x + 2); + */ cell_duplicate(n, &c, &cells[12]); lighten(n, &c, 0, y, x); cell_release(n, &c); diff --git a/tests/channel.cpp b/tests/channel.cpp index 5f6996ea7..f1daf9ec5 100644 --- a/tests/channel.cpp +++ b/tests/channel.cpp @@ -143,16 +143,13 @@ TEST_CASE("ChannelBlendDefaultRight") { uint32_t c1 = 0; uint32_t c2 = 0; channel_set_rgb(&c1, 0x80, 0x40, 0x20); + CHECK(!channel_default_p(c1)); CHECK(channel_default_p(c2)); uint32_t c = channels_blend(c1, c2, 0); CHECK(channel_default_p(c)); - unsigned r, g, b; - channel_rgb(c, &r, &g, &b); - CHECK(0 == r); - CHECK(0 == g); - CHECK(0 == b); c = channels_blend(c1, c2, 1); CHECK(!channel_default_p(c)); + unsigned r, g, b; channel_rgb(c, &r, &g, &b); CHECK(0x80 == r); CHECK(0x40 == g);