PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.2KB

  1. #pragma once
  2. #include <Arduino.h>
  3. namespace ssd1351 {
  4. typedef uint8_t IndexedColor;
  5. typedef uint16_t LowColor;
  6. struct HighColor {
  7. uint8_t r = 0;
  8. uint8_t g = 0;
  9. uint8_t b = 0;
  10. HighColor() {}
  11. HighColor(int16_t _r, int16_t _g, int16_t _b) : r(_r), g(_g), b(_b) {}
  12. };
  13. struct RGB {
  14. uint8_t r = 0;
  15. uint8_t g = 0;
  16. uint8_t b = 0;
  17. uint8_t __attribute__((always_inline)) clamp(int16_t val) {
  18. val = val > 255 ? 255 : val;
  19. return val < 0 ? 0 : val;
  20. }
  21. RGB(const LowColor encoded) {
  22. r = (encoded & 0xf800) >> 8;
  23. g = (encoded & 0x7E0) >> 3;
  24. b = (encoded & 0x1F) << 3;
  25. }
  26. RGB(const HighColor &c) {
  27. r = c.r << 2;
  28. g = c.g << 2;
  29. b = c.b << 2;
  30. }
  31. RGB(int16_t _r = 0, int16_t _g = 0, int16_t _b = 0) {
  32. r = clamp(_r);
  33. g = clamp(_g);
  34. b = clamp(_b);
  35. }
  36. operator HighColor() const {
  37. return HighColor(r, g, b);
  38. }
  39. operator LowColor() const {
  40. // Encode 3 byte colour into two byte (5r/6g/5b) color
  41. // Masks the last three bits of red/blue, last two bits of green
  42. // and shifts colours up to make RRRR RGGG GGGB BBBB
  43. return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3);
  44. }
  45. operator IndexedColor() const {
  46. return (r & 0xE0) | ((g & 0xE0) >> 3) | (b >> 6);
  47. }
  48. };
  49. }