9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: schwartz@bio.cse.psu.edu (Scott Schwartz)
To: 9fans@cse.psu.edu
Subject: [9fans] blend demo
Date: Sun, 16 Jul 2000 15:01:02 -0400	[thread overview]
Message-ID: <200007161850.OAA270930@f04n07.cac.psu.edu> (raw)

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


                 reply	other threads:[~2000-07-16 19:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200007161850.OAA270930@f04n07.cac.psu.edu \
    --to=schwartz@bio.cse.psu.edu \
    --cc=9fans@cse.psu.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).