Skip to content

Instantly share code, notes, and snippets.

@vankxr
Created August 28, 2019 02:13
Show Gist options
  • Save vankxr/148e901528938d1bbd750ae7f6c437ca to your computer and use it in GitHub Desktop.
Save vankxr/148e901528938d1bbd750ae7f6c437ca to your computer and use it in GitHub Desktop.
Color alpha blending
#include <stdio.h>
#include <stdint.h>
uint32_t blend(uint32_t base, uint32_t new, float alpha)
{
uint8_t base_r = (base >> 16) & 0xFF;
uint8_t base_g = (base >> 8) & 0xFF;
uint8_t base_b = (base >> 0) & 0xFF;
uint8_t new_r = (new >> 16) & 0xFF;
uint8_t new_g = (new >> 8) & 0xFF;
uint8_t new_b = (new >> 0) & 0xFF;
uint8_t final_r = base_r + ((float)new_r - (float)base_r) * alpha;
uint8_t final_g = base_g + ((float)new_g - (float)base_g) * alpha;
uint8_t final_b = base_b + ((float)new_b - (float)base_b) * alpha;
return ((uint32_t)final_r << 16) | ((uint32_t)final_g << 8) | ((uint32_t)final_b << 0);
}
int main()
{
uint32_t base = 0xFFFFFF;
uint32_t new = 0xFB00FF;
float alpha = 0.5f;
printf("base: %06X\n", base);
printf("new: %06X\n", new);
printf("final: %06X\n", blend(base, new, alpha));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment