Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpilib] Add hex string constructor to Color and Color8Bit #5063

Merged
merged 12 commits into from
Dec 1, 2023
18 changes: 18 additions & 0 deletions wpilibc/src/main/native/include/frc/util/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <algorithm>
#include <stdexcept>
#include <string>

namespace frc {
Expand Down Expand Up @@ -811,6 +812,23 @@ class Color {
}
}

/**
* Create a Color from a hex string. Throws an exception if the Hex String is
* invalid.
*
* @param hexString a string of the format <code>#RRGGBB</code>
* @return Color object from hex string.
*/
static constexpr Color FromHexString(std::string hexString) {
calcmogul marked this conversation as resolved.
Show resolved Hide resolved
if (hexString.length() != 7 || !hexString.starts_with("#")) {
throw std::invalid_argument("Invalid Hex String for Color");
}

int r, g, b;
std::sscanf(hexString.c_str(), "#%02x%02x%02x", &r, &g, &b);
calcmogul marked this conversation as resolved.
Show resolved Hide resolved
return Color(r / 255, g / 255, b / 255);
calcmogul marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Return this color represented as a hex string.
*
Expand Down
18 changes: 18 additions & 0 deletions wpilibc/src/main/native/include/frc/util/Color8Bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <algorithm>
#include <stdexcept>
#include <string>

#include "Color.h"
Expand Down Expand Up @@ -44,6 +45,23 @@ class Color8Bit {
return Color(red / 255.0, green / 255.0, blue / 255.0);
}

/**
* Create a Color8Bit from a hex string. Throws an exception if the Hex String
* is invalid.
*
* @param hexString a string of the format <code>#RRGGBB</code>
* @return Color8Bit object from hex string.
*/
static constexpr Color8Bit FromHexString(std::string hexString) {
if (hexString.length() != 7 || !hexString.starts_with("#")) {
throw std::invalid_argument("Invalid Hex String for Color");
}

int r, g, b;
std::sscanf(hexString.c_str(), "#%02x%02x%02x", &r, &g, &b);
return Color8Bit(r, g, b);
}

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

/**
Expand Down
16 changes: 16 additions & 0 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ public static Color fromHSV(int h, int s, int v) {
}
}

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

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

@Override
public boolean equals(Object other) {
if (this == other) {
Expand Down
16 changes: 16 additions & 0 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/util/Color8Bit.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ public Color8Bit(Color color) {
this((int) (color.red * 255), (int) (color.green * 255), (int) (color.blue * 255));
}

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

return new Color8Bit(
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