WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
Color8Bit.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <algorithm>
8#include <stdexcept>
9#include <string>
10#include <string_view>
11
12#include <fmt/format.h>
13
14#include "wpi/util/Color.hpp"
15#include "wpi/util/StringExtras.hpp"
17
18namespace wpi::util {
19
20/**
21 * Represents colors that can be used with Addressable LEDs.
22 */
23class Color8Bit {
24 public:
25 /**
26 * Constructs a default color (black).
27 */
28 constexpr Color8Bit() = default;
29
30 /**
31 * Constructs a Color8Bit.
32 *
33 * @param r Red value (0-255)
34 * @param g Green value (0-255)
35 * @param b Blue value (0-255)
36 */
37 constexpr Color8Bit(int r, int g, int b)
38 : red(std::clamp(r, 0, 255)),
39 green(std::clamp(g, 0, 255)),
40 blue(std::clamp(b, 0, 255)) {}
41
42 /**
43 * Constructs a Color8Bit from a Color.
44 *
45 * @param color The color
46 */
47 constexpr Color8Bit(const Color& color) // NOLINT
48 : red(color.red * 255),
49 green(color.green * 255),
50 blue(color.blue * 255) {}
51
52 /**
53 * Constructs a Color8Bit from a hex string.
54 *
55 * @param hexString a string of the format <tt>\#RRGGBB</tt>
56 * @throws std::invalid_argument if the hex string is invalid.
57 */
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));
68 }
69
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]);
76 }
77
78 constexpr bool operator==(const Color8Bit&) const = default;
79
80 constexpr operator Color() const { // NOLINT
81 return Color(red / 255.0, green / 255.0, blue / 255.0);
82 }
83
84 /**
85 * Create a Color8Bit from a hex string.
86 *
87 * @param hexString a string of the format <tt>\#RRGGBB</tt>
88 * @return Color8Bit object from hex string.
89 * @throws std::invalid_argument if the hex string is invalid.
90 */
91 static constexpr Color8Bit FromHexString(std::string_view hexString) {
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));
101 }
102
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]);
109 return Color8Bit{r, g, b};
110 }
111
112 /**
113 * Return this color represented as a hex string.
114 *
115 * @return a string of the format <tt>\#RRGGBB</tt>
116 */
117 constexpr auto HexString() const {
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)}};
122 }
123
124 /// Red component (0-255).
125 int red = 0;
126
127 /// Green component (0-255).
128 int green = 0;
129
130 /// Blue component (0-255).
131 int blue = 0;
132};
133
134} // namespace wpi::util
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