mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
[wpilib] Add hex string constructor to Color and Color8Bit (#5063)
Co-authored-by: Tyler Veness <calcmogul@gmail.com>
This commit is contained in:
@@ -5,7 +5,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <fmt/core.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
|
||||
namespace frc {
|
||||
|
||||
@@ -739,6 +744,9 @@ class Color {
|
||||
*/
|
||||
static const Color kYellowGreen;
|
||||
|
||||
/**
|
||||
* Constructs a default color (black).
|
||||
*/
|
||||
constexpr Color() = default;
|
||||
|
||||
/**
|
||||
@@ -763,6 +771,33 @@ class Color {
|
||||
constexpr Color(int r, int g, int b)
|
||||
: Color(r / 255.0, g / 255.0, b / 255.0) {}
|
||||
|
||||
/**
|
||||
* Constructs a Color from a hex string.
|
||||
*
|
||||
* @param hexString a string of the format <tt>\#RRGGBB</tt>
|
||||
* @throws std::invalid_argument if the hex string is invalid.
|
||||
*/
|
||||
explicit constexpr Color(std::string_view hexString) {
|
||||
if (hexString.length() != 7 || !hexString.starts_with("#") ||
|
||||
!wpi::isHexDigit(hexString[1]) || !wpi::isHexDigit(hexString[2]) ||
|
||||
!wpi::isHexDigit(hexString[3]) || !wpi::isHexDigit(hexString[4]) ||
|
||||
!wpi::isHexDigit(hexString[5]) || !wpi::isHexDigit(hexString[6])) {
|
||||
throw std::invalid_argument(
|
||||
fmt::format("Invalid hex string for Color \"{}\"", hexString));
|
||||
}
|
||||
|
||||
int r = wpi::hexDigitValue(hexString[1]) * 16 +
|
||||
wpi::hexDigitValue(hexString[2]);
|
||||
int g = wpi::hexDigitValue(hexString[3]) * 16 +
|
||||
wpi::hexDigitValue(hexString[4]);
|
||||
int b = wpi::hexDigitValue(hexString[5]) * 16 +
|
||||
wpi::hexDigitValue(hexString[6]);
|
||||
|
||||
red = r / 255.0;
|
||||
green = g / 255.0;
|
||||
blue = b / 255.0;
|
||||
}
|
||||
|
||||
constexpr bool operator==(const Color&) const = default;
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <fmt/core.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
|
||||
#include "Color.h"
|
||||
|
||||
@@ -16,6 +21,9 @@ namespace frc {
|
||||
*/
|
||||
class Color8Bit {
|
||||
public:
|
||||
/**
|
||||
* Constructs a default color (black).
|
||||
*/
|
||||
constexpr Color8Bit() = default;
|
||||
|
||||
/**
|
||||
@@ -40,11 +48,59 @@ class Color8Bit {
|
||||
green(color.green * 255),
|
||||
blue(color.blue * 255) {}
|
||||
|
||||
/**
|
||||
* Constructs a Color8Bit from a hex string.
|
||||
*
|
||||
* @param hexString a string of the format <tt>\#RRGGBB</tt>
|
||||
* @throws std::invalid_argument if the hex string is invalid.
|
||||
*/
|
||||
explicit constexpr Color8Bit(std::string_view hexString) {
|
||||
if (hexString.length() != 7 || !hexString.starts_with("#") ||
|
||||
!wpi::isHexDigit(hexString[1]) || !wpi::isHexDigit(hexString[2]) ||
|
||||
!wpi::isHexDigit(hexString[3]) || !wpi::isHexDigit(hexString[4]) ||
|
||||
!wpi::isHexDigit(hexString[5]) || !wpi::isHexDigit(hexString[6])) {
|
||||
throw std::invalid_argument(
|
||||
fmt::format("Invalid hex string for Color \"{}\"", hexString));
|
||||
}
|
||||
|
||||
red = wpi::hexDigitValue(hexString[1]) * 16 +
|
||||
wpi::hexDigitValue(hexString[2]);
|
||||
green = wpi::hexDigitValue(hexString[3]) * 16 +
|
||||
wpi::hexDigitValue(hexString[4]);
|
||||
blue = wpi::hexDigitValue(hexString[5]) * 16 +
|
||||
wpi::hexDigitValue(hexString[6]);
|
||||
}
|
||||
|
||||
constexpr bool operator==(const Color8Bit&) const = default;
|
||||
|
||||
constexpr operator Color() const { // NOLINT
|
||||
return Color(red / 255.0, green / 255.0, blue / 255.0);
|
||||
}
|
||||
|
||||
constexpr bool operator==(const Color8Bit&) const = default;
|
||||
/**
|
||||
* Create a Color8Bit from a hex string.
|
||||
*
|
||||
* @param hexString a string of the format <tt>\#RRGGBB</tt>
|
||||
* @return Color8Bit object from hex string.
|
||||
* @throws std::invalid_argument if the hex string is invalid.
|
||||
*/
|
||||
static constexpr Color8Bit FromHexString(std::string_view hexString) {
|
||||
if (hexString.length() != 7 || !hexString.starts_with("#") ||
|
||||
!wpi::isHexDigit(hexString[1]) || !wpi::isHexDigit(hexString[2]) ||
|
||||
!wpi::isHexDigit(hexString[3]) || !wpi::isHexDigit(hexString[4]) ||
|
||||
!wpi::isHexDigit(hexString[5]) || !wpi::isHexDigit(hexString[6])) {
|
||||
throw std::invalid_argument(
|
||||
fmt::format("Invalid hex string for Color \"{}\"", hexString));
|
||||
}
|
||||
|
||||
int r = wpi::hexDigitValue(hexString[0]) * 16 +
|
||||
wpi::hexDigitValue(hexString[1]);
|
||||
int g = wpi::hexDigitValue(hexString[2]) * 16 +
|
||||
wpi::hexDigitValue(hexString[3]);
|
||||
int b = wpi::hexDigitValue(hexString[4]) * 16 +
|
||||
wpi::hexDigitValue(hexString[5]);
|
||||
return Color8Bit{r, g, b};
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this color represented as a hex string.
|
||||
|
||||
Reference in New Issue
Block a user