Last active
March 30, 2025 17:34
-
-
Save mattiasgustavsson/c11e824e3d603d0c86e5e0dde4ecf839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// color1 and color2 are R4G4B4 12bit RGB color values, alpha is 0-255 | |
uint16_t blend_12bit( uint16_t color1, uint16_t color2, uint8_t alpha ) { | |
uint64_t c1 = (uint64_t) color1; | |
uint64_t c2 = (uint64_t) color2; | |
uint64_t a = (uint64_t)( alpha >> 4 ); | |
// bit magic to alpha blend R G B with single mul | |
c1 = ( c1 | ( c1 << 12 ) ) & 0x0f0f0f; | |
c2 = ( c2 | ( c2 << 12 ) ) & 0x0f0f0f; | |
uint32_t o = ( ( ( ( c2 - c1 ) * a ) >> 4 ) + c1 ) & 0x0f0f0f; | |
return (uint16_t) ( o | ( o >> 12 ) ); | |
} | |
// color1 and color2 are R5G6B5 16bit RGB color values, alpha is 0-255 | |
uint16_t blend_16bit( uint16_t color1, uint16_t color2, uint8_t alpha ) { | |
uint32_t c1 = (uint32_t) color1; | |
uint32_t c2 = (uint32_t) color2; | |
uint32_t a = (uint32_t)( alpha >> 3 ); | |
// bit magic to alpha blend R G B with single mul | |
c1 = ( c1 | ( c1 << 16 ) ) & 0x07e0f81fu; | |
c2 = ( c2 | ( c2 << 16 ) ) & 0x07e0f81fu; | |
uint32_t o = ( ( ( ( c2 - c1 ) * a ) >> 5 ) + c1 ) & 0x07e0f81fu; | |
return (uint16_t) ( o | ( o >> 16 ) ); | |
} | |
// color1 and color2 are R8G8B8 24bit RGB color values, alpha is 0-255 | |
uint32_t blend_24bit( uint32_t color1, uint32_t color2, uint8_t alpha ) { | |
uint64_t c1 = (uint64_t) color1; | |
uint64_t c2 = (uint64_t) color2; | |
uint64_t a = (uint64_t) alpha; | |
// bit magic to alpha blend R G B with single mul | |
c1 = ( c1 | ( c1 << 24 ) ) & 0x00ff00ff00ffull; | |
c2 = ( c2 | ( c2 << 24 ) ) & 0x00ff00ff00ffull; | |
uint64_t o = ( ( ( ( c2 - c1 ) * a ) >> 8 ) + c1 ) & 0x00ff00ff00ffull; | |
return (uint32_t) ( o | ( o >> 24 ) ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/mattiasgustavsson/c11e824e3d603d0c86e5e0dde4ecf839#file-rgb_blend-c-L31 recommend renaming arg to alpha