260 likes | 398 Views
Color. How X handles color. Widget use of color. Widgets that use color have XmNforeground XmNbackground XmNborderColor (since default border width is 0 this attribute is not often seen) XmNhighlightColor (widget has focus)
E N D
Color How X handles color
Widget use of color • Widgets that use color have • XmNforeground • XmNbackground • XmNborderColor (since default border width is 0 this attribute is not often seen) • XmNhighlightColor (widget has focus) • These resources are found in Primitive or Manager and thus inherited by all children.
Some widgets have additional color resources • ToggleButtons have XmSelectColor for toggle color, • PushButtons have XmNarmColor which changes background when armed, • ScrollBar has XmNthroughColor for the area behind the slider, • . . .
Some resources are specialized and handled automatically • XmNtopShadowColor • XmNbottomShadowColor are used to give a 3d effect and are computed automatically from background to be a shade lighter or a shade darker than the background color. If the background color is dynamically changed these may not change automatically. UseXmChangeColor(widget,background) to recalculate and reset shadows
The Problem with Color • What you see is not what you get somewhere else. • Color varies from platform to platform since the value of a color is relative to a colormap data structure.
Colormaps • X assumes the RGB color model. Each pixel’s color is defined by a percent of each of the colors Red, Green, Blue. • All 3 at 100% is white • All 3 at 0% is black • 50% red, 50% green, 0% blue is yellow • It is not like “paint” which is subtractive.
Each screen pixel has a storage size (8,16,24) bits.The value of this storage is an index into a table of colors called the colormap. • 8 bits is 256 values, so this would define a colormap with 256 entries indexed 0..255 • Each entry in the colormap is capable of storing one of 256**3 (~16M) colors but a given colormap selects only 256 different colors.
Pixels in X • The data type Pixel is just an index into the colormap. Clients using colors are requesting and using colorcells in the colormap. • In general don’t deal directly with these values. They are best manipulated through library routines. • Clients can create and use different colormaps if needed
X Color Basics • X Color basics assumes • only a few colors are needed • there is space in the colormap (when the server starts it creates a default colormap which it assigns to the root window and is inherited by all applications. There will be some colorcells available for application windows)
X startup • at least two colors will be in the colormap, black and white (0 and 1) but they may not appear black and white if the percentages are changed. • Window managers generally install a few (12?) more colors. • Motif adds a few more (5?) for TopShadow, BottomShadow, .. • Remaining color cells are for applications.
All active windows have their own small set of colorcells. • Defining colors: • Try not to pick rgb percentages. • #70DB93 (assumes 8 bit) • Names can be chosen from the file rgb.txt which is usually found somewhere in X directories. Usage of these names increases the probablility that the rgb percentages these names represent will be shared in the colormap. • aquamarine
If resource files are used rc.XmLabel.background: aquamarine rc.XmLabel.background:#70DB93 • In application code use a XColor data structure defined as typedef struct { unsigned long pixel; unsigned short red,green,blue; char flags; char pad; } XColor;\ • Named Color examples slide
Colorcells continued • There are 2 kinds of color cells in a colormap. • shared (readonly) • private(read/write) • Application clients can request a private cell to assign a color. If none are available they can choose a shared cell that is “close” to the desired color.
private cells may be very limited and used up quickly. • Use shared cells when possible.
Using Shared Colorcells • First create pointers to two XColor structures. XColor colorcell; • Initialize with the values you want colorcell.red = 65535; colorcell.green= . . . • get the colormap cmap = DefaultColormap(display, DefaultScreen(screen));
Using Shared Colorcells • Make a library call to get the closest pixel value if ( XAllocColor( display, cmap, &colorcell) == 0 ) {/* failure*/} • The XAllocColor supports the shared cell strategy. It returns a pixel value that you can use. • If you prefer named colors (and you should use these to promote sharing) use XAllocNamedColor()
Example Uses XAllocNamedColor()
Getting info. • Because a “closest” will be chosen for you, you can use Colormap cmap; Xcolor color; color.red = ... XQueryColor(display,cmap,&color); or XParseColor(display,cmap,65535,&color) or XLookupColor(display,cmap,name,&color,&exact) for information about what will happen
Allocating private cells • To allocate private color cells use XAllocColorCells ( ) library routine to allocate the cells. This should be checked for failure. • Then XStoreColor( ) or XStoreNamedColor( ) can be used to fill these cells. • private color cells should be freed when no longer needed with XFreeColors( ). DO NOT CALL with shared cells.
Standard ColorMaps • To promote maximum sharing X provides several standard color maps. These maps contain cells with preloaded values. • The root window usually loads a standard colormap at startup • Application using a standard colormap face colormap change effects and must take what they get.
Colormaps are resources • XCreateColormap( ) • XCopyColormap( ) are both ways to create new colormaps. Colormaps created by applications must be installed in order to be used and are destroyed on exit. • XInstallColormap( ) • XUninstallColormap( ) • XFreeColormap( )
Colormaps are not really private • Installed colormaps are a resource of the screen and if its resource id is discovered by other applications (XListInstalledColormaps( )) it can be used - but not altered if you fill all the cells) • Once installed it must be associated with windows.
Events • Applications can arrange to receive ColormapNotify events. These events notify the application of changes to the colormap in your windows.
Summary Applications draw into windows and pixmaps using pixel values. These values are relative to color cells that are defined in colormaps. Applications cannot assume any meaning about colors on a platform. X calls are used to generate pixel values corresponding to desired colors. Colors are specified as a combination of red green and blue values. Each color map cell contains a single rgb triplet. Colormap cells are scarse, so choosing the closest color values to the default colormap installed in the root window is a preferred technique. However applications can create and install their own private color maps. Changing colormaps on some platforms can create undesirable effects outside of the application window.
Summary Production code must do a lot of determination about the platform on which it is running before using library routines: the number of colormap cells, the default color map, the number of color planes ( not discussed with these slides) Xlib provides a variety of service functions for color, the most important being the ability to translate named colors into rgb triples.
Future studies • Students interested in color should look next into visual techniques and their support in X • color planes • support in X for applications that need to interpolate between colors for shading effects. • Production quality color code examples should be investigated. HAVE A GREEN DAY!