Skip to content

Commit

Permalink
Make hex string conversion constexpr and add test stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Dec 1, 2023
1 parent e259f59 commit b26bd0c
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 32 deletions.
15 changes: 0 additions & 15 deletions wpilibc/src/main/native/cpp/util/Color.cpp

This file was deleted.

13 changes: 0 additions & 13 deletions wpilibc/src/main/native/cpp/util/Color8Bit.cpp

This file was deleted.

22 changes: 21 additions & 1 deletion wpilibc/src/main/native/include/frc/util/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>

#include <fmt/core.h>
#include <wpi/StringExtras.h>
Expand Down Expand Up @@ -846,7 +847,26 @@ class Color {
*
* @return a string of the format <tt>\#RRGGBB</tt>
*/
std::string HexString() const;
constexpr std::string HexString() const {
int r = static_cast<int>(255.0 * red);
int g = static_cast<int>(255.0 * green);
int b = static_cast<int>(255.0 * blue);

if (std::is_constant_evaluated()) {
std::string str = "#";

str += wpi::hexdigit(r / 16);
str += wpi::hexdigit(r % 16);
str += wpi::hexdigit(g / 16);
str += wpi::hexdigit(g % 16);
str += wpi::hexdigit(b / 16);
str += wpi::hexdigit(b % 16);

return str;
} else {
return fmt::format("#{:02X}{:02X}{:02X}", r, g, b);
}
}

double red = 0.0;
double green = 0.0;
Expand Down
22 changes: 19 additions & 3 deletions wpilibc/src/main/native/include/frc/util/Color8Bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>

#include <fmt/core.h>
#include <wpi/StringExtras.h>
Expand Down Expand Up @@ -45,6 +46,8 @@ class Color8Bit {
green(color.green * 255),
blue(color.blue * 255) {}

constexpr bool operator==(const Color8Bit&) const = default;

constexpr operator Color() const { // NOLINT
return Color(red / 255.0, green / 255.0, blue / 255.0);
}
Expand Down Expand Up @@ -74,14 +77,27 @@ class Color8Bit {
return Color8Bit{r, g, b};
}

constexpr bool operator==(const Color8Bit&) const = default;

/**
* Return this color represented as a hex string.
*
* @return a string of the format <tt>\#RRGGBB</tt>
*/
std::string HexString() const;
constexpr std::string HexString() const {
if (std::is_constant_evaluated()) {
std::string str = "#";

str += wpi::hexdigit(red / 16);
str += wpi::hexdigit(red % 16);
str += wpi::hexdigit(green / 16);
str += wpi::hexdigit(green % 16);
str += wpi::hexdigit(blue / 16);
str += wpi::hexdigit(blue % 16);

return str;
} else {
return fmt::format("#{:02X}{:02X}{:02X}", red, green, blue);
}
}

int red = 0;
int green = 0;
Expand Down
19 changes: 19 additions & 0 deletions wpilibc/src/test/native/cpp/util/Color8BitTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 <gtest/gtest.h>

#include "frc/util/Color8Bit.h"

TEST(Color8BitTest, ConstructDefault) {}

TEST(Color8BitTest, ConstructFromInts) {}

TEST(Color8BitTest, ConstructFromColor) {}

TEST(Color8BitTest, ImplicitConversionToColor) {}

TEST(Color8BitTest, FromHexString) {}

TEST(Color8BitTest, HexString) {}
19 changes: 19 additions & 0 deletions wpilibc/src/test/native/cpp/util/ColorTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 <gtest/gtest.h>

#include "frc/util/Color.h"

TEST(ColorTest, ConstructDefault) {}

TEST(ColorTest, ConstructFromDoubles) {}

TEST(ColorTest, ConstructFromInts) {}

TEST(ColorTest, FromHSV) {}

TEST(ColorTest, FromHexString) {}

TEST(ColorTest, HexString) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.

package edu.wpi.first.wpilibj;

import org.junit.jupiter.api.Test;

class Color8BitTest {
@Test
void testConstructDefault() {}

@Test
void testConstructFromInts() {}

@Test
void testConstructFromColor() {}

@Test
void testFromHexString() {}

@Test
void testHexString() {}
}
27 changes: 27 additions & 0 deletions wpilibj/src/test/java/edu/wpi/first/wpilibj/util/ColorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.

package edu.wpi.first.wpilibj;

import org.junit.jupiter.api.Test;

class ColorTest {
@Test
void testConstructDefault() {}

@Test
void testConstructFromDoubles() {}

@Test
void testConstructFromInts() {}

@Test
void testFromHSV() {}

@Test
void testFromHexString() {}

@Test
void testHexString() {}
}

0 comments on commit b26bd0c

Please sign in to comment.