Programmer Reference : Common Graphics : Using palettes : From pixel values to colors and back
From pixel values to colors and back
As described earlier, a palette defines a mapping from pixel values to colors. Pixel values are integers between 0 and n-1, where n is the number of colors defined by the palette.
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:
| colors palette |
colors := Array
with: (CgRGBColor red: 0 green: 0 blue: 0)
with: (CgRGBColor red: 16rFFFF green: 0 blue: 0)
with: (CgRGBColor red: 16rFFFF green: 16rFFFF blue: 16rFFFF).
palette := CgIndexedPalette colors: colors.
palette at: 0
==> aCgRGBColor(16r0000, 16r0000, 16r0000)
palette at: 1
==> aCgRGBColor(16rFFFF, 16r0000 16r0000)
palette at: 2
==> aCgRGBColor(16rFFFF, 16rFFFF, 16rFFFF)
palette at: 3
==> aCgRGBColor(16r0000, 16r0000, 16r0000)
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:.
| colors palette |
colors := Array
with: (CgRGBColor red: 0 green: 0 blue: 0)
with: (CgRGBColor red: 16rFFFF green: 0 blue: 0)
with: (CgRGBColor red: 16rFFFF green: 16rFFFF blue: 16rFFFF).
palette := CgIndexedPalette colors: colors.
palette nearestPixelValue: (CgRGBColor red: 0 green: 0 blue: 0)
==> 0
palette nearestPixelValue: (CgRGBColor red: FFFF green: 0 blue: 0)
==> 1
palette nearestPixelValue: (CgRGBColor red: FFFF green: FFFF
blue: FFFF)
==> 2
palette nearestPixelValue: (CgRGBColor red: BFFF green: 0 blue: 0)
==> 1
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:.
| colors palette |
colors := Array
with: (CgRGBColor red: 0 green: 0 blue: 0)
with: (CgRGBColor red: 16rFFFF green: 0 blue: 0)
with: (CgRGBColor red: 16rFFFF green: 16rFFFF blue: 16rFFFF).
palette := CgIndexedPalette colors: colors.
palette nearestColor: (CgRGBColor red: 0 green: 0 blue: 0)
==> (CgRGBColor red: 0 green: 0 blue: 0)
palette nearestColor: (CgRGBColor red: FFFF green: 0 blue: 0)
==> (CgRGBColor red: FFFF green: 0 blue: 0)
palette nearestColor: (CgRGBColor red: FFFF green: FFFF
blue: FFFF)
==> (CgRGBColor red: FFFF green: FFFF blue: FFFF)
palette nearestColor: (CgRGBColor red: BFFF green: 0 blue: 0)
==> (CgRGBColor red: FFFF green: 0 blue: 0)
Last modified date: 01/29/2015