Advertisement
honey_the_codewitch

palette example

Mar 26th, 2022
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. template<typename PixelType>
  2. struct waveshare5in65f_palette {
  3. private:
  4.     constexpr static gfx::gfx_result index_to_mapped(int idx,PixelType* result) {
  5.         switch(idx) {
  6.             case 0:
  7.                 return gfx::convert(gfx::rgb_pixel<16>(0,0,0),result);
  8.             case 1:
  9.                 return gfx::convert(gfx::rgb_pixel<16>(31,63,31),result);
  10.             case 2:
  11.                 return gfx::convert(gfx::rgb_pixel<16>(0,63,0),result);
  12.             case 3:
  13.                 return gfx::convert(gfx::rgb_pixel<16>(0,0,31),result);
  14.             case 4:
  15.                 return gfx::convert(gfx::rgb_pixel<16>(31,0,0),result);
  16.             case 5:
  17.                 return gfx::convert(gfx::rgb_pixel<16>(31,63,0),result);
  18.             default: //case 6:
  19.                 return gfx::convert(gfx::rgb_pixel<16>(31,31,0),result);
  20.         }
  21.     }
  22. public:
  23.     using type = waveshare5in65f_palette;
  24.     using pixel_type = gfx::pixel<gfx::channel_traits<gfx::channel_name::index,4,0,6>>;
  25.     using mapped_pixel_type = PixelType;
  26.     constexpr static const bool writable = false;
  27.     constexpr static const size_t size = 7;
  28.     gfx::gfx_result map(pixel_type pixel,mapped_pixel_type* mapped_pixel) const {
  29.         return index_to_mapped(pixel.channel<gfx::channel_name::index>(),mapped_pixel);
  30.     }
  31.     gfx::gfx_result nearest(mapped_pixel_type mapped_pixel,pixel_type* pixel) const {
  32.         if(nullptr==pixel) {
  33.             return gfx::gfx_result::invalid_argument;
  34.         }
  35.         mapped_pixel_type mpx;
  36.         gfx::gfx_result r = index_to_mapped(0,&mpx);
  37.         if(gfx::gfx_result::success!=r) {
  38.             return r;
  39.         }
  40.         double least = mpx.difference(mapped_pixel);
  41.         if(0.0==least) {
  42.             pixel->native_value = 0;
  43.             return gfx::gfx_result::success;
  44.         }
  45.         int ii=0;
  46.         for(int i = 1;i<size;++i) {
  47.             r=index_to_mapped(i,&mpx);
  48.             if(gfx::gfx_result::success!=r) {
  49.                 return r;
  50.             }
  51.             double cmp = mpx.difference(mapped_pixel);
  52.             if(0.0==cmp) {
  53.                 ii=i;
  54.                 least = 0.0;
  55.                 break;
  56.             }
  57.             if(cmp<least) {
  58.                 least = cmp;
  59.                 ii=i;
  60.             }
  61.         }
  62.    
  63.         pixel->channel<gfx::channel_name::index>(ii);
  64.         return gfx::gfx_result::success;
  65.     }
  66. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement