Cross-platform C SDK logo

Cross-platform C SDK

Palettes

❮ Back
Next ❯
This page has been automatically translated using the Google Translate API services. We are working on improving texts. Thank you for your understanding and patience.

Functions

Palette*palette_create (...)
Palette*palette_optimal (...)
Palette*palette_cga2 (...)
Palette*palette_ega4 (void)
Palette*palette_rgb8 (void)
Palette*palette_gray1 (void)
Palette*palette_gray2 (void)
Palette*palette_gray4 (void)
Palette*palette_gray8 (void)
voidpalette_destroy (...)
uint32_tpalette_size (...)
color_t*palette_colors (...)

A palette is nothing more than a list of colors, usually related to an indexed Pixel Buffer (Figure 1). Its main utility is to save space in the images representation, since each pixel is encoded by an index of 1, 2, 4 or 8 bits instead of the real color where 24 or 32 bits are necessary. For this reason, it is usual to have palettes of 2, 4, 16 or 256 colors.


1. Predefined palette

We have several predefined palettes both in color (Figure 2) and in grays (Figure 3). The RGB8 palette has been created by combining 8 tones of red (3bits), 8 tones of green (3bits) and 4 tones of blue (2bits). This is so because the human eye distinguishes much less the variation of blue than the other two colors.

  • Use palette_rgb8 and similars to create a color palette.
  • Use palette_gray4 and similars to create a palette in grays.
  • CGA and EGA palettes.
    Figure 2: Predefined color palettes.
    Palettes in shades of gray.
    Figure 3: Predefined gray palettes.

2. Optimal palette

Normally RGB images use 8 bits for each channel, which means 16.777.216 different colors, much more than the human eye can distinguish (Figure 4). To optimize the space occupied by the image, with the least possible loss, the optimal palette is calculated using color quantification algorithms (Figure 5). This contrasts with the use of a generic palette, where the loss of quality is evident since many colors may not be used as they are very different from those required (Figure 6).

  • Use palette_optimal to get the optimal palette for an RGB image.
  • Image of a landscape and associated RGB space.
    Figure 4: RGB 24 bits image (true color).
    Image of a landscape and its optimal palette of 256 colors.
    Figure 5: 256-color image using an optimal palette.
    Image of a landscape and its generic 81-color palette.
    Figure 6: 256-color image using a generic palette.

palette_create ()

Create a palette.

Palette*
palette_create(const uint32_t size);
size

The number of colors.

Return

The palette. The initial content is undetermined. Edit with palette_colors.


palette_optimal ()

Create an optimal color palette, applying color quantization algorithms.

Palette*
palette_optimal(const uint32_t width,
                const uint32_t height,
                const pixformat_t format,
                const byte_t *data,
                const uint32_t size);
width

The width in pixels of the image.

height

The pixel height of the image.

format

The pixel format. Only ekRGB24 and ekRGBA32 are valid.

data

Buffer with pixel color information.

size

Number of colors in the resulting palette.

Return

The palette.

Remarks

Optimal palette


palette_cga2 ()

Create the 4-color (2-bit) palette of CGA cards.

Palette*
palette_cga2(const bool_t mode,
             const bool_t intense);
mode

TRUE for CGA mode 1, FALSE mode 0.

intense

TRUE for bright colors.

Return

The palette.

Remarks

Predefined palette


palette_ega4 ()

Create the default palette for EGA cards (16 colors, 4 bits).

Palette*
palette_ega4(void);

Return

The palette.

Remarks

Predefined palette


palette_rgb8 ()

Create the default 8-bit RGB palette. Colors combine 8 tones of red, 8 green and 4 blue.

Palette*
palette_rgb8(void);

Return

The palette.

Remarks

Predefined palette


palette_gray1 ()

Create a palette of 2 tones of gray (1 bit). Black (0) and white (1).

Palette*
palette_gray1(void);

Return

The palette.

Remarks

Predefined palette


palette_gray2 ()

Create a palette of 4 tones of gray (2 bit). Black (0), White (3).

Palette*
palette_gray2(void);

Return

The palette.

Remarks

Predefined palette


palette_gray4 ()

Create a palette of 16 tones of gray (4 bit). Black (0), White (15).

Palette*
palette_gray4(void);

Return

The palette.

Remarks

Predefined palette


palette_gray8 ()

Create a palette of 256 shades of gray (8 bit). Black (0), White (255).

Palette*
palette_gray8(void);

Return

The palette.

Remarks

Predefined palette


palette_destroy ()

Destroy the palette.

void
palette_destroy(Palette**);

The palette. It will be set to NULL after the destruction.


palette_size ()

Returns the number of colors in the palette.

uint32_t
palette_size(Palette*);

The palette.

Return

The number of colors.


palette_colors ()

Get the color list.

color_t*
palette_colors(Palette*);

The palette.

Return

Colors. The size of the array is given by palette_size.

Remarks

The buffer is read/write.

❮ Back
Next ❯