mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
[wpilib] Make Color::HexString() constexpr (#5985)
Related improvements to wpi::ct_string: * Implicitly convert to std::string * Add operator== for std::string, std::string_view, and const Char*
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "frc/util/Color.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
std::string Color::HexString() const {
|
||||
return fmt::format("#{:02X}{:02X}{:02X}", static_cast<int>(255.0 * red),
|
||||
static_cast<int>(255.0 * green),
|
||||
static_cast<int>(255.0 * blue));
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "frc/util/Color8Bit.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
std::string Color8Bit::HexString() const {
|
||||
return fmt::format("#{:02X}{:02X}{:02X}", red, green, blue);
|
||||
}
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include <fmt/core.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/ct_string.h>
|
||||
|
||||
namespace frc {
|
||||
|
||||
@@ -851,7 +852,16 @@ class Color {
|
||||
*
|
||||
* @return a string of the format <tt>\#RRGGBB</tt>
|
||||
*/
|
||||
std::string HexString() const;
|
||||
constexpr auto HexString() const {
|
||||
const int r = 255.0 * red;
|
||||
const int g = 255.0 * green;
|
||||
const int b = 255.0 * blue;
|
||||
|
||||
return wpi::ct_string<char, std::char_traits<char>, 7>{
|
||||
{'#', wpi::hexdigit(r / 16), wpi::hexdigit(r % 16),
|
||||
wpi::hexdigit(g / 16), wpi::hexdigit(g % 16), wpi::hexdigit(b / 16),
|
||||
wpi::hexdigit(b % 16)}};
|
||||
}
|
||||
|
||||
double red = 0.0;
|
||||
double green = 0.0;
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include <fmt/core.h>
|
||||
#include <wpi/StringExtras.h>
|
||||
#include <wpi/ct_string.h>
|
||||
|
||||
#include "Color.h"
|
||||
|
||||
@@ -107,7 +108,12 @@ class Color8Bit {
|
||||
*
|
||||
* @return a string of the format <tt>\#RRGGBB</tt>
|
||||
*/
|
||||
std::string HexString() const;
|
||||
constexpr auto HexString() const {
|
||||
return wpi::ct_string<char, std::char_traits<char>, 7>{
|
||||
{'#', wpi::hexdigit(red / 16), wpi::hexdigit(red % 16),
|
||||
wpi::hexdigit(green / 16), wpi::hexdigit(green % 16),
|
||||
wpi::hexdigit(blue / 16), wpi::hexdigit(blue % 16)}};
|
||||
}
|
||||
|
||||
int red = 0;
|
||||
int green = 0;
|
||||
|
||||
Reference in New Issue
Block a user