Skip to content

Commit ddbb717

Browse files
committed
Adds Color8Bit and fromDutyCycle to Color enum
1 parent 40df0f0 commit ddbb717

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/main/java/com/chaos131/util/Color.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.chaos131.util;
22

3+
import edu.wpi.first.wpilibj.util.Color8Bit;
4+
35
/** Colors stored in RGB format */
46
public enum Color {
57
/** Red */
@@ -19,6 +21,8 @@ public enum Color {
1921
/** Purple */
2022
PURPLE(255, 0, 255);
2123

24+
public Color8Bit color8Bit;
25+
2226
/** Color's red channel */
2327
public int red;
2428

@@ -28,10 +32,35 @@ public enum Color {
2832
/** Color's blue channel */
2933
public int blue;
3034

31-
/** Forms a color out of numbers */
35+
/** Forms a Color out of numbers */
3236
Color(int r, int g, int b) {
33-
red = r;
34-
green = g;
35-
blue = b;
37+
this(new Color8Bit(r, g, b));
38+
}
39+
40+
/** Forms a Color out of a Color8BIt */
41+
Color(Color8Bit c) {
42+
color8Bit = c;
43+
red = c.red;
44+
green = c.green;
45+
blue = c.blue;
46+
}
47+
48+
/**
49+
* Returns a COlor8Bit that shows scaled values for the duty cycle
50+
* @param dutyCycle a percentage value [-1.0, 1.0] to convert to a scaled color
51+
*/
52+
public static Color8Bit fromDutyCycle(double dutyCycle) {
53+
int grayBase = 131;
54+
// If duty cycle is zero, show gray
55+
if (dutyCycle == 0) {
56+
return new Color8Bit(grayBase, grayBase, grayBase);
57+
}
58+
59+
int scaleValue = (int) Math.floor(100 * dutyCycle);
60+
int scaledUpValue = grayBase + scaleValue + 10;
61+
int scaledDownValue = grayBase - scaleValue - 10;
62+
return dutyCycle > 0
63+
? new Color8Bit(scaledDownValue, scaledUpValue, scaledDownValue) // Show a scaled green value if positive
64+
: new Color8Bit(scaledUpValue, scaledDownValue, scaledDownValue); // Show a scaled red value if negatve
3665
}
3766
}

0 commit comments

Comments
 (0)