13#include <wpi/StringExtras.h>
38 :
red(
std::clamp(r, 0, 255)),
58 explicit constexpr Color8Bit(std::string_view hexString) {
59 if (hexString.length() != 7 || !hexString.starts_with(
"#") ||
60 !wpi::isHexDigit(hexString[1]) || !wpi::isHexDigit(hexString[2]) ||
61 !wpi::isHexDigit(hexString[3]) || !wpi::isHexDigit(hexString[4]) ||
62 !wpi::isHexDigit(hexString[5]) || !wpi::isHexDigit(hexString[6])) {
63 throw std::invalid_argument(
64 fmt::format(
"Invalid hex string for Color \"{}\"", hexString));
67 red = wpi::hexDigitValue(hexString[1]) * 16 +
68 wpi::hexDigitValue(hexString[2]);
69 green = wpi::hexDigitValue(hexString[3]) * 16 +
70 wpi::hexDigitValue(hexString[4]);
71 blue = wpi::hexDigitValue(hexString[5]) * 16 +
72 wpi::hexDigitValue(hexString[6]);
77 constexpr operator Color()
const {
89 if (hexString.length() != 7 || !hexString.starts_with(
"#") ||
90 !wpi::isHexDigit(hexString[1]) || !wpi::isHexDigit(hexString[2]) ||
91 !wpi::isHexDigit(hexString[3]) || !wpi::isHexDigit(hexString[4]) ||
92 !wpi::isHexDigit(hexString[5]) || !wpi::isHexDigit(hexString[6])) {
93 throw std::invalid_argument(
94 fmt::format(
"Invalid hex string for Color \"{}\"", hexString));
97 int r = wpi::hexDigitValue(hexString[0]) * 16 +
98 wpi::hexDigitValue(hexString[1]);
99 int g = wpi::hexDigitValue(hexString[2]) * 16 +
100 wpi::hexDigitValue(hexString[3]);
101 int b = wpi::hexDigitValue(hexString[4]) * 16 +
102 wpi::hexDigitValue(hexString[5]);
113 {
'#', wpi::hexdigit(
red / 16), wpi::hexdigit(
red % 16),
114 wpi::hexdigit(
green / 16), wpi::hexdigit(
green % 16),
115 wpi::hexdigit(
blue / 16), wpi::hexdigit(
blue % 16)}};
Represents colors that can be used with Addressable LEDs.
Definition Color8Bit.h:23
constexpr Color8Bit(std::string_view hexString)
Constructs a Color8Bit from a hex string.
Definition Color8Bit.h:58
constexpr auto HexString() const
Return this color represented as a hex string.
Definition Color8Bit.h:111
constexpr Color8Bit(const Color &color)
Constructs a Color8Bit from a Color.
Definition Color8Bit.h:47
static constexpr Color8Bit FromHexString(std::string_view hexString)
Create a Color8Bit from a hex string.
Definition Color8Bit.h:88
constexpr bool operator==(const Color8Bit &) const =default
constexpr Color8Bit(int r, int g, int b)
Constructs a Color8Bit.
Definition Color8Bit.h:37
int red
Red component (0-255).
Definition Color8Bit.h:119
constexpr Color8Bit()=default
Constructs a default color (black).
int blue
Blue component (0-255).
Definition Color8Bit.h:125
int green
Green component (0-255).
Definition Color8Bit.h:122
Represents colors that can be used with Addressable LEDs.
Definition Color.h:24
Definition SystemServer.h:9
Definition PointerIntPair.h:280
Fixed length string (array of character) for compile time use.
Definition ct_string.h:29
color
Definition color.h:16