-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKDColor.cpp
28 lines (25 loc) · 1.05 KB
/
KDColor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "KDColor.h"
KDColor KDColor::blend(KDColor first, KDColor second, uint8_t alpha) {
/* This function is a hot path since it's being called for every single pixel
* whenever we want to display a string. In this context, we're quite often
* calling it with a value of either 0 or 0xFF, which can be very trivially
* dealt with. Similarly, blending the same two colors yields a trivial
* result and can be bypassed. Let's make a special case for them. */
if (alpha == 0) {
return second;
}
if (alpha == 0xFF) {
return first;
}
if (first == second) {
return first;
}
// We want to do first*alpha + second*(1-alpha)
// First is RRRRR GGGGGG BBBBB
// Second is same
uint16_t oneMinusAlpha = 0x100 - alpha;
uint16_t red = first.red() * alpha + second.red() * oneMinusAlpha;
uint16_t green = first.green() * alpha + second.green() * oneMinusAlpha;
uint16_t blue = first.blue() * alpha + second.blue() * oneMinusAlpha;
return RGB888(red >> 8, green >> 8, blue >> 8);
}