Last active
January 10, 2020 18:47
-
-
Save dzil123/8c5b602d2ae662740722349ea4b9212f to your computer and use it in GitHub Desktop.
A way to show arbitrary RGB colors on Shuffleboard. Intended for use with the REV Color Sensor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import edu.wpi.first.wpilibj.shuffleboard.BuiltInWidgets; | |
import edu.wpi.first.wpilibj.shuffleboard.ShuffleboardContainer; | |
import edu.wpi.first.wpilibj.shuffleboard.SuppliedValueWidget; | |
import edu.wpi.first.wpilibj.util.Color; | |
import java.util.Map; | |
public class ShuffleboardRGB { | |
public final SuppliedValueWidget<Boolean> widget; | |
private static final String offStr = "Color when false"; | |
private static final String onStr = "Color when true"; | |
private static final String hexStringFmt = "#%02X%02X%02X"; | |
public static final String toHexString(Color c) { | |
// Float to 0-255 conversion implementation from java.awt.Color | |
return String.format(hexStringFmt, (int) (c.red * 255 + 0.5), (int) (c.green * 255 + 0.5), | |
(int) (c.blue * 255 + 0.5)); | |
} | |
public ShuffleboardRGB(ShuffleboardContainer tab, String name) { | |
widget = tab.addBoolean(name, () -> false).withWidget(BuiltInWidgets.kBooleanBox); | |
setColor("#000000"); | |
} | |
public final void setColor(Color color) { | |
setColor(toHexString(color)); | |
} | |
public final void setColor(String color) { | |
// Hex color string #RRGGBB | |
widget.withProperties(Map.of(offStr, color, onStr, color)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment