Skip to content

Commit

Permalink
Extracted graphicsBlendColorRGB565 to its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Dec 3, 2024
1 parent 743af5d commit 1c3ae85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
29 changes: 16 additions & 13 deletions libs/graphics/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,20 @@ JsGraphicsSetPixelFn graphicsGetSetPixelUnclippedFn(JsGraphics *gfx, int x1, int
return gfx->setPixel; // fast
}

/// Merge one color into another based RGB565(amt is 0..256)
uint16_t graphicsBlendColorRGB565(uint16_t f, uint16_t b, int amt) {
unsigned int br = (b>>11)&0x1F;
unsigned int bg = (b>>5)&0x3F;
unsigned int bb = b&0x1F;
unsigned int fr = (f>>11)&0x1F;
unsigned int fg = (f>>5)&0x3F;
unsigned int fb = f&0x1F;
unsigned int ri = (br*(256-amt) + fr*amt) >> 8;
unsigned int gi = (bg*(256-amt) + fg*amt) >> 8;
unsigned int bi = (bb*(256-amt) + fb*amt) >> 8;
return (bi | gi<<5 | ri<<11);
}

/// Merge one color into another based on current bit depth (amt is 0..256)
uint32_t graphicsBlendColor(JsGraphics *gfx, unsigned int fg, unsigned int bg, int iamt) {
unsigned int amt = (iamt>0) ? (unsigned)iamt : 0;
Expand All @@ -374,18 +388,7 @@ uint32_t graphicsBlendColor(JsGraphics *gfx, unsigned int fg, unsigned int bg, i
// TODO: if our graphics instance is paletted this isn't correct!
return (bg*(256-amt) + fg*amt + 127) >> 8;
} else if (gfx->data.bpp==16) { // Blend from bg to fg
unsigned int b = bg;
unsigned int br = (b>>11)&0x1F;
unsigned int bg = (b>>5)&0x3F;
unsigned int bb = b&0x1F;
unsigned int f = fg;
unsigned int fr = (f>>11)&0x1F;
unsigned int fg = (f>>5)&0x3F;
unsigned int fb = f&0x1F;
unsigned int ri = (br*(256-amt) + fr*amt) >> 8;
unsigned int gi = (bg*(256-amt) + fg*amt) >> 8;
unsigned int bi = (bb*(256-amt) + fb*amt) >> 8;
return (bi | gi<<5 | ri<<11);
return graphicsBlendColorRGB565(fg,bg,iamt);
#ifdef ESPR_GRAPHICS_12BIT
} else if (gfx->data.bpp==12) { // Blend from bg to fg
unsigned int b = bg;
Expand Down Expand Up @@ -911,7 +914,7 @@ void graphicsScroll(JsGraphics *gfx, int xdir, int ydir) {
static void graphicsDrawString(JsGraphics *gfx, int x1, int y1, const char *str) {
// no need to modify coordinates as setPixel does that
while (*str) {
#ifdef USE_FONT_6X8
#ifdef USE_FONT_6X8
graphicsDrawChar6x8(gfx,x1,y1,*(str++),1,1,false);
x1 = (int)(x1 + 6);
#else
Expand Down
2 changes: 2 additions & 0 deletions libs/graphics/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ void graphicsSetModified(JsGraphics *gfx, int x1, int y1, int x2, int y2);
JsGraphicsSetPixelFn graphicsGetSetPixelFn(JsGraphics *gfx);
/// Get a setPixel function and set modified area (assuming no clipping) (inclusive of x2,y2) - if all is ok it can choose a faster draw function
JsGraphicsSetPixelFn graphicsGetSetPixelUnclippedFn(JsGraphics *gfx, int x1, int y1, int x2, int y2, bool coordsRotatedAlready);
/// Merge one color into another based RGB565(amt is 0..256)
uint16_t graphicsBlendColorRGB565(uint16_t fg, uint16_t bg, int iamt);
/// Merge one color into another based on current bit depth (amt is 0..256)
uint32_t graphicsBlendColor(JsGraphics *gfx, unsigned int fg, unsigned int bg, int iamt);
/// Merge one color into another based on current bit depth (amt is 0..256)
Expand Down

0 comments on commit 1c3ae85

Please sign in to comment.