/* gray.c 1.1 */ /* Copyright 1992, 1993 O'Reilly and Associates, Inc. Permission to use, copy, and modify this program is hereby granted, as long as this copyright notice appears in each copy of the program source code. */ #include "book_utils.h" #define WIN_GEOM "500x500+50+50" #define NUM_GRAYS 64 static Display *dpy; static Window window; static PEXStructure struct_id; static PEXRenderer renderer; static void redraw() { /* Redraw the window, after first erasing (clearing) it. */ XClearWindow( dpy, window ); PEXRenderNetwork( dpy, window, renderer, struct_id ); XFlush( dpy ); } #define X0 0.1 #define XN 0.9 #define Y0 0.6 #define YN 0.9 #define YP0 0.1 #define YPN 0.4 static void create_structure( num_grays ) int num_grays; { int i, j, k, nr = 4, ng = 4, nb = 4, num_colors; double x, dx, dc; PEXColor color; PEXCoord2D pts[4]; dx = (XN - X0) / num_grays; dc = 1.0 / (num_grays - 1); /* color increment */ /* Create the structure. */ struct_id = PEXCreateStructure( dpy ); /* Set the interior style to solid. */ PEXSetInteriorStyle( dpy, struct_id, PEXOCStore, PEXInteriorStyleSolid ); /* Show the gray scale. */ for ( i = 0, x = X0; i < num_grays; i++, x += dx ) { color.rgb.red = color.rgb.green = color.rgb.blue = i * dc; PEXSetSurfaceColor( dpy, struct_id, PEXOCStore, PEXColorTypeRGB, &color ); pts[0].y = pts[1].y = Y0; pts[2].y = pts[3].y = YN; pts[0].x = pts[3].x = x; pts[1].x = pts[2].x = x + dx; PEXFillArea2D( dpy, struct_id, PEXOCStore, PEXShapeConvex, False, 4, pts ); } /* Create the "colored" scales. */ num_colors = nr * ng * nb; dx = (XN - X0) / num_colors; x = X0; for ( i = 0; i < nr; i++ ) { for ( j = 0; j < ng; j++ ) { for ( k = 0; k < nb; k++ ) { color.rgb.red = i * 1.0 / (nr - 1); color.rgb.green = j * 1.0 / (ng - 1); color.rgb.blue = k * 1.0 / (nb - 1); PEXSetSurfaceColor( dpy, struct_id, PEXOCStore, PEXColorTypeRGB, &color ); pts[0].y = pts[1].y = YP0; pts[2].y = pts[3].y = YPN; pts[0].x = pts[3].x = x; pts[1].x = pts[2].x = x + dx; PEXFillArea2D( dpy, struct_id, PEXOCStore, PEXShapeConvex, False, 4, pts ); x += dx; } } } } main( argc, argv ) int argc; char *argv[]; { XEvent event; XVisualInfo *vis_info, vis_templ; XColor bkgd_color; XStandardColormap cmap_info; PEXColorApproxEntry capx_info; PEXExtensionInfo *pexinfo; PEXRendererAttributes rattrs; int done = 0, num_vis; /* Open a display and initialize PEX. */ dpy = ora_init_pex( argv, &pexinfo ); if ( !dpy ) exit(1); /* Get a GrayScale visual. */ vis_templ.screen = DefaultScreen( dpy ); vis_templ.class = GrayScale; vis_info = XGetVisualInfo( dpy, VisualScreenMask | VisualClassMask , &vis_templ, &num_vis ); if ( num_vis == 0 ) { fprintf ( stderr, "No gray-scale visual.\n" ); exit(1); } /* Create a colormap for the visual. */ if ( !ora_create_gray_map( dpy, vis_info, NUM_GRAYS, &cmap_info, &capx_info ) ) { fprintf ( stderr, "Cannot create a colormap\n" ); exit(1); } /* Create the window, giving it a gray background. */ bkgd_color.red = bkgd_color.green = bkgd_color.blue = 0x5555; bkgd_color.flags = DoRed | DoGreen | DoBlue; window = ora_create_window( dpy, vis_info, &cmap_info, WIN_GEOM, &bkgd_color ); /* Create the renderer and the structure. */ if ( STRUCTURE_SPT( pexinfo ) ) { renderer = ora_setup_renderer( dpy, window, &capx_info, &rattrs ); create_structure( NUM_GRAYS ); } else { fprintf( stderr, "Structures not supported.\n" ); exit(1); } /* Read and respond to X events. */ XSelectInput( dpy, window, ExposureMask | ButtonPressMask ); while ( !done ) { XNextEvent( dpy, &event ); switch ( event.type ) { case Expose: /* Flush remaining Expose events, then redraw. */ while ( XCheckTypedWindowEvent( dpy, window, Expose, &event ) ) ; /* empty statement */ redraw(); break; case ButtonPress: done = 1; break; } } XCloseDisplay( dpy ); return 0; }