Skip to content

Commit

Permalink
Make hex string factory a constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Dec 1, 2023
1 parent 3fbcb9c commit b572e4b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 57 deletions.
52 changes: 27 additions & 25 deletions wpilibc/src/main/native/include/frc/util/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,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[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]);

red = r / 255.0;
green = g / 255.0;
blue = b / 255.0;
}

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

/**
Expand Down Expand Up @@ -817,31 +844,6 @@ class Color {
}
}

/**
* Create a Color from a hex string.
*
* @param hexString a string of the format <tt>\#RRGGBB</tt>
* @return Color object from hex string.
* @throws std::invalid_argument if the hex string is invalid.
*/
static constexpr Color 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 Color{r, g, b};
}

/**
* Return this color represented as a hex string.
*
Expand Down
23 changes: 23 additions & 0 deletions wpilibc/src/main/native/include/frc/util/Color8Bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ 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[0]) * 16 +
wpi::hexDigitValue(hexString[1]);
green = wpi::hexDigitValue(hexString[2]) * 16 +
wpi::hexDigitValue(hexString[3]);
blue = wpi::hexDigitValue(hexString[4]) * 16 +
wpi::hexDigitValue(hexString[5]);
}

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

constexpr operator Color() const { // NOLINT
Expand Down
4 changes: 2 additions & 2 deletions wpilibc/src/test/native/cpp/util/Color8BitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ TEST(Color8BitTest, ConstructFromInts) {}

TEST(Color8BitTest, ConstructFromColor) {}

TEST(Color8BitTest, ImplicitConversionToColor) {}
TEST(Color8BitTest, ConstructFromHexString) {}

TEST(Color8BitTest, FromHexString) {}
TEST(Color8BitTest, ImplicitConversionToColor) {}

TEST(Color8BitTest, HexString) {}
4 changes: 2 additions & 2 deletions wpilibc/src/test/native/cpp/util/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ TEST(ColorTest, ConstructFromDoubles) {}

TEST(ColorTest, ConstructFromInts) {}

TEST(ColorTest, FromHSV) {}
TEST(ColorTest, ConstructFromHexString) {}

TEST(ColorTest, FromHexString) {}
TEST(ColorTest, FromHSV) {}

TEST(ColorTest, HexString) {}
34 changes: 16 additions & 18 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ private Color(double red, double green, double blue, String name) {
this.m_name = name;
}

/**
* Constructs a Color from a hex string.
*
* @param hexString a string of the format <code>#RRGGBB</code>
* @throws IllegalArgumentException if the hex string is invalid.
*/
public Color(String hexString) {
if (hexString.length() != 7 || !hexString.startsWith("#")) {
throw new IllegalArgumentException("Invalid hex string \"" + hexString + "\"");
}

this.red = Integer.valueOf(hexString.substring(1, 3), 16) / 255.0;
this.green = Integer.valueOf(hexString.substring(3, 5), 16) / 255.0;
this.blue = Integer.valueOf(hexString.substring(5, 7), 16) / 255.0;
}

/**
* Creates a Color from HSV values.
*
Expand Down Expand Up @@ -115,24 +131,6 @@ public static Color fromHSV(int h, int s, int v) {
}
}

/**
* Create a Color from a hex string.
*
* @param hexString a string of the format <code>#RRGGBB</code>
* @return Color object from hex string.
* @throws IllegalArgumentException if the hex string is invalid.
*/
public static Color fromHexString(String hexString) {
if (hexString.length() != 7 || !hexString.startsWith("#")) {
throw new IllegalArgumentException("Invalid hex string \"" + hexString + "\"");
}

return new Color(
Integer.valueOf(hexString.substring(1, 3), 16),
Integer.valueOf(hexString.substring(3, 5), 16),
Integer.valueOf(hexString.substring(5, 7), 16));
}

@Override
public boolean equals(Object other) {
if (this == other) {
Expand Down
12 changes: 5 additions & 7 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color8Bit.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@ public Color8Bit(Color color) {
}

/**
* Create a Color8Bit from a hex string.
* Constructs a Color8Bit from a hex string.
*
* @param hexString a string of the format <code>#RRGGBB</code>
* @return Color8Bit object from hex string.
* @throws IllegalArgumentException if the hex string is invalid.
*/
public static Color8Bit fromHexString(String hexString) {
public Color8Bit(String hexString) {
if (hexString.length() != 7 || !hexString.startsWith("#")) {
throw new IllegalArgumentException("Invalid hex string \"" + hexString + "\"");
}

return new Color8Bit(
Integer.valueOf(hexString.substring(1, 3), 16),
Integer.valueOf(hexString.substring(3, 5), 16),
Integer.valueOf(hexString.substring(5, 7), 16));
this.red = Integer.valueOf(hexString.substring(1, 3), 16);
this.green = Integer.valueOf(hexString.substring(3, 5), 16);
this.blue = Integer.valueOf(hexString.substring(5, 7), 16);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void testConstructFromInts() {}
void testConstructFromColor() {}

@Test
void testFromHexString() {}
void testConstructFromHexString() {}

@Test
void testHexString() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ void testConstructFromDoubles() {}
void testConstructFromInts() {}

@Test
void testFromHSV() {}
void testConstructFromHexString() {}

@Test
void testFromHexString() {}
void testFromHSV() {}

@Test
void testHexString() {}
Expand Down

0 comments on commit b572e4b

Please sign in to comment.