Last active
December 5, 2016 22:20
-
-
Save dmadison/0dcd929cd5ce73a66a120398223dfc08 to your computer and use it in GitHub Desktop.
Example of a basic RGB color blending function using floats. Takes two packed 32-bit RGB colors and a percentage byte as an input and returns a single 32-bit color as an output.
This file contains hidden or 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
uint32_t blendRGB(uint32_t c1, uint32_t c2, uint8_t p){ | |
/* Blends two RGB color values using a linear algorithm | |
* in the form of y = mx + b. | |
* | |
* Where y is the resultant blended color (per channel), m | |
* is the slope of the color change divided by the number of steps, | |
* x is the current step, and b is the initial channel value. | |
* | |
* This implementation uses a single byte for gradation, giving steps from 0-255. | |
*/ | |
float mR, mG, mB; | |
uint8_t newR, newG, newB; | |
mR = (float)(((uint8_t)(c2 >> 16)) - ((uint8_t)(c1 >> 16))) / 255; | |
mG = (float)(((uint8_t)(c2 >> 8)) - ((uint8_t)(c1 >> 8))) / 255; | |
mB = (float)(((uint8_t)(c2)) - ((uint8_t)(c1))) / 255; | |
newR = (mR * p) + (uint8_t)(c1 >> 16); | |
newG = (mG * p) + (uint8_t)(c1 >> 8); | |
newB = (mB * p) + (uint8_t)(c1); | |
return ((uint32_t)newR << 16) | ((uint32_t)newG << 8) | newB; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment