mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-03 03:01:44 +00:00
[wpilib] Improve Color.toString (#4450)
This commit is contained in:
@@ -34,8 +34,7 @@ MechanismRoot2d* Mechanism2d::GetRoot(std::string_view name, double x,
|
||||
}
|
||||
|
||||
void Mechanism2d::SetBackgroundColor(const Color8Bit& color) {
|
||||
std::snprintf(m_color, sizeof(m_color), "#%02X%02X%02X", color.red,
|
||||
color.green, color.blue);
|
||||
m_color = color.HexString();
|
||||
if (m_table) {
|
||||
m_table->GetEntry(kBackgroundColor).SetString(m_color);
|
||||
}
|
||||
|
||||
15
wpilibc/src/main/native/cpp/util/Color.cpp
Normal file
15
wpilibc/src/main/native/cpp/util/Color.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
// 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"
|
||||
|
||||
#include <fmt/format.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));
|
||||
}
|
||||
13
wpilibc/src/main/native/cpp/util/Color8Bit.cpp
Normal file
13
wpilibc/src/main/native/cpp/util/Color8Bit.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
// 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"
|
||||
|
||||
#include <fmt/format.h>
|
||||
|
||||
using namespace frc;
|
||||
|
||||
std::string Color8Bit::HexString() const {
|
||||
return fmt::format("#{:02X}{:02X}{:02X}", red, green, blue);
|
||||
}
|
||||
@@ -79,7 +79,7 @@ class Mechanism2d : public nt::NTSendable,
|
||||
private:
|
||||
double m_width;
|
||||
double m_height;
|
||||
char m_color[10];
|
||||
std::string m_color;
|
||||
mutable wpi::mutex m_mutex;
|
||||
std::shared_ptr<nt::NetworkTable> m_table;
|
||||
wpi::StringMap<std::unique_ptr<MechanismRoot2d>> m_roots;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
namespace frc {
|
||||
|
||||
@@ -808,6 +809,13 @@ class Color {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this color represented as a hex string.
|
||||
*
|
||||
* @return a string of the format <tt>\#RRGGBB</tt>
|
||||
*/
|
||||
std::string HexString() const;
|
||||
|
||||
double red = 0.0;
|
||||
double green = 0.0;
|
||||
double blue = 0.0;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
#include "Color.h"
|
||||
|
||||
@@ -43,6 +44,13 @@ class Color8Bit {
|
||||
return Color(red / 255.0, green / 255.0, blue / 255.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this color represented as a hex string.
|
||||
*
|
||||
* @return a string of the format <tt>\#RRGGBB</tt>
|
||||
*/
|
||||
std::string HexString() const;
|
||||
|
||||
int red = 0;
|
||||
int green = 0;
|
||||
int blue = 0;
|
||||
|
||||
Reference in New Issue
Block a user