[Xcb] dealing with colors
Jean-Loup Defaisse
jloupdef at gmail.com
Thu Aug 7 08:48:50 PDT 2008
Thank's everyone I solved my issue, I'm using the default screen colormap
and it seems i don't need to allocate the color with xcb_alloc_color, I just
called xcb_change_gc (thanks Peter) with the color wanted and it works fine.
++
2008/8/7 Ian Osgood <iano at quirkster.com>
> Your stumbling point may be that foreground/background color is not set in
> an independent X protocol call, but as one of the many attributes of a
> Graphics Context (GC) or a Window. Whereas Xlib has convenience functions
> for setting an individual attribute in a single request (XSetForeground),
> XCB only exposes the bulk attribute change request:
> typedef enum xcb_gc_t {
> ...
> XCB_GC_FOREGROUND = (1 << 2),
> XCB_GC_BACKGROUND = (1 << 3),
> ...
> } xcb_gc_t;
>
> xcb_void_cookie_t
> xcb_change_gc (xcb_connection_t *c /**< */,
> xcb_gcontext_t gc /**< */,
> uint32_t value_mask /**< */,
> const uint32_t *value_list /**< */);
>
> I *think* this is also covered in Vincent's XCB tutorial.
>
> There is a more Xlib-like wrapper, where you fill in a struct, in
> xcb/util/aux/xcb_aux.h
>
> xcb_void_cookie_t
> xcb_aux_change_gc (xcb_connection_t *c,
> xcb_gcontext_t gc,
> uint32_t mask,
> const xcb_params_gc_t *params);
>
> You can also look at demo/neko/xcbneko.c Here are the relevant excerpts,
> following the course of theFgPixel:
>
> unsigned long theFgPixel;
>
> if ( fgColor == NULL) fgColor = "black";
> xcb_alloc_named_color_cookie_t fgCookie = xcb_alloc_named_color ( xc,
> theColormap, strlen(fgColor), fgColor );
>
> xcb_alloc_named_color_reply_t *fgRep = xcb_alloc_named_color_reply( xc,
> fgCookie, 0 );
> if (!fgRep) {
> fprintf( stderr,
> "%s: Can't allocate the foreground color %s.\n", ProgramName, fgColor );
> exit( 1 );
> }
> theFgPixel = fgRep->pixel;
>
> ...
>
> void InitBitmapAndGCs(void) {
> BitmapGCData *BitmapGCDataTablePtr;
> uint32_t theGCValues[5];
>
> theGCValues[0] = XCB_GX_COPY;
>
> theGCValues[1] = theFgPixel;
> theGCValues[2] = theBgPixel;
>
> theGCValues[3] = XCB_FILL_STYLE_TILED;
>
> /* TODO: latency: make all the bitmaps, then all the contexts? */
>
> for ( BitmapGCDataTablePtr = BitmapGCDataTable;
> BitmapGCDataTablePtr->GCCreatePtr != NULL;
> BitmapGCDataTablePtr++ ) {
>
> *(BitmapGCDataTablePtr->BitmapCreatePtr)
> = CreatePixmapFromBitmapData( xc, theScreen->root,
> BitmapGCDataTablePtr->PixelPattern,
> BitmapGCDataTablePtr->PixelWidth,
> BitmapGCDataTablePtr->PixelHeight,
> theFgPixel, theBgPixel, theScreen->root_depth);
>
> theGCValues[4] = *(BitmapGCDataTablePtr->BitmapCreatePtr); /* tile */
> *(BitmapGCDataTablePtr->GCCreatePtr) = xcb_generate_id( xc );
> xcb_create_gc( xc, *(BitmapGCDataTablePtr->GCCreatePtr), theWindow,
> XCB_GC_FUNCTION | XCB_GC_FOREGROUND | XCB_GC_BACKGROUND |
> XCB_GC_FILL_STYLE | XCB_GC_TILE,
> theGCValues );
> }
>
> /* later: xcb_free_pixmap( c, bitmap ); */
> /* later: xcb_free_gc( c, gc ); */
> }
> ...
>
> uint32_t theWindowAttributes[] = {
> theBgPixel, /* background */
> theFgPixel, /* border */
> False, /* override_redirect */
> EVENT_MASK,
> theCursor };
>
> unsigned long theWindowMask = XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL |
> XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_CURSOR;
> theWindow = xcb_generate_id( xc );
> xcb_create_window( xc,
> theDepth,
> theWindow,
> theScreen->root,
> WindowPointX, WindowPointY,
> WindowWidth, WindowHeight,
> BorderWidth,
> XCB_WINDOW_CLASS_INPUT_OUTPUT,
> theScreen->root_visual, /* CopyFromParent */
> theWindowMask, theWindowAttributes );
>
> Ian
>
> On Aug 7, 2008, at 7:13 AM, Jean-Loup Defaisse wrote:
>
> the code of julia.c doesn't help me because it use XCBImagePutPixel and
> the color is directly given in parameter, but I'm drawing directly in the
> window using xcb_poly_segment, xcb_poly_point... and the color can't be
> given directly. That's why i'm looking to a function corresponding to
> XSetForeground to select the color i want.
> I hope i'm clear enough, else i'll try to simplify my program to post it
> here when i have time.
>
> 2008/8/7 Vincent Torri <vtorri at univ-evry.fr>
>
>>
>> Hey,
>>
>>
>> I started few days ago a gui creation with Xlib, but i found xcb
>>> yesterday
>>> and decided try it. I modified my code quiet easily (it wasn't really
>>> long)
>>> except for the color. How should i replace
>>> XAllocColor(WinInf->dpy, WinInf->cmap, &c);
>>> XSetForeground(WinInf->dpy, WinInf->gc, c.pixel);
>>>
>>> I tried by using xcb_alloc_color but i didn't succeed (all my drawings
>>> were
>>> black).
>>>
>>> Any idee?
>>> Else, is there a simple example just drawing colored points in an empty
>>> window?
>>>
>>
>> see in demo/ the code of 'julia.c'
>>
>> Vincent
>>
>
> _______________________________________________
> Xcb mailing list
> Xcb at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/xcb
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freedesktop.org/archives/xcb/attachments/20080807/888c01a4/attachment-0001.htm
More information about the Xcb
mailing list