15#include "wpi/util/StringExtras.hpp"
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::util::isHexDigit(hexString[1]) ||
61 !wpi::util::isHexDigit(hexString[2]) ||
62 !wpi::util::isHexDigit(hexString[3]) ||
63 !wpi::util::isHexDigit(hexString[4]) ||
64 !wpi::util::isHexDigit(hexString[5]) ||
65 !wpi::util::isHexDigit(hexString[6])) {
66 throw std::invalid_argument(
67 fmt::format(
"Invalid hex string for Color \"{}\"", hexString));
70 red = wpi::util::hexDigitValue(hexString[1]) * 16 +
71 wpi::util::hexDigitValue(hexString[2]);
72 green = wpi::util::hexDigitValue(hexString[3]) * 16 +
73 wpi::util::hexDigitValue(hexString[4]);
74 blue = wpi::util::hexDigitValue(hexString[5]) * 16 +
75 wpi::util::hexDigitValue(hexString[6]);
80 constexpr operator Color()
const {
92 if (hexString.length() != 7 || !hexString.starts_with(
"#") ||
93 !wpi::util::isHexDigit(hexString[1]) ||
94 !wpi::util::isHexDigit(hexString[2]) ||
95 !wpi::util::isHexDigit(hexString[3]) ||
96 !wpi::util::isHexDigit(hexString[4]) ||
97 !wpi::util::isHexDigit(hexString[5]) ||
98 !wpi::util::isHexDigit(hexString[6])) {
99 throw std::invalid_argument(
100 fmt::format(
"Invalid hex string for Color \"{}\"", hexString));
103 int r = wpi::util::hexDigitValue(hexString[0]) * 16 +
104 wpi::util::hexDigitValue(hexString[1]);
105 int g = wpi::util::hexDigitValue(hexString[2]) * 16 +
106 wpi::util::hexDigitValue(hexString[3]);
107 int b = wpi::util::hexDigitValue(hexString[4]) * 16 +
108 wpi::util::hexDigitValue(hexString[5]);
119 {
'#', wpi::util::hexdigit(
red / 16), wpi::util::hexdigit(
red % 16),
120 wpi::util::hexdigit(
green / 16), wpi::util::hexdigit(
green % 16),
121 wpi::util::hexdigit(
blue / 16), wpi::util::hexdigit(
blue % 16)}};
constexpr Color8Bit(const Color &color)
Constructs a Color8Bit from a Color.
Definition Color8Bit.hpp:47
constexpr auto HexString() const
Return this color represented as a hex string.
Definition Color8Bit.hpp:117
constexpr Color8Bit()=default
Constructs a default color (black).
int red
Red component (0-255).
Definition Color8Bit.hpp:125
int green
Green component (0-255).
Definition Color8Bit.hpp:128
int blue
Blue component (0-255).
Definition Color8Bit.hpp:131
static constexpr Color8Bit FromHexString(std::string_view hexString)
Create a Color8Bit from a hex string.
Definition Color8Bit.hpp:91
constexpr Color8Bit(std::string_view hexString)
Constructs a Color8Bit from a hex string.
Definition Color8Bit.hpp:58
constexpr bool operator==(const Color8Bit &) const =default
constexpr Color8Bit(int r, int g, int b)
Constructs a Color8Bit.
Definition Color8Bit.hpp:37
Represents colors that can be used with Addressable LEDs.
Definition Color.hpp:42
color
Definition color.h:16
Definition StringMap.hpp:773
Definition raw_os_ostream.hpp:19
Fixed length string (array of character) for compile time use.
Definition ct_string.hpp:29