* [9fans] blend demo
@ 2000-07-16 19:01 Scott Schwartz
0 siblings, 0 replies; only message in thread
From: Scott Schwartz @ 2000-07-16 19:01 UTC (permalink / raw)
To: 9fans
Here's a little program I've been using to exercise alpha channel
blending. It draws a bunch of overlapping circles in various colors
on various backgrounds. I'm not sure the displayed results are
correct, but maybe that's an artifact of my 8-bit display, and it's
pretty slow, but debugging aside (I'm running with some patches to
libdraw from Russ), I thought it would make a nice little example of
the new graphics model.
# To unbundle, run this file
echo blend.c
sed 's/.//' >blend.c <<'//GO.SYSIN DD blend.c'
-#include <u.h>
-#include <libc.h>
-#include <draw.h>
-#include <event.h>
-
-#define RGBA rgba
-#define MIN(a,b) ((a)<(b)?(a):(b))
-
-int rgba(int r, int g, int b, int a)
-{
- int c;
-
- r = (r*a)/255;
- g = (g*a)/255;
- b = (b*a)/255;
- c= (r<<24) | (g<<16) | (b<<8) | (a);
- return c;
-}
-
-enum { Nbg = 3 };
-Image *fg[2][2][3];
-Image *bg[Nbg];
-
-void main(int argc, char *argv[])
-{
- Event e;
- Rectangle r1 = Rect(0,0,1,1);
- Image **i;
-
- USED(argc, argv);
-
- if (initdraw(nil, nil, "blend") < 0) {
- fprint(2, "initdraw failed: %r\n");
- exits("initdraw");
- }
- einit(Emouse|Ekeyboard);
-
- bg[0] = allocimage(display, r1, RGBA32, 1, DBlack);
- bg[1] = allocimage(display, r1, RGBA32, 1, DWhite);
- bg[2] = allocimage(display, r1, RGBA32, 1, RGBA(0,0,0,64));
-
- // Pure CMY
- i = fg[0][0];
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,255,0,255));
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,255,255,255));
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,0,255,255));
- // 50% CMY
- i = fg[0][1];
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,255,0,127));
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,255,255,127));
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,0,255,127));
- // Pure RGB
- i = fg[1][0];
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,0,0,255));
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,255,0,255));
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,0,255,255));
- // 50% RGB
- i = fg[1][1];
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,0,0,127));
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,255,0,127));
- *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,0,255,127));
-
- eresized(0);
- for (;;) {
- switch(event(&e)){
- case Ekeyboard:
- switch(e.kbdc){
- case 'q':
- case '\04':
- exits(nil);
- }
- break;
- case Emouse:
- if(e.mouse.buttons){
- }
- break;
- }
- }
- exits(nil);
-}
-
-void spots(Point o, int R, Image *bg, Image **fg)
-{
- Rectangle r = Rpt(o, addpt(o, Pt(3*R, 3*R)));
-
- draw(screen, r, bg, nil, ZP);
-
- fillellipse(screen, addpt(o,Pt(1*R,2*R)), R,R, fg[0], ZP);
- fillellipse(screen, addpt(o,Pt(2*R,2*R)), R,R, fg[1], ZP);
- fillellipse(screen, addpt(o,Pt(1.5*R,1*R)), R,R, fg[2], ZP);
-}
-
-void redraw(Image *screen)
-{
- Rectangle r = screen->r;
- int dX = r.max.x-r.min.x;
- int dY = r.max.y-r.min.y;
- int R = MIN(dX,dY)/4/3;
- int x, y;
-
- fprint(2, "s=%R, R=%d\n", screen->r, R);
- // draw(screen, r, bg[2], nil, ZP);
-
- for (x=0; x<4; ++x)
- for (y=0; y<4; ++y)
- spots(addpt(r.min, Pt(x*3*R, y*3*R)), R,
- bg[(x<2)+(y<2)], fg[y%2][x%2]);
- //flushimage(display, 1);
-}
-
-void
-eresized(int new)
-{
- if(new && getwindow(display, Refnone) < 0){
- fprint(2, "can't reattach to window: %r\n");
- exits("attach");
- }
- redraw(screen);
-}
//GO.SYSIN DD blend.c
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2000-07-16 19:01 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-07-16 19:01 [9fans] blend demo Scott Schwartz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).