WPILibC++ 2024.3.2
Color8Bit.h
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/core.h>
13#include <wpi/StringExtras.h>
14#include <wpi/ct_string.h>
15
16#include "Color.h"
17
18namespace frc {
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::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));
65 }
66
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]);
73 }
74
75 constexpr bool operator==(const Color8Bit&) const = default;
76
77 constexpr operator Color() const { // NOLINT
78 return Color(red / 255.0, green / 255.0, blue / 255.0);
79 }
80
81 /**
82 * Create a Color8Bit from a hex string.
83 *
84 * @param hexString a string of the format <tt>\#RRGGBB</tt>
85 * @return Color8Bit object from hex string.
86 * @throws std::invalid_argument if the hex string is invalid.
87 */
88 static constexpr Color8Bit FromHexString(std::string_view hexString) {
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));
95 }
96
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]);
103 return Color8Bit{r, g, b};
104 }
105
106 /**
107 * Return this color represented as a hex string.
108 *
109 * @return a string of the format <tt>\#RRGGBB</tt>
110 */
111 constexpr auto HexString() const {
113 {'#', wpi::hexdigit(red / 16), wpi::hexdigit(red % 16),
115 wpi::hexdigit(blue / 16), wpi::hexdigit(blue % 16)}};
116 }
117
118 /// Red component (0-255).
119 int red = 0;
120
121 /// Green component (0-255).
122 int green = 0;
123
124 /// Blue component (0-255).
125 int blue = 0;
126};
127
128} // namespace frc
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
basic_string_view< char > string_view
Definition: core.h:501
Definition: AprilTagPoseEstimator.h:15
Definition: array.h:89
b
Definition: data.h:44
constexpr char hexdigit(unsigned X, bool LowerCase=false) noexcept
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16).
Definition: StringExtras.h:37
constexpr bool isHexDigit(char C) noexcept
Checks if character C is a hexadecimal numeric character.
Definition: StringExtras.h:65
constexpr unsigned hexDigitValue(char C) noexcept
Interpret the given character C as a hexadecimal digit and return its value.
Definition: StringExtras.h:46
Fixed length string (array of character) for compile time use.
Definition: ct_string.h:29
color
Definition: color.h:16
auto format(wformat_string< T... > fmt, T &&... args) -> std::wstring
Definition: xchar.h:108