Programmer's Reference

Creating icons

The fromImage:maskImage: class method creates a CgIcon given a shape image and a mask image. Wherever there is a 1 in the mask image, the icon is opaque and the corresponding pixel in the shape image is displayed. Wherever there is a 0 in the mask image, the icon is transparent and the corresponding pixel in the shape image is ignored. Specifying nil as the mask image is equivalent to specifying a mask image of all 1s (entirely opaque).

Tip:
The palette of the mask image arugment of fromImage:maskImage: is ignored when creating an icon. Transparency information is specified directly by the mask data, with 1 indicating opaque and 0 indicating transparent.

An icon must be freed using the freeIcon method when it is no longer required, in other words, when it will no longer be drawn and it is not set as the value of an icon resource of any widget.

The following example creates an 8-by-8 monochrome icon of a white cross with a black border. The icon is transparent outside of the white border. The icon is not freed because it will be used in the next example.

| palette shapeImage maskImage icon |
palette := CgIndexedPalette colors:
    (Array
        with: CgRGBColor black              "0s are black"
        with: CgRGBColor white).            "1s are white"
 
shapeImage := CgDeviceIndependentImage
    width: 8
    height: 8
    depth: 1
    palette: palette
    scanlinePad: 1
    data: #[
        2r00111100
        2r00100100
        2r11100111
        2r10000001
        2r10000001
        2r11100111
        2r00100100
        2r00111100].
 
maskImage := CgDeviceIndependentImage
    width: 8
    height: 8
    depth: 1
    palette: palette "The mask image palette is not used."
    scanlinePad: 1
    data: #[
        2r00111100
        2r00111100
        2r11111111
        2r11111111
        2r11111111
        2r11111111
        2r00111100
        2r00111100].
icon := CgIcon
    fromImage: shapeImage
    maskImage: maskImage


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]