A direct-color image is an image in which the pixel values directly encode the colors. Typically these images have 16 or more bits per pixel, with 24 bits per pixel being the most common. A CgDirectPalette is used to define the pixel-value-to-color mapping for direct-color images. Although CgDirectPalettes cannot be selected into a CgDrawable, a CgIndexedPalette creation convenience method, colorCube:, can be used to create an approximation of all possible colors. The colorCube: method takes an array of three integers specifying the number of levels of red (R), green (G), and blue (B) to include in the palette. The resulting palette has R*G*B entries. The following example creates and displays a 256-by-256 direct-color image with a constant amount of red, with green increasing from top to bottom, and with blue increasing from left to right. The resulting image has red at the top left corner, magenta at the top right, yellow at the bottom left, and white at the bottom right. The image is not shown here because it cannot be adequately reproduced in black and white.
| directPalette image pixel gc r g b | directPalette := CgDirectPalette redMask: 16rFF greenMask: 16rFF00 blueMask: 16rFF0000. image := CgDeviceIndependentImage width: 64 height: 64 depth: 24 palette: directPalette.
r := 255. 0 to: 63 do: [:y | g := y * 255 // 63. 0 to: 63 do: [:x | b := x * 255 // 63. pixel := (b bitShift: 16) + (g bitShift: 8) + r. image putPixel: x y: y pixelValue: pixel]].
self shell window setPalette: (CgIndexedPalette colorCube: #(6 6 6)). gc := self drawable createGC: None values: CgGCValues new.
self drawable putDeviceIndependentImage: gc image: image srcRect: (0@0 extent: image extent) destRect: (20@20 extent: image extent). gc freeGC