tds3p-rsvp-reader/firmware/include/Palette.h

51 lines
1.6 KiB
C++

#pragma once
#include <stddef.h>
#include <stdint.h>
namespace palette {
constexpr uint16_t rgb565(uint8_t red, uint8_t green, uint8_t blue) {
return (static_cast<uint16_t>(red & 0xF8) << 8) |
(static_cast<uint16_t>(green & 0xFC) << 3) | (blue >> 3);
}
constexpr uint16_t kOtherGrey = rgb565(176, 176, 176);
constexpr uint16_t kWhite = rgb565(255, 255, 255);
constexpr uint16_t kHues[] = {0, 30, 60, 120, 180, 225, 270, 300, 330};
constexpr uint16_t kColors[] = {
rgb565(255, 128, 128), rgb565(255, 191, 128), rgb565(255, 255, 128),
rgb565(128, 255, 128), rgb565(128, 255, 255), rgb565(128, 159, 255),
rgb565(191, 128, 255), rgb565(255, 128, 255), rgb565(255, 128, 191),
};
constexpr const char* kHueNames[] = {
"Red", "Orange", "Yellow", "Green", "Cyan", "Blue",
"Violet", "Magenta", "Pink",
};
// The color picker includes the two existing neutral choices before the hue
// palette documented in docs/history/PLAN.md.
constexpr uint8_t kChoiceCount = 11;
inline uint16_t colorForChoice(uint8_t choice) {
if (choice == 0) return kWhite;
if (choice == 1) return kOtherGrey;
return choice < kChoiceCount ? kColors[choice - 2] : kWhite;
}
inline const char* nameForChoice(uint8_t choice) {
if (choice == 0) return "White";
if (choice == 1) return "Gray";
return choice < kChoiceCount ? kHueNames[choice - 2] : "White";
}
inline int indexForHue(uint16_t hue) {
for (size_t index = 0; index < sizeof(kHues) / sizeof(kHues[0]); ++index) {
if (kHues[index] == hue) return static_cast<int>(index);
}
return -1;
}
} // namespace palette