If the default palette is to be used, the setPalette: message is not required, because it is selected by default when windows are created. Remember that CgIndexedPalettes can be selected in windows, not CgDirectPalettes.The setPalette: message can be sent only to the window of a shell widget or to a pixmap. It is an error to send setPalette: to other windows. The shell window's palette is inherited by all child windows of the shell window.The following example illustrates how to select a palette. This assumes that the shell message answers the CwTopLevelShell widget of the application (see "Shell widgets" for more information on shell widgets).The selected palette can be retrieved using the getPalette message.The getPalette message can be sent to any drawable. If getPalette is sent to a window that is not a shell window, the palette of the shell window is answered.To use a color specified by the selected palette, the corresponding pixel value is set in the graphics context foreground or background component. For example, the following code draws a blue-filled rectangle, assuming the palette in the previous example is selected. This code assumes that the drawable message answers the window of a CwDrawingArea widget (which is a child of the shell widget used in the previous example).Alternatively, the getPalette and nearestPixelValue: methods can be used to find the appropriate pixel value.If drawing is done in black or white, the blackPixel and whitePixel methods of CgDrawable can be used to determine the pixel value to use for the selected palette.The blackPixel and whitePixel methods of CgDrawable answer the pixel value of the color closest to black and white, respectively, in the drawable's selected palette.When a CgPixmap is first created using createPixmap:, the default palette is selected in the pixmap, not the palette of receiver of the createPixmap: message. The receiver is only used to specify the screen and compatible depths of the pixmap. Because the receiver might not be the intended destination of a copy operation using the pixmap, the receiver's palette is not selected in the pixmap.The following code shows the correct way to draw in a pixmap using a palette other than the default palette. The code assumes that the shell method answers the CwTopLevelShell widget and that the drawingArea method answers the CwDrawingArea widget in which the pixmap will be displayed.
|