LPC1343 Graphics Sub-System: Colors and Alpha-Blending (colors.c)
Summary of the color conversion and alpha-blending support in the LPC1343 Code Base (v1.1.0+)
The following functions facilitate drawing more complex compound shapes, and allow you to change the look and feel of the specific elements in one location, or wrap any user interaction logic into the control code itself without messing up the rest of the low-level rendering code.
uint16_t colorsRGB24toRGB565(uint8_t r, uint8_t g, uint8_t b)
Converts a 24-bit RGB color to an equivalent 16-bit RGB565 value
// Get 16-bit equivalent of 24-bit color
uint16_t gray = colorsRGB24toRGB565(0x33, 0x33, 0x33);
uint32_t colorsRGB565toBGRA32(uint16_t color)
Converts a 16-bit RGB565 color to a standard 32-bit BGRA32 color (with alpha set to 0xFF)
// First convert 24-bit color to RGB565
uint16_t rgb565 = colorsRGB24toRGB565(0xFF, 0x00, 0x00);
// Convert RGB565 color back to BGRA32
uint32_t bgra32 = colorsRGB565toBGRA32(rgb565);
// Display results
printf("BGRA32: 0x%08X R: %u G: %u B: %u A: %u \r\n",
(bgra32 & 0x000000FF), // Blue
(bgra32 & 0x0000FF00) >> 8, // Green
(bgra32 & 0x00FF0000) >> 16, // Red
(bgra32 & 0xFF000000) >> 24); // Alpha
uint16_t colorsBGR2RGB(uint16_t color)
Reverses a 16-bit color from BGR to RGB or vice verse
uint16_t colorsDim(uint16_t color, uint8_t intensity)
Adjusts the supplied color to have the specified intensity (0..100). 100 will leave the color as is at full intensity, 50 will reduce the color intensity by half, and 0 will return black.
This function is useful for anti-aliasing and sub-pixel rendering since colors are returned as a percentage of the original value, depending on the amount of space they take up in the sub-pixel array.
// Draw a pure red rectangle
drawRectangleFilled(10, 10, 200, 100, COLOR_RED);
// Draw a rectangle at 50% intensity red
newColor = colorsDim(COLOR_RED, 50);
drawRectangleFilled(20, 20, 190, 90, newColor);
// Draw a rectangle at 25% intensity red
newColor = colorsDim(COLOR_RED, 25);
drawRectangleFilled(30, 30, 180, 80, newColor);
// Draw a rectangle at 0% intensity red
newColor = colorsDim(COLOR_RED, 0);
drawRectangleFilled(40, 40, 170, 70, newColor);
uint16_t colorsAlphaBlend(uint16_t bgColor, uint16_t foreColor, uint8_t fadePercent)
Returns an alpha-blended color based on the supplied bg color, fore color, and intensity value (0..100).
This function is used when alpha-blending anti-aliased fonts with an existing background image, amongst other things, and can be used to create images that appear to be 'faded' or semi-transparent, though at the expense of slow updates since reading pixels from most LCD controllers is an extremely expensive operation.
fadePercent: Visibility of the background color in percent (0..100). The higher the number, the more visible the back color becomes. 100% signifies that the back color is entirely visible (only the BG color is shown), 0% signifies that only the fore color is shown, and 25% would indicate that the background is visible at approximately 25% intensity (combined with 75% of the fore color).
uint16_t bg = COLOR_GREEN;
uint16_t fore = COLOR_WHITE;
// Calculate the intermediate color with 25% bg blending
uint16_t result = colorsAlphaBlend(bg, fore, 25);
drawRectangleFilled(10, 10, 50, 50, bg);
drawRectangleFilled(60, 10, 100, 50, fore);
drawRectangleFilled(35, 60, 75, 100, result);