GUIs and Color Here's a first crack at an 8-puzzle widget:
How It Was Produced A BulletinBoard container widget bb of 500 x 500 pixels was created. Resources: XmNheight (500) XmNwidth (500) XmNshadowThickness (5 pixels) Nine PushButton widgets were created as children of bb inside of a doubly nested loop: XmNheight (100) XmNwidth (100)
How It Was Produced (cont'd) To make the buttons appear to pop out of the screen, use resources: XmNshadowType (XmSHADOW_OUT) XmNshadowThickness (5) To make the tiles white, use resource XmNbackground (see later slide) To display the tile number: convert it to a string convert the string to a Motif compound string (recall XmStringCreateLocalized) give the compound string as value to the resource XmNlabelString
Changing the XmNbackground Resource Suppose you want your 8-puzzle board to look like this:
About Colors The XmNbackground resource requires a value of type Pixel A Pixel is a special kind of integer, one that is a valid index into a Colormap A Colormap is an array of colors; most displays provide a hardware colormap To specify a new background color, the programmer must know: the symbolic name of the desired color, and how to set the XmNbackground resource to the Pixel value associated with that color
Role of the Colormap R G B.. R = Red G = Green B = Blue Frame Buffer Pixel Value Colormap blue
Color Database (Human Readable) ! $XConsortium: rgb.txt,v /02/20 18:39:36 rws Exp $ snow ghost white GhostWhite white smoke WhiteSmoke gainsboro floral white FloralWhite old lace OldLace linen antique white AntiqueWhite papaya whip PapayaWhip blanched almond... [ about 720 more ]... Directory: /usr/X/lib File: rgb.txt The numbers stand for the intensity of the red, green, and blue components of the color
Setting Color Programmatically The desired bulletin board and push button background colors are DarkRed and PowderBlue To find their indexes into the colormap (pixels) a programmer can use: XAllocNamedColor(Display *display, Colormap cmap, char *name, XColor *color, XColor *exact) This requires knowing how to retrieve displays, screens, and colormaps.
Getting A Pixel By Color Name Pixel GetPixelByName (Widget w, char* colorname) { Display *dpy = XtDisplay( w ); int scr = DefaultScreen (dpy); Colormap cmap = DefaultColormap (dpy, scr); XColor color, ignore; If (XAllocNamedColor (dpy,cmap,colorname,&color,&ignore)) return color.pixel; else { XtWarning ("Couldn't allocate color" ); return BlackPixel (dpy, scr); { } Now use GetPixelByName(bb,"DarkRed") paired with resource XmNbackground when creating bb.
Using GetPixelByName Widget bb = XtVaCreateManagedWidget ( "puzzle", xmBulletinBoardWidgetClass, form, Nheight, 3*TILE_SIZE+2*BB_SHADOW, XmNwidth, 3*TILE_SIZE+2*BB_SHADOW, XmNmarginHeight, 0, XmNmarginWidth, 0, XmNshadowThickness, BB_SHADOW, XmNshadowType, XmSHADOW_IN, XmNbackground, GetPixelByName(bb, ``DarkRed''), NULL ); Use the same approach to make the color of the tile pushbuttons “PowderBlue”.
Disadvantages of Setting Color Programmatically GetPixelByName is not defined in either Xt or Xm; you have to write it Anytime you want to change the background color you have to recompile the program A better way is to use the Resource Manager