Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <cstdlib>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <unistd.h>
- /*
- root@ST-LAPTOP:/usr/include/X11# grep "XGetPixel" ./*
- ./Xutil.h:extern unsigned long XGetPixel(
- ./Xutil.h:#define XGetPixel(ximage, x, y) \
- */
- // Compile: g++ mypointer.c -o my -lX11
- // cc prog.c -o prog -L/usr/X11/lib -lX11
- // or perhaps this (for a system with release 6 of X11):
- //cc prog.c -o prog -L/usr/X11R6/lib -lX11
- // On SunOs 4 systems, the X libraries are placed in /usr/openwin/lib:
- // cc prog.c -o prog -L/usr/openwin/lib -lX11
- #define GetRValue(dwColor) dwColor & 0x000000FF
- #define GetGValue(dwColor) (dwColor & 0x0000FF00) >> 8
- #define GetBValue(dwColor) dwColor >> 16
- int GetPixel(int x, int y)
- {
- Display *dpy;
- XImage *image;
- XColor col;
- dpy = XOpenDisplay(NULL);
- image = XGetImage(dpy, DefaultRootWindow(dpy),
- x, y, 1, 1, AllPlanes, ZPixmap);
- col.pixel = XGetPixel(image, 0, 0);
- XDestroyImage(image);
- // The XQueryColor() function returns the current RGB value for the pixel in the XColor structure and sets the DoRed, DoGreen, and DoBlue flags.
- // XQueryColor() can generate BadColor and BadValue errors.
- // BadColor A value for a Colormap argument does not name a defined Colormap.
- // BadValue Some numeric value falls outside the range of values accepted by the request.
- // Unless a specific range is specified for an argument, the full range defined by the argument's type
- // is accepted. Any argument defined as a set of alternatives can generate this error.
- XQueryColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), &col);
- // The colormap is a lookup table located on the server the pixel value is used as an index into the table.
- XCloseDisplay(dpy);
- printf("R: %03d G: %03d B: %03d\n", (int)((double) col.red/257.0), (int)((double) col.green/257.0), (int)((double) col.blue/257.0));
- return (col.red&0xff00)<<8 | (col.green)&0xff00 | (col.blue) >> 8; // = windows return value
- }
- int main()
- {
- // this variable will contain the pointer to the Display structure
- // returned when opening a connection.
- Display* display;
- int screen_num;
- Screen *screen;
- Window root_win;
- XGCValues gc_val;
- // open the connection to the display "simey:0".
- display = XOpenDisplay(0);// display = XOpenDisplay("simey:0");
- if (display == NULL)
- {
- fprintf(stderr, "Cannot connect to X server %s\n", "simey:0");
- exit (EXIT_FAILURE);
- }
- XGrabServer(display);
- screen_num = DefaultScreen(display);
- screen = XScreenOfDisplay(display, screen_num);
- root_win = RootWindow(display, XScreenNumberOfScreen(screen));
- // Create a GC (Graphics Context) for the line
- gc_val.function = GXclear;
- gc_val.plane_mask = AllPlanes;
- gc_val.foreground = WhitePixel(display, screen_num);
- gc_val.background = BlackPixel(display, screen_num);
- gc_val.line_width = 4;
- gc_val.line_style = LineSolid;
- gc_val.cap_style = CapButt;
- gc_val.join_style = JoinMiter;
- gc_val.fill_style = FillOpaqueStippled;
- gc_val.fill_rule = WindingRule;
- gc_val.graphics_exposures = False;
- gc_val.clip_x_origin = 0;
- gc_val.clip_y_origin = 0;
- gc_val.clip_mask = None;
- gc_val.subwindow_mode = IncludeInferiors;
- GC gc_line = XCreateGC(display, root_win, GCFunction | GCPlaneMask | GCForeground | GCBackground | GCLineWidth | GCLineStyle |
- GCCapStyle | GCJoinStyle | GCFillStyle | GCFillRule | GCGraphicsExposures |
- GCClipXOrigin | GCClipYOrigin | GCClipMask | GCSubwindowMode, &gc_val);
- while( 1 )
- {
- // XSetForeground(display, gc_line, some_color);
- // XDrawRectangle(display, root_win, gc_line, 50, 50, 400, 400);
- for (int ix = 0; ix < 500;++ix)
- {
- for (int iy = 0; iy < 500;++iy)
- {
- XDrawPoint(display, root_win, gc_line, ix, iy); // SetPixel
- }
- }
- usleep(100);
- }
- XFlush(display);
- XUngrabServer(display);
- XCloseDisplay( display );
- int iMyColor = GetPixel(20,20) ;
- printf("Sole color: 0x%08X\n", iMyColor);
- printf("R: %03d G: %03d B: %03d\n", GetRValue(iMyColor), GetGValue(iMyColor), GetBValue(iMyColor));
- // printf("Sizof short: %ld\n", sizeof(unsigned short)) ; // 2 Bytes
- return EXIT_SUCCESS ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement