# Color Coding

## Color Coding

The library's color system is RGB. In order to simplify some functions, a compact representation was created for the 3 values. To make a conversion from this representation to the 3 separate values and vice-versa, use functions cdDecodeColor and cdEncodeColor.

When the canvas used does not support more than 8 bpp of color resolution, you can use function cdPalette to give the driver an idea of which colors to prioritize. cdPalette's behavior is driver dependent.

There are some predefined colors:

```CD_RED          = (255,  0,  0)
CD_DARK_RED     = (128,  0,  0)
CD_GREEN        = (0  ,255,  0)
CD_DARK_GREEN   = (  0,128,  0)
CD_BLUE         = (  0,  0,255)
CD_DARK_BLUE    = (  0,  0,128)
CD_YELLOW       = (255,255,  0)
CD_DARK_YELLOW  = (128,128,  0)
CD_MAGENTA      = (255,  0,255)
CD_DARK_MAGENTA = (128,  0,128)
CD_CYAN         = (  0,255,255)
CD_DARK_CYAN    = (  0,128,128)
CD_WHITE        = (255,255,255)
CD_BLACK        = (  0,  0 , 0)
CD_DARK_GRAY    = (128,128,128)
CD_GRAY         = (192,192,192)
```

```long int cdEncodeColor(unsigned char red, unsigned char green, unsigned char blue) [in C]

cd.EncodeColor(r, g, b: number) -> (old_color: color_tag) [in Lua]```

Returns a codified triple (r,g,b) in a long integer such as 0x00RRGGBB, where RR are the red components, GG are the green ones and BB are the blue ones. The code is used in the CD library to define colors. It can be used without an active canvas.

```void cdDecodeColor(long int color, unsigned char *red, unsigned char *green, unsigned char *blue) [in C]

cd.DecodeColor(color: color_tag) -> (r, g, b: number) [in Lua]```

Returns the red, green and blue components of a color in the CD library. Can be used without an active canvas.

```long int cdEncodeAlpha(long int color, unsigned char alpha) [in C]

cd.EncodeAlpha(color: color_tag, alpha: number) -> (color: color_tag) [in Lua]```

Returns the given color coded with the alpha information. ATENTION: At the moment only the Win32 with GDI+ and the IMAGERGB drivers support alpha components in color coding. Se in Windows Using GDI+ Base Driver and IMAGERGB driver. The internal representation of the component is inverted, because the default value must be 0 and opaque for backward compatibility, so you should use the cdDecodeAlpha function ot the cdAlpha macro to retrieve the alpha component.

```unsigned char cdDecodeAlpha(long int color) [in C]

cd.DecodeAlpha(color: color_tag) -> (a: number) [in Lua]```

Returns the alpha component of a color in the CD library. Can be used without an active canvas. 0 is transparent, 255 is opaque.

```unsigned char cdAlpha(long int color); [in C]

cd.Alpha(color: color_tag) -> (r: number) [in Lua]```

Macro that returns the alpha component of a color in the CD library. Can be used without an active canvas.

```unsigned char cdRed(long int color); [in C]

cd.Red(color: color_tag) -> (r: number) [in Lua]```

Macro that returns the red component of a color in the CD library. Can be used without an active canvas.

```unsigned char cdGreen(long int color); [in C]

cd.Green(color: color_tag) -> (g: number) [in Lua]```

Macro that returns the green component of a color in the CD library. Can be used without an active canvas.

```unsigned char cdBlue(long int color); [in C]

cd.Blue(color: color_tag) -> (b: number) [in Lua]```

Macro that returns the blue component of a color in the CD library. Can be used without an active canvas.

```int cdCanvasGetColorPlanes(cdCanvas* canvas); [in C]

canvas:GetColorPlanes() -> (bpp: number) [in Lua]```

Returns a given number, for instance p, which defines the number of colors supported by the current device as 2p, representing the number of bits by pixel.

```void cdCanvasPalette(cdCanvas* canvas, int n, const long int *color, int mode); [in C]

canvas:Palette(palette: palette_tag; mode: number) [in Lua]```

In systems limited to 256 palette colors, this function aims at addingÂ  n colors to the system's palette. In such systems, the colors demanded forward or backward which are not in the palette are approximated to the closest available color. The type can be CD_FORCE or CD_POLITE. CD_FORCE ignores the system colors and interface elements, since the menus and dialogues may be in illegible colors, but there will be more colors available. CD_POLITE is the recommended type. It must always be used before drawing. It cannot be queried.

`cd.CreatePalette(size: number) -> (palette: palette_tag) [in Lua Only]`

Creates a palette. The object returned can be used as a table to set palette entries (palette[0], palette[1],..), each entry is a color object.

`cd.KillPalette(palette: palette_tag) [in Lua Only]`

Destroys the created palette and liberates allocated memory.