Home › Tk Library Procedures Tk
Tk_AllocColorFromObj C API
Maintain database of colors
Also documents Tk_GetColor, Tk_GetColorFromObj, Tk_GetColorByValue, Tk_NameOfColor, Tk_FreeColorFromObj, Tk_FreeColor
literal type exactly as shown
argument replace with your value
?optional? may be omitted
Synopsis#
#include <tk.h>
XColor * Tk_AllocColorFromObj(interp, tkwin, objPtr)#
XColor * Tk_GetColor(interp, tkwin, name)#
XColor * Tk_GetColorFromObj(tkwin, objPtr)#
XColor * Tk_GetColorByValue(tkwin, prefPtr)#
const char * Tk_NameOfColor(colorPtr)#
GC Tk_GCForColor(colorPtr, drawable)#
Tk_FreeColorFromObj(tkwin, objPtr)#
Tk_FreeColor(colorPtr)#
Arguments#
- Tcl_Interp *interp (in)
-
Interpreter to use for error reporting.
- Tk_Window tkwin (in)
-
Token for window in which color will be used.
- Tcl_Obj *objPtr (in/out)
-
String value describes desired color; internal rep will be modified to cache pointer to corresponding (XColor *).
- const char *name (in)
-
Same as objPtr except description of color is passed as a string and resulting (XColor *) is not cached.
- XColor *prefPtr (in)
-
Indicates red, green, and blue intensities of desired color.
- XColor *colorPtr (in)
-
Pointer to X color information. Must have been allocated by previous call to
Tk_AllocColorFromObj,Tk_GetColororTk_GetColorByValue, except when passed toTk_NameOfColor. - Drawable drawable (in)
-
Drawable in which the result graphics context will be used. Must have same screen and depth as the window for which the color was allocated.
Description#
These procedures manage the colors being used by a Tk application. They allow colors to be shared whenever possible, so that colormap space is preserved, and they pick closest available colors when colormap space is exhausted.
Given a textual description of a color, Tk_AllocColorFromObj locates a pixel value that may be used to render the color in a particular window. The desired color is specified with a value whose string value must have one of the following forms:
- colorname
-
Any of the valid textual names for a color defined in the server's color database file, such as
redorPeachPuff. #RGB#RRGGBB#RRRGGGBBB#RRRRGGGGBBBB-
A numeric specification of the red, green, and blue intensities to use to display the color. Each R, G, or B represents a single hexadecimal digit. The four forms permit colors to be specified with 4-bit, 8-bit, 12-bit or 16-bit values. When fewer than 16 bits are provided for each color, they represent the most significant bits of the color, while the lower unfilled bits will be repeatedly replicated from the available higher bits. For example, #3a7 is the same as #3333aaaa7777.
Tk_AllocColorFromObjreturns a pointer to an XColor structure; the structure indicates the exact intensities of the allocated color (which may differ slightly from those requested, depending on the limitations of the screen) and a pixel value that may be used to draw with the color in tkwin. If an error occurs inTk_AllocColorFromObj(such as an unknown color name) then NULL is returned and an error message is stored in interp's result if interp is not NULL. If the colormap for tkwin is full,Tk_AllocColorFromObjwill use the closest existing color in the colormap.Tk_AllocColorFromObjcaches information about the return value in objPtr, which speeds up future calls to procedures such asTk_AllocColorFromObjandTk_GetColorFromObj.Tk_GetColoris identical toTk_AllocColorFromObjexcept that the description of the color is specified with a string instead of a value. This preventsTk_GetColorfrom caching the return value, soTk_GetColoris less efficient thanTk_AllocColorFromObj.Tk_GetColorFromObjreturns the token for an existing color, given the window and description used to create the color.Tk_GetColorFromObjdoes not actually create the color; the color must already have been created with a previous call toTk_AllocColorFromObjorTk_GetColor. The return value is cached in objPtr, which speeds up future calls toTk_GetColorFromObjwith the same objPtr and tkwin.Tk_GetColorByValueis similar toTk_GetColorexcept that the desired color is indicated with the red, green, and blue fields of the structure pointed to by colorPtr.This package maintains a database of all the colors currently in use. If the same color is requested multiple times from
Tk_GetColororTk_AllocColorFromObj(e.g. by different windows), or if the same intensities are requested multiple times fromTk_GetColorByValue, then existing pixel values will be re-used. Re-using an existing pixel avoids any interaction with the window server, which makes the allocation much more efficient. These procedures also provide a portable interface that works across all platforms. For this reason, you should generally useTk_AllocColorFromObj,Tk_GetColor, orTk_GetColorByValueinstead of lower level procedures likeXAllocColor.Since different calls to this package may return the same shared pixel value, callers should never change the color of a pixel returned by the procedures. If you need to change a color value dynamically, you should use
XAllocColorCellsto allocate the pixel value for the color.The procedure
Tk_NameOfColoris roughly the inverse ofTk_GetColor. If its colorPtr argument was created byTk_AllocColorFromObjorTk_GetColorthen the return value is the string that was used to create the color. If colorPtr was created by a call toTk_GetColorByValue, or by any other mechanism, then the return value is a string that could be passed toTk_GetColorto return the same color. Note: the string returned byTk_NameOfColoris only guaranteed to persist until the next call toTk_NameOfColor.Tk_GCForColorreturns a graphics context whoseforegroundfield is the pixel allocated for colorPtr and whose other fields all have default values. This provides an easy way to do basic drawing with a color. The graphics context is cached with the color and will exist only as long as colorPtr exists; it is freed when the last reference to colorPtr is freed by callingTk_FreeColor.When a color is no longer needed
Tk_FreeColorFromObjorTk_FreeColorshould be called to release it. ForTk_FreeColorFromObjthe color to release is specified with the same information used to create it; forTk_FreeColorthe color to release is specified with a pointer to its XColor structure. There should be exactly one call toTk_FreeColorFromObjorTk_FreeColorfor each call toTk_AllocColorFromObj,Tk_GetColor, orTk_GetColorByValue.
Keywords#
color · intensity · value · pixel value
Copyright#
- Copyright © 1990-1991 The Regents of the University of California
- Copyright © 1994-1998 Sun Microsystems, Inc