[wpilib] Improve Color.toString (#4450)

This commit is contained in:
Starlight220
2022-10-04 22:36:51 +03:00
committed by GitHub
parent 5c067d30a0
commit 3b81cf6c35
9 changed files with 290 additions and 149 deletions

View File

@@ -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);
}

View 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));
}

View 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);
}