acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

alphaBlend_Test.ino (6866B)

      1 /*
      2   This tests the alpha blending function that is used with the anti-aliased
      3   fonts:
      4 
      5   Alpha = 0 = 100% background, alpha = 255 = 100% foreground colour
      6 
      7   blendedColor = tft.alphaBlend(alpha, fg_color, bg_color);
      8 
      9   The alphaBlend() function operates on 16 bit colours only
     10   A test is included where the colours are mapped to 8 bits after blending
     11 
     12   Information on alpha blending is here
     13   https://en.wikipedia.org/wiki/Alpha_compositing
     14   
     15   Example for library:
     16   https://github.com/Bodmer/TFT_eSPI
     17 
     18   The sketch has been tested on a 320x240 ILI9341 based TFT, it
     19   could be adapted for other screen sizes.
     20 
     21   Created by Bodmer 10/2/18
     22 
     23   #########################################################################
     24   ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
     25   #########################################################################
     26 */
     27 
     28 #include <TFT_eSPI.h>       // Include the graphics library
     29 
     30 TFT_eSPI tft = TFT_eSPI();  // Create object "tft"
     31 
     32 // -------------------------------------------------------------------------
     33 // Setup
     34 // -------------------------------------------------------------------------
     35 void setup(void) {
     36   tft.init();
     37   tft.setRotation(0);
     38   tft.fillScreen(TFT_DARKGREY);
     39 }
     40 
     41 // -------------------------------------------------------------------------
     42 // Main loop
     43 // -------------------------------------------------------------------------
     44 void loop()
     45 {
     46   // 16 bit colours  (5 bits red, 6 bits green, 5 bits blue)
     47   // Blend from white to full spectrum
     48   for (int a = 0; a < 256; a+=2) // Alpha 0 = 100% background, alpha 255 = 100% foreground
     49   {
     50     for (int c = 0; c < 192; c++) tft.drawPixel(c, a/2, tft.alphaBlend(a, rainbow(c), TFT_WHITE));
     51   }
     52 
     53   // Blend from full spectrum to black
     54   for (int a = 255; a > 2; a-=2)
     55   {
     56     for (int c = 0; c < 192; c++) tft.drawPixel(c, 128 + (255-a)/2, tft.alphaBlend(a, rainbow(c), TFT_BLACK));
     57   }
     58 
     59   // Blend from white to black (32 grey levels)
     60   for (uint16_t a = 0; a < 255; a++) // Alpha 0 = 100% background, alpha 255 = 100% foreground
     61   {
     62     tft.drawFastHLine(192, a, 12, tft.alphaBlend(a, TFT_BLACK, TFT_WHITE));
     63     tft.drawFastHLine(204, a, 12, tft.alphaBlend(a, TFT_BLACK, TFT_RED));
     64     tft.drawFastHLine(216, a, 12, tft.alphaBlend(a, TFT_BLACK, TFT_GREEN));
     65     tft.drawFastHLine(228, a, 12, tft.alphaBlend(a, TFT_BLACK, TFT_BLUE));
     66   }
     67 
     68   delay(4000);
     69 
     70   // Blend from white to colour (32 grey levels)
     71   for (uint16_t a = 0; a < 255; a++) // Alpha 0 = 100% background, alpha 255 = 100% foreground
     72   {
     73     //tft.drawFastHLine(192, a, 12, tft.alphaBlend(a, TFT_BLACK, TFT_WHITE));
     74     tft.drawFastHLine(204, a, 12, tft.alphaBlend(a, TFT_RED,   TFT_WHITE));
     75     tft.drawFastHLine(216, a, 12, tft.alphaBlend(a, TFT_GREEN, TFT_WHITE));
     76     tft.drawFastHLine(228, a, 12, tft.alphaBlend(a, TFT_BLUE,  TFT_WHITE));
     77   }
     78 
     79   delay(4000);
     80 
     81   //*
     82   // Decrease to 8 bit colour (3 bits red, 3 bits green, 2 bits blue)
     83   // Blend from white to full spectrum
     84   for (int a = 0; a < 256; a+=2) // Alpha 0 = 100% background, alpha 255 = 100% foreground
     85   {
     86     // Convert blended 16 bit colour to 8 bits to reduce colour resolution, then map back to 16 bits for displaying
     87     for (int c = 0; c < 192; c++) tft.drawPixel(c, a/2, tft.color8to16(tft.color16to8(tft.alphaBlend(a, rainbow(c), 0xFFFF))));
     88   }
     89 
     90   // Blend from full spectrum to black
     91   for (int a = 255; a > 2; a-=2)
     92   {
     93     // Convert blended 16 bit colour to 8 bits to reduce colour resolution, then map back to 16 bits for displaying
     94     for (int c = 0; c < 192; c++) tft.drawPixel(c, 128 + (255-a)/2, tft.color8to16(tft.color16to8(tft.alphaBlend(a, rainbow(c), 0))));
     95   }
     96 
     97   // Blend from white to black (4 grey levels - it will draw 4 more with a blue tinge due to lower blue bit count)
     98   // Blend from black to a primary colour
     99   for (uint16_t a = 0; a < 255; a++) // Alpha 0 = 100% background, alpha 255 = 100% foreground
    100   {
    101     tft.drawFastHLine(192, a, 12, tft.color8to16(tft.color16to8(tft.alphaBlend(a, TFT_BLACK, TFT_WHITE))));
    102     tft.drawFastHLine(204, a, 12, tft.color8to16(tft.color16to8(tft.alphaBlend(a, TFT_BLACK, TFT_RED))));
    103     tft.drawFastHLine(216, a, 12, tft.color8to16(tft.color16to8(tft.alphaBlend(a, TFT_BLACK, TFT_GREEN))));
    104     tft.drawFastHLine(228, a, 12, tft.color8to16(tft.color16to8(tft.alphaBlend(a, TFT_BLACK, TFT_BLUE))));
    105   }
    106 
    107   delay(4000);
    108   //*/
    109 
    110   /*
    111   // 16 bit colours  (5 bits red, 6 bits green, 5 bits blue)
    112   for (int a = 0; a < 256; a+=2) // Alpha 0 = 100% background, alpha 255 = 100% foreground
    113   {
    114     for (int c = 0; c < 192; c++) tft.drawPixel(c, a/2, tft.alphaBlend(a, rainbow(c), TFT_CYAN));
    115   }
    116 
    117   // Blend from full spectrum to cyan
    118   for (int a = 255; a > 2; a-=2)
    119   {
    120     for (int c = 0; c < 192; c++) tft.drawPixel(c, 128 + (255-a)/2, tft.alphaBlend(a, rainbow(c), TFT_YELLOW));
    121   }
    122   //*/
    123 
    124   /*
    125   // Blend other colour transitions for test purposes
    126   for (uint16_t a = 0; a < 255; a++) // Alpha 0 = 100% background, alpha 255 = 100% foreground
    127   {
    128     tft.drawFastHLine(192, a, 12, tft.alphaBlend(a, TFT_WHITE,  TFT_WHITE));  // Should show as solid white
    129     tft.drawFastHLine(204, a, 12, tft.alphaBlend(a, TFT_BLACK,  TFT_BLACK));  // Should show as solid black
    130     tft.drawFastHLine(216, a, 12, tft.alphaBlend(a, TFT_YELLOW, TFT_CYAN));   // Brightness should be fairly even
    131     tft.drawFastHLine(228, a, 12, tft.alphaBlend(a, TFT_CYAN,   TFT_MAGENTA));// Brightness should be fairly even
    132   }
    133 
    134   delay(4000);
    135   //*/
    136 }
    137 
    138 
    139 // #########################################################################
    140 // Return a 16 bit rainbow colour
    141 // #########################################################################
    142 unsigned int rainbow(byte value)
    143 {
    144   // If 'value' is in the range 0-159 it is converted to a spectrum colour
    145   // from 0 = red through to 127 = blue to 159 = violet
    146   // Extending the range to 0-191 adds a further violet to red band
    147  
    148   value = value%192;
    149   
    150   byte red   = 0; // Red is the top 5 bits of a 16 bit colour value
    151   byte green = 0; // Green is the middle 6 bits, but only top 5 bits used here
    152   byte blue  = 0; // Blue is the bottom 5 bits
    153 
    154   byte sector = value >> 5;
    155   byte amplit = value & 0x1F;
    156 
    157   switch (sector)
    158   {
    159     case 0:
    160       red   = 0x1F;
    161       green = amplit; // Green ramps up
    162       blue  = 0;
    163       break;
    164     case 1:
    165       red   = 0x1F - amplit; // Red ramps down
    166       green = 0x1F;
    167       blue  = 0;
    168       break;
    169     case 2:
    170       red   = 0;
    171       green = 0x1F;
    172       blue  = amplit; // Blue ramps up
    173       break;
    174     case 3:
    175       red   = 0;
    176       green = 0x1F - amplit; // Green ramps down
    177       blue  = 0x1F;
    178       break;
    179     case 4:
    180       red   = amplit; // Red ramps up
    181       green = 0;
    182       blue  = 0x1F;
    183       break;
    184     case 5:
    185       red   = 0x1F;
    186       green = 0;
    187       blue  = 0x1F - amplit; // Blue ramps down
    188       break;
    189   }
    190 
    191   return red << 11 | green << 6 | blue;
    192 }
    193 
    194