Numworks Epsilon  1.4.1
Graphing Calculator Operating System
color.cpp
Go to the documentation of this file.
1 #include <kandinsky/color.h>
2 
3 KDColor KDColor::blend(KDColor first, KDColor second, uint8_t alpha) {
4  /* This function is a hot path since it's being called for every single pixel
5  * whenever we want to display a string. In this context, we're quite often
6  * calling it with a value of either 0 or 0xFF, which can be very trivially
7  * dealt with. So let's make a special case for them. */
8  if (alpha == 0) {
9  return second;
10  } else if (alpha == 0xFF) {
11  return first;
12  }
13 
14  // We want to do first*alpha + second*(1-alpha)
15  // First is RRRRR GGGGGG BBBBB
16  // Second is same
17 
18  uint8_t oneMinusAlpha = 0xFF-alpha;
19  uint16_t red = first.red()*alpha + second.red()*oneMinusAlpha;
20  uint16_t green = first.green()*alpha + second.green()*oneMinusAlpha;
21  uint16_t blue = first.blue()*alpha + second.blue()*oneMinusAlpha;
22  return RGB888(red>>8, green>>8, blue>>8);
23 
24 
25  // Blend White + black, ask for white
26  // white.red() = 0x1F << 3 = 0xF8
27 // white.red() * 0xFF = 0xF708, we wanted 0xF800
28 }
static constexpr KDColor RGB888(uint8_t r, uint8_t g, uint8_t b)
Definition: color.h:16
unsigned short uint16_t
Definition: stdint.h:5
unsigned char uint8_t
Definition: stdint.h:4
uint8_t green() const
Definition: color.h:24
uint8_t red() const
Definition: color.h:19
Definition: color.h:6
uint8_t blue() const
Definition: color.h:29
static KDColor blend(KDColor first, KDColor second, uint8_t alpha)
Definition: color.cpp:3