canidlogic / vgapal

Generate the default VGA 256-color palette.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

vgapal

Generate the default VGA 256-color palette.

This is the palette that would be initially loaded in the Digital Analog Converters (DACs) when a VGA card is switched into 256-color Mode 13h using the VGA BIOS at interrupt 10h.

VGA cards used 6 bits per RGB channel in the DACs, so the intensity levels of red, green, and blue components range from 0-63 decimal, rather than the more typical modern 8-bit range of 0-255. The VGAPAL program is able to upconvert from the original 6-bit components to 8-bit components by shifting 6-bit component values two bits to the left and duplicating the two most significant bits as the two least significant bits. Hence, the VGAPAL program can generate both the original 64-level (6-bit) RGB palette, and a modernized 256-level (8-bit) RGB palette.

The VGAPAL program generates a 64-level (6-bit) 256-color palette that exactly matches the "default_palette" in the Allegro 4.4.2 library (source file "allegro.c"), as well as the "vga_palette" in the DOSBox 0.74 DOS emulator, which includes VGA card emulation (source file "int10_modes.cpp"). The only exception is that the very last color in the palette is full white according to Allegro but full black according to DOSBox. VGAPAL has the last eight colors as full black, matching DOSBox.

Wikipedia references a "Default VGA 256-color palette" posted by user "Psychonaut" on Wikimedia Commons. Examining the source code used to generate the diagram indicates that 8-bit, 256-level colors were used for the palette instead of the original 6-bit, 64-level colors. Although the palette posted by Psychonaut appears to have the same structure as the palette generated by VGAPAL, the upconverted 8-bit palette generated by VGAPAL does not exactly match the 8-bit RGB channel values that Psychonaut uses. Unfortunately, Psychonaut does not specify how the 6-bit RGB palette values were upconverted to 8-bit, nor is the source of the palette colors specified, so it is difficult to analyze the discrepancy further. Similar to DOSBox, Psychonaut has the last color of the palette as full black, which matches VGAPAL.

The first 16 colors of the generated palette match the colors used in 16-color VGA modes. To decode these colors, consider the four least significant bits of the palette index to be IRGB, in order from most significant to least significant. The RGB bits turn on the relevant RGB channel while the I bit makes the resulting color brighter. The only exception to these rules is that what would normally be dark yellow (I bit OFF, RG bits ON, B bit OFF) is modified slightly to make it brown instead.

The next 16 colors of the generated palette are a grayscale gradient ranging from full black to full white.

The rest of the palette after the grayscale gradient is occupied by nine "cycles," each consisting of 24 colors. Each cycle consists of six "runs" of four colors each. Each run gradually changes one of the RGB channels from "high" intensity to "low" intensity or from "low" intensity to "high" intensity, with the definition of "low" and "high" varying depending on the particular cycle. The pattern is that each cycle starts at blue high and red and green low. The first run changes red from low to high. The second run changes blue from high to low. The third run changes green from low to high. The fourth run changes red from high to low. The fifth run changes blue from low to high. And, finally, the sixth run changes green from high to low. At the end of the sixth run, we're back where we started from with blue high and red and green low, thereby completing the cycle.

The effect of the cycle is to run through all the hues in a hue-saturation-value (HSV) color space.

The nine cycles differ from each other in their definition of "high" and "low" (as discussed earlier), as well as in what intermediate values they use to transition between high and low during runs. These values are defined so as to vary the saturation and value of each of the nine cycles.

The nine cycles are organized into three subgroups, each containing three cycles. The first subgroup has high defined as full intensity, yielding high value for the colors in these cycles. The second subgroup has high defined as moderate intensity, yielding moderate value for the colors in these cycles. The third subgroup has high defined as low intensity, yielding low value for the colors in these cycles.

Within each subgroup, the first cycle has low defined as zero intensity, yielding maximum saturation for the colors in the first cycle of each subgroup. The second cycle has low defined halfway between zero and high intensity, yielding moderate saturation for the colors in the second cycle of each subgroup. Finally, the third cycle has low defined close to high intensity, yielding low saturation for the colors in the third cycle of each subgroup.

The 16-color IRGB palette, the 16-color grayscale palette, and the nine RGB cycles leave eight colors left over at the end of the 256-color palette. These are simply filled with full black.

In essence, the default VGA 256-color palette consists of a 16-color IRGB palette (for backwards compatibility with 16-color modes), followed by a grayscale palette and RGB cycles designed to cover the HSV color space. Considering the HSV color space in its usual cone geometry, the grayscale palette represents the neutral, unsaturated colors at the center of the cone, while the RGB cycles represent the rest of the colors, radiating out from the center of the cone.

About

Generate the default VGA 256-color palette.

License:MIT License


Languages

Language:C 100.0%