Pixel bg, top_shadow, bottom_shadow, fg, select_color;
Colormap cmap;
Widget pb;
/* First, set the background color to red... */
XtVaSetValues (pb,
XtVaTypedArg, XmNbackground, XmRString, "red", 4,/* strlen("red")+1 */
NULL);
/* Once set, get it again, so we know what pixel value it got.
* Also get the widget's colormap, since we'll be setting its new
* colors based on the same colormap.
*/
XtVaGetValues (pb,
XmNbackground, &bg,
XmNcolormap, &cmap,
NULL);
/* Let Motif calculate the new colors based on that one color */
XmGetColors (XtScreen (pb), cmap, bg, &fg,
&top_shadow, &bottom_shadow, &select_color);
/* Set the colors accordingly. */
XtVaSetValues (pb,
XmNtopShadowColor, top_shadow,
XmNbottomShadowColor, bottom_shadow,
XmNarmColor, select_color,
XmNborderColor, fg,
NULL);
A basic problem behind setting and getting colors for widgets is that what you get for a given pixel value depends on
the colormap. A pixel is simply an index value into an array of color definitions (a colormap). The problem with
colormaps is that you never know what colormap is associated with any particular widget.
By calling XtVaSetValues() using the type−converting resource, XtVaTypedArg, we defer the problem to the
toolkit and its string−to−color type converter. The toolkit allocates the color out of the colormap already owned by the
toolkit and sets the background color accordingly. Then we can get the actual pixel value and the colormap using
XtVaGetValues(). We pass the colormap and the background pixel ...