To find the color corresponding to a given pixel value, the at: message is sent to the palette with the pixel value as the argument. In the following example, the palette has three colors and so defines colors for pixel values between 0 and 2 inclusive:The last expression illustrates that if the palette does not define a color for a given pixel value, the at: method answers black, that is, a CgRGBColor with intensities of 0.It is also possible to find the pixel value corresponding to a given color in a palette by using the nearestPixelValue: message. This message answers the pixel value for the color in the palette that most closely matches the given color, as the following example illustrates. The exact algorithm used for matching colors is described in the specification for CgPalette>>nearestPixelValue:.The nearestColor: message answers the color in the palette that most closely matches the given color. It is equivalent to looking up the color in the palette using at: after getting the nearest pixel value using nearestPixelValue:.
![]() |