fork download
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. #pragma pack( push, 1 )
  5. struct rgb565_t
  6. {
  7. uint8_t r : 5;
  8. uint8_t g : 6;
  9. uint8_t b : 5;
  10.  
  11. operator uint16_t() const
  12. {
  13. return (r << 11) | (g << 5) | b;
  14. }
  15.  
  16. rgb565_t lerp( const rgb565_t target, const float step ) const
  17. {
  18. if ( step <= 0.0f ) return *this;
  19. if ( step >= 1.0f ) return target;
  20.  
  21. return
  22. {
  23. (uint8_t)round( r + ( ( target.r - r ) * step ) ),
  24. (uint8_t)round( g + ( ( target.g - g ) * step ) ),
  25. (uint8_t)round( b + ( ( target.b - b ) * step ) ),
  26. };
  27. }
  28. };
  29. #pragma pack( pop )
  30.  
  31. int main()
  32. {
  33. rgb565_t color_start = { 0, 0, 0 };
  34. rgb565_t color_end = { 31, 63, 31 };
  35.  
  36. for ( float step = 0.0f; step <= 1.0f; step += 0.01f )
  37. {
  38. rgb565_t color = color_start.lerp( color_end, step );
  39. printf( "{ %2hhu, %2hhu, %2hhu }\n", color.r, color.g, color.b );
  40. }
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 5420KB
stdin
Standard input is empty
stdout
{  0,  0,  0 }
{  0,  1,  0 }
{  1,  1,  1 }
{  1,  2,  1 }
{  1,  3,  1 }
{  2,  3,  2 }
{  2,  4,  2 }
{  2,  4,  2 }
{  2,  5,  2 }
{  3,  6,  3 }
{  3,  6,  3 }
{  3,  7,  3 }
{  4,  8,  4 }
{  4,  8,  4 }
{  4,  9,  4 }
{  5,  9,  5 }
{  5, 10,  5 }
{  5, 11,  5 }
{  6, 11,  6 }
{  6, 12,  6 }
{  6, 13,  6 }
{  7, 13,  7 }
{  7, 14,  7 }
{  7, 14,  7 }
{  7, 15,  7 }
{  8, 16,  8 }
{  8, 16,  8 }
{  8, 17,  8 }
{  9, 18,  9 }
{  9, 18,  9 }
{  9, 19,  9 }
{ 10, 20, 10 }
{ 10, 20, 10 }
{ 10, 21, 10 }
{ 11, 21, 11 }
{ 11, 22, 11 }
{ 11, 23, 11 }
{ 11, 23, 11 }
{ 12, 24, 12 }
{ 12, 25, 12 }
{ 12, 25, 12 }
{ 13, 26, 13 }
{ 13, 26, 13 }
{ 13, 27, 13 }
{ 14, 28, 14 }
{ 14, 28, 14 }
{ 14, 29, 14 }
{ 15, 30, 15 }
{ 15, 30, 15 }
{ 15, 31, 15 }
{ 15, 31, 15 }
{ 16, 32, 16 }
{ 16, 33, 16 }
{ 16, 33, 16 }
{ 17, 34, 17 }
{ 17, 35, 17 }
{ 17, 35, 17 }
{ 18, 36, 18 }
{ 18, 37, 18 }
{ 18, 37, 18 }
{ 19, 38, 19 }
{ 19, 38, 19 }
{ 19, 39, 19 }
{ 20, 40, 20 }
{ 20, 40, 20 }
{ 20, 41, 20 }
{ 20, 42, 20 }
{ 21, 42, 21 }
{ 21, 43, 21 }
{ 21, 43, 21 }
{ 22, 44, 22 }
{ 22, 45, 22 }
{ 22, 45, 22 }
{ 23, 46, 23 }
{ 23, 47, 23 }
{ 23, 47, 23 }
{ 24, 48, 24 }
{ 24, 49, 24 }
{ 24, 49, 24 }
{ 24, 50, 24 }
{ 25, 50, 25 }
{ 25, 51, 25 }
{ 25, 52, 25 }
{ 26, 52, 26 }
{ 26, 53, 26 }
{ 26, 54, 26 }
{ 27, 54, 27 }
{ 27, 55, 27 }
{ 27, 55, 27 }
{ 28, 56, 28 }
{ 28, 57, 28 }
{ 28, 57, 28 }
{ 29, 58, 29 }
{ 29, 59, 29 }
{ 29, 59, 29 }
{ 29, 60, 29 }
{ 30, 60, 30 }
{ 30, 61, 30 }
{ 30, 62, 30 }
{ 31, 62, 31 }
{ 31, 63, 31 }