Guest User

Untitled

a guest
Jun 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. /*
  2. File: COLORS.C
  3. Author: Joshua Marantz, Viewlogic Systems, Inc.
  4. Date: 5/19/88
  5. Env: Unix/X11
  6.  
  7. This program demonstrates some basic raster drawing operations. It uses
  8. a simple 8-color scheme with a map that is arranged to yield intuitive
  9. effects when raster operations such as OR are used. This program should
  10. work correctly on a monochrome system, or any color system where it is
  11. possible to allocate 3 planes. The program draws three solid boxes in OR
  12. mode: one red, one green, one blue. They intersect in a manner such that
  13. all 7 colors should be visible. It then does several operations, waiting
  14. for a standard keyboard hit (using getchar ()) in between each one:
  15.  
  16. 1. Redraws green box in GXandInverted. This should leave
  17. the red and blue boxes untouched on a color system.
  18. 2. Redraws green box in OR mode again.
  19. 3. Redraws red box in GXandInverted.
  20. 4. Redraws red box in OR mode.
  21. 5. Redraws blue box in GXandInverted.
  22. 6. Redraws blue box in OR mode. The window should now
  23. be just as it was when the boxes were initially drawn.
  24. 7. In XOR mode, draw a blue solid box over the whole window.
  25. 8. Redraw blue solid box, thus undoing effects of #7.
  26. 9-20. Repeat 7-8 for green,cyan,red,magenta,yellow,white.
  27.  
  28. On a color system, the rectangles drawn using GXandInverted will erase the
  29. rectangle in the foreground color without trashing the rectangles drawn in
  30. the other two colors. This is because the pixel values are bitwise mutually
  31. exclusive.
  32.  
  33. On a monochrome system, the rectangles drawn in GXandInverted mode will
  34. erase the corresponding rectangle on the screen, but it will also trash
  35. the other two. This is because all the rectangles were drawn in color 1,
  36. and thus are not bitwise mutually exclusive.
  37.  
  38. Another problem with the monochrome system is that it is not
  39. straightforward to specify a black background and a white foreground.
  40. In the "initmono" procedure, the color map values are explicitly set
  41. to pixel values 0 and 1. In order for the OR-mode drawing to work
  42. correctly, the background must be 0, and the foreground non-zero. If
  43. BlackPixel is 1 and WhitePixel is 0, then we must use different raster
  44. operations to achieve the same effect.
  45.  
  46. Thus we must set up a mapping for raster operations:
  47.  
  48. Desired Operation Black==0 rasterop Black==1 rasterop
  49.  
  50. DRAW GXor GXand
  51. ERASE GXandInverted GXorInverted
  52. HIGHLIGHT GXxor GXequiv
  53. */
  54.  
  55. #include <stdio.h>
  56. #include <X11/Xlib.h>
  57. #include <X11/Xutil.h>
  58.  
  59. #define BLACK 0
  60. #define BLUE 1
  61. #define GREEN 2
  62. #define CYAN 3
  63. #define RED 4
  64. #define MAGENTA 5
  65. #define YELLOW 6
  66. #define WHITE 7
  67.  
  68. #define FALSE 0
  69. #define TRUE 1
  70.  
  71. static char *color_strings[] = {"black", "blue", "green", "cyan",
  72. "red", "magenta", "yellow", "white"};
  73. static int color_map[8];
  74.  
  75. /* Set up the rectangles so they intersect in interesting ways. */
  76. XRectangle red = {10, 10, 300, 400};
  77. XRectangle green = {10, 250, 450, 100};
  78. XRectangle blue = {200, 300, 300, 250};
  79. XRectangle big = {0, 0, 500, 500};
  80.  
  81. /* These arrays make it easier to do things to each primary color */
  82. static int primary_colors[] = {RED, GREEN, BLUE};
  83. static XRectangle *primary_rects[] = {&red, &green, &blue};
  84.  
  85. /* Raster operation mapping */
  86. #define DRAW 0
  87. #define ERASE 1
  88. #define HIGHLIGHT 2
  89.  
  90. /* Raster operations must be specified differently for mono */
  91. static unsigned long raster_map_normal[] = {GXor, GXandInverted, GXxor};
  92. static unsigned long raster_map_reverse[] = {GXand, GXorInverted, GXequiv};
  93.  
  94. /* The default raster operations are OK if we can set the color map */
  95. static unsigned long *raster_map = raster_map_normal;
  96.  
  97. /*
  98. This utility function maps a color number between 0 and 7 into
  99. an X pixel value, which can be used as an argument to
  100. XSetForeground or as the .pixel member of the XColor structure.
  101. */
  102. static unsigned int xlate_color(color_number, plane_mask, pixel)
  103. unsigned int color_number;
  104. unsigned int plane_mask[];
  105. unsigned int pixel[];
  106. {
  107. return (((color_number & 0x1) ? plane_mask[0] : 0) |
  108. ((color_number & 0x2) ? plane_mask[1] : 0) |
  109. ((color_number & 0x4) ? plane_mask[2] : 0) |
  110. pixel[0]);
  111. }
  112.  
  113. /* Allocate colors in an X color map and set them up. */
  114. unsigned int initcolor(dpy, scrn)
  115. Display *dpy;
  116. int scrn;
  117. {
  118. Colormap map;
  119. int status;
  120. unsigned int plane_mask[3], pixel[1];
  121. int i;
  122. XColor colors[8];
  123.  
  124. map = DefaultColormap (dpy, scrn);
  125. status = XAllocColorCells (dpy, map, FALSE,
  126. plane_mask, 3, pixel, 1);
  127. printf ("status = %d\n", status);
  128. XInstallColormap (dpy, map);
  129.  
  130. /* Initialize the X color structure and store it in the map. */
  131. for (i = 0; i < 8; i++) {
  132. XParseColor (dpy, map, color_strings[i], &colors[i]);
  133. colors[i].pixel = color_map[i] = xlate_color (i, plane_mask, pixel);
  134. colors[i].flags = DoRed | DoGreen | DoBlue;
  135. }
  136. XStoreColors (dpy, map, colors, 8);
  137.  
  138. return (plane_mask[0] | plane_mask[1] | plane_mask[2]);
  139. }
  140.  
  141. /*
  142. Set up the mapping of color numbers into monochrome values.
  143. Change the raster operations to the logically opposite functions
  144. if black is pixel #1.
  145. */
  146. static unsigned int initmono(dpy, scrn)
  147. Display *dpy;
  148. int scrn;
  149. {
  150. int i;
  151.  
  152. color_map[BLACK] = BlackPixel (dpy, scrn);
  153. for (i = 1; i < 8; i++)
  154. color_map[i] = WhitePixel (dpy, scrn);
  155.  
  156. /* If the background color is not color 0, then change raster maps */
  157. if (color_map[BLACK] != 0)
  158. raster_map = raster_map_reverse;
  159.  
  160. return (AllPlanes);
  161. }
  162.  
  163. /*
  164. Draw a rectangle in the specified color and raster operation. This
  165. routine is inefficient because it flushes after each graphics call,
  166. but it is useful for debugging and demos.
  167. */
  168. static void drawrect(dpy, win, gc, r, color, rasterop)
  169. Display *dpy;
  170. Window win;
  171. GC gc;
  172. XRectangle *r;
  173. unsigned int color, rasterop;
  174. {
  175. XSetForeground (dpy, gc, color_map[color]);
  176. XSetFunction (dpy, gc, raster_map[rasterop]);
  177. XFillRectangle (dpy, win, gc, r -> x, r -> y, r -> width, r -> height);
  178. XFlush (dpy);
  179. }
  180.  
  181. /*
  182. Main procedure: initialize X display, set up monochrome or color
  183. mapping, create a window, wait for it to be mapped, draw rectangles
  184. in various forms, and exit.
  185. */
  186. int main() {
  187. Display *dpy;
  188. Window win;
  189. int scrn;
  190. GC gc;
  191. unsigned int plane_mask;
  192. XGCValues v;
  193. XEvent e;
  194. int i;
  195.  
  196. /* Connect to the display and init the color map */
  197. if ((dpy = XOpenDisplay (NULL)) == NULL) {
  198. fprintf (stderr, "Could not connect to default X display.\n");
  199. exit (1);
  200. }
  201. scrn = DefaultScreen (dpy);
  202. if (DisplayPlanes (dpy, scrn) >= 3)
  203. plane_mask = initcolor (dpy, scrn);
  204. else
  205. plane_mask = initmono (dpy, scrn);
  206.  
  207. /* Create and map the window. */
  208. win = XCreateSimpleWindow (dpy, DefaultRootWindow (dpy),
  209. 100, 100, 500, 500,
  210. 1, /* Border width */
  211. color_map[WHITE],
  212. color_map[BLACK]);
  213. XMapWindow (dpy, win);
  214. XSetWindowColormap (dpy, win, DefaultColormap (dpy, scrn));
  215.  
  216. /* Set up the graphics context for the plane mask and fill style */
  217. v.plane_mask = plane_mask;
  218. v.fill_style = FillSolid;
  219. gc = XCreateGC (dpy, win, GCPlaneMask | GCFillStyle, &v);
  220.  
  221. /* Wait for the window to be mapped. */
  222. XSelectInput (dpy, win, ExposureMask);
  223. XNextEvent (dpy, &e);
  224.  
  225. /* Draw the initial filled rectangles. */
  226. for (i = 0; i < 3; i++)
  227. drawrect (dpy, win, gc, primary_rects[i], primary_colors[i], DRAW);
  228.  
  229. /* 1-6: For each primary color, erase its box and redraw it. */
  230. for (i = 0; i < 3; i++) {
  231. getchar ();
  232. drawrect (dpy, win, gc, primary_rects[i], primary_colors[i], ERASE);
  233. getchar ();
  234. drawrect (dpy, win, gc, primary_rects[i], primary_colors[i], DRAW);
  235. }
  236.  
  237. /* 7-20: For each of the colors, XOR a big box over the window twice. */
  238. for (i = 0; i < 8; i++) {
  239. getchar ();
  240. drawrect (dpy, win, gc, &big, i, HIGHLIGHT);
  241. getchar ();
  242. drawrect (dpy, win, gc, &big, i, HIGHLIGHT);
  243. }
  244. getchar ();
  245. }
Add Comment
Please, Sign In to add comment