#include <tk.h>
XColor * Tk_GetColor(interp, tkwin, colorMap, nameId)
XColor * Tk_GetColorByValue(interp, tkwin, colorMap, prefPtr)
char * Tk_NameOfColor(colorPtr)
Tk_FreeColor(colorPtr)
The Tk_GetColor and Tk_GetColorByValue procedures locate pixel values that may be used to render particular colors in the window given by tkwin using the colormap given by colormap. In Tk_GetColor the desired color is specified with a Tk_Uid (nameId), which may have any of the following forms:
In Tk_GetColorByValue, the desired color is indicated with the red, green, and blue fields of the structure pointed to by colorPtr.
If Tk_GetColor or Tk_GetColorByValue is successful in allocating the desired color, then it returns 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 from colormap that may be used to draw in the color. If an error occurs in allocating a color then NULL is returned and an error message will be stored in interp->result.
Tk_GetColor and Tk_GetColorByValue maintain a database of all the colors currently in use. If the same nameId is requested multiple times from Tk_GetColor (e.g. by different windows), or if the same intensities are requested multiple times from Tk_GetColorByValue, then existing pixel values will be re-used. Re-using an existing pixel avoids any interaction with the X server, which makes the allocation much more efficient. For this reason, you should generally use Tk_GetColor or Tk_GetColorByValue instead of Xlib procedures like XAllocColor, XAllocNamedColor, or XParseColor.
Since different calls to Tk_GetColor or Tk_GetColorByValue 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 XAllocColorCells to allocate the pixel value for the color.
The procedure Tk_NameOfColor is roughly the inverse of Tk_GetColor. If its colorPtr argument was created by Tk_GetColor, then the return value is the nameId string that was passed to Tk_GetColor to create the color. If colorPtr was created by a call to Tk_GetColorByValue, or by any other mechanism, then the return value is a string that could be passed to Tk_GetColor to return the same color. Note: the string returned by Tk_NameOfColor is only guaranteed to persist until the next call to Tk_NameOfColor.
When a pixel value returned by Tk_GetColor or Tk_GetColorByValue is no longer needed, Tk_FreeColor should be called to release the color. There should be exactly one call to Tk_FreeColor for each call to Tk_GetColor or Tk_GetColorByValue. When a pixel value is no longer in use anywhere (i.e. it has been freed as many times as it has been gotten) Tk_FreeColor will release it to the X server and delete it from the database.