Color struct samples
This section shows common samples of how to work with colors to manage DIV Games Studio palettes and images.
Remember that DIV Games Studio palettes working in DAC format. This means that the RGB channels ranges are 0 to 63 instead of 0 to 255.
The DIV Games Studio files, PAL, MAP, FPG and FNT files, works in DAC format. If you try to set a full RGB Color value for any related DIV Games Studio file operations, or even try to load a modified file with full RGB Color values, you will gets an exception.
You can use full RGB Color values when you need to export a PAL colors or the bitmap data from a MAP to use in modern systems (e.g. to render a 16x16 PAL color matrix or the bitmap image from a MAP in a custom tool using the common RGB 24/32 bits system GUI, web based GUI, or a custom GUI in major environments like Unity editor custom tool).
Create a color
The common way to create a new color value is using the main constructor:
var color = new Color(0, 31, 63);
Also you can create an empty Color value or pure black color (0, 0, 0) using the default non-parametrized consutructor and setup the color channel values later.
Access color channels values
You can access, to write or read, the each Color channel values: red, green and blue, using the struct fields:
color.red = 0;
color.green = 31;
color.blue = 63;
Console.Write($"Red: {color.red}, Green: {color.green}, Blue: {color.blue}");
// Red: 0, Green: 31, Blue: 63
And also, like a vector structure, you can using a direct accessor by index position:
color[0] = 0;
color[1] = 31;
color[2] = 63;
Console.Write($"Red: {color[0]}, Green: {color[1]}, Blue: {color[2]}");
// Red: 0, Green: 31, Blue: 63
Check if the color is a valid DAC value
You can check if a Color value is a DAC value using the following function:
bool isDACColor = color.IsDAC();
Note
This function only checks if the color channels values are bounded in the DAC range [0..63]. There is not way to ensure if a color value is a true DAC or full RGB value [0..255].
Convert a DAC color to RGB equivalent
You can convert any DAC color to his equivalent RGB value using the following function. Remember that the value is a closer approximation of the real RGB value:
Color rgbColor = color.ToRGB();
Convert a RGB color to DAC equivalent
You can convert any RGB color to his equivalent DAC value using the following function. Remember that the value is a closer approximation of the real DAC value:
Color dacColor = color.ToDAC();
Using an integer value as a Color struct value and viceversa
The Color structure can be casted to integer values and viceversa. This is useful in some scenaries where you need to implements algorithms that are more effective working with simple numbers than a structures, for example, for sorting processes or similar actions.
You can cast an integer value as Color simply assigning the value using the assignation operator:
Color color = 33023;
Console.Write(color);
// { Color: { Red: 0, Green: 128, Blue: 255 } }
And viceversa, you can cast a Color value to an integer value in the similar way:
int color = (int)new Color(0, 128, 255);
Console.Write(color);
// 33023