PAL class samples
This section shows common samples of how to create and manage DIV Games Studio palettes.
Note
Usually is not necessary to edit the Color Range Tables in a palette to make it usable in games. Color Range Tables are used by the DIV Games Studio built-in Drawing Editor. The default Color Range Tables initialization performed by this framework is enough to use in this program. We are skipped all operations related with Color Range Tables in palettes in this section.
Create an empty palette
This code shows how to create an empty PAL instance and setup colors manually:
var palette = new PAL();
palette[0] = new Color(0, 0, 0);
palette[1] = new Color(63, 0, 0);
palette[2] = new Color(63, 63, 63);
...
Also you can setup a Color array to initialize a new palette:
var colors = new Color[256];
... // Sets each Color array element value.
var palette = new PAL(colors);
Warning
Remember that DIV Games Studio palettes working in DAC format. This means that the RGB channel ranges are 0 to 63 instead of 0 to 255. If you trying to initialize a palette using values over 63 for any color channel, you thrown an exception.
Load an existing palette
This code shows how to load a PAL file:
var palette = new PAL("DIV.PAL");
Also you can load a PAL file from a byte array:
byte[] buffer = System.IO.File.ReadAllBytes("DIV.PAL");
var palette = new PAL(buffer);
Create a palette from an image
You can create palettes from a image file. The process allows you to load an image file (JPEG, PNG, BMP, GIF, and TGA image formats, and also supported 256 colors PCX files), extract all unique colors from the image, and convert them to 8 bit DAC format.
var palette = PAL.FromImage("background.png");
Also you can create the palette from a byte array with the content of the image file:
byte[] buffer = System.IO.File.ReadAllBytes("background.png");
var palette = PAL.FromImage(buffer);
You can perform a sorting color action when creates the palette from a image:
var palette = PAL.FromImage("background.png", sortColors: true);
byte[] buffer = System.IO.File.ReadAllBytes("background.png");
var palette = PAL.FromImage(buffer, sortColors: true);
Note
Not is a requirement to sort the colors in a palette but is recomended to ensure that the black color, if is present in the palette, was the first color (index zero). DIV Games Studio draw operations usually uses the first color in a palette, the black, as transparent color for sprite masks.
Extract palettes from MAP and FPG files
You can create the palette from the existing one from a MAP or FPG file like you load a supported image file:
var palette = PAL.FromImage("COIN.MAP");
var palette = PAL.FromImage("PLAYER.FPG");
Read colors
You can easily read each color of the palette using a direct accessor, like an array:
Color color = palette[42]; // Reads the color at index 42.
And also, you can use a foreach loop to read all colors:
foreach (Color color in palette)
{
Console.WriteLine(color); // Prints the current color value in console.
}
Write colors
You can easily write each color of the palette using a direct accessor, like an array:
palette[42] = new Color(0, 16, 63); // Writes the color at index 42.
Warning
Remember that DIV Games Studio palettes working in DAC format. This means that the RGB channel ranges are 0 to 63 instead of 0 to 255. If you trying to set a value over 63 for any color channel, you thrown an exception.
Sort colors
You can manually perform a color sorting in your palettes. This action trying to sort the colors, from the black, or the darkest color in palette, to the white or the brightest color in the palette:
palette.Sort();
Note
Not is a requirement to sort the colors in a palette but is recomended to ensure that the black color, if is present in the palette, was the first color (index zero). DIV Games Studio draw operations usually uses the first color in a palette, the black, as transparent color for sprite masks.
Save palette to a file
You can easily save your palette to a PAL file using the following call:
palette.Save("NEW.PAL");
Get full RGB colors
If you need, for example, render the 16x16 palette color matrix in a custom tool, that works with full RGB 24/32 bits, you can get a copy of the all colors in RGB format [0..255] using a simple call:
Color[] rgbPaletteColors = palette.ToRGB();