Skip to content

Commit 0a4b139

Browse files
committed
Clean implementation of Gradient.
1 parent ffd4f30 commit 0a4b139

File tree

5 files changed

+82
-176
lines changed

5 files changed

+82
-176
lines changed

IHMCFootstepPlanning/test/us/ihmc/footstepPlanning/scoring/BipedalStepAdjustmentCostCalculatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import us.ihmc.graphicsDescription.appearance.AppearanceDefinition;
2121
import us.ihmc.graphicsDescription.appearance.YoAppearance;
2222
import us.ihmc.graphicsDescription.appearance.YoAppearanceRGBColor;
23+
import us.ihmc.graphicsDescription.color.Gradient;
2324
import us.ihmc.graphicsDescription.yoGraphics.YoGraphicPolygon;
2425
import us.ihmc.graphicsDescription.yoGraphics.YoGraphicsListRegistry;
2526
import us.ihmc.robotics.MathTools;
@@ -33,7 +34,6 @@
3334
import us.ihmc.simulationconstructionset.Robot;
3435
import us.ihmc.simulationconstructionset.SimulationConstructionSet;
3536
import us.ihmc.simulationconstructionset.util.simulationTesting.SimulationTestingParameters;
36-
import us.ihmc.tools.color.Gradient;
3737
import us.ihmc.tools.thread.ThreadTools;
3838

3939
public class BipedalStepAdjustmentCostCalculatorTest
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package us.ihmc.graphicsDescription.color;
2+
3+
import java.awt.Color;
4+
5+
/**
6+
* Class to create color gradients.
7+
*
8+
*/
9+
public class Gradient
10+
{
11+
public static Color[] createRainbow(int steps)
12+
{
13+
return createMultiGradient(new Color[] { Color.magenta, Color.blue, Color.green, Color.yellow, Color.orange, Color.red }, steps);
14+
}
15+
16+
public static Color[] createGradient(Color start, Color end, int steps)
17+
{
18+
int startRed = start.getRed();
19+
int startGreen = start.getGreen();
20+
int startBlue = start.getBlue();
21+
int startAlpha = start.getAlpha();
22+
23+
int endRed = end.getRed();
24+
int endGreen = end.getGreen();
25+
int endBlue = end.getBlue();
26+
int endAlpha = end.getAlpha();
27+
28+
Color[] gradient = new Color[steps];
29+
30+
double redRange = (endRed - startRed);
31+
double greenRange = (endGreen - startGreen);
32+
double blueRange = (endBlue - startBlue);
33+
double alphaRange = (endAlpha - startAlpha);
34+
35+
for (int i = 0; i < steps; i++)
36+
{
37+
double stepFactor = (double) i / (double) steps;
38+
39+
gradient[i] = new Color(startRed + (int) (stepFactor * redRange), startGreen + (int) (stepFactor * greenRange), startBlue + (int) (stepFactor * blueRange), startAlpha + (int) (stepFactor * alphaRange));
40+
}
41+
42+
return gradient;
43+
}
44+
45+
public static Color[] createMultiGradient(Color[] colors, int steps)
46+
{
47+
if(colors.length < 2)
48+
{
49+
throw new IllegalArgumentException("Need at least 2 colors for a gradient");
50+
}
51+
52+
53+
Color[] gradient = new Color[steps];
54+
55+
int stepsBetween = steps / (colors.length - 1);
56+
57+
58+
int index = 0;
59+
for (int i = 0; i < colors.length - 1; i++)
60+
{
61+
Color[] thisGradient = createGradient(colors[i], colors[i + 1], stepsBetween);
62+
63+
System.arraycopy(thisGradient, 0, gradient, index, stepsBetween);
64+
index += stepsBetween;
65+
}
66+
67+
for(; index < steps; index++)
68+
{
69+
gradient[index] = colors[colors.length - 1];
70+
}
71+
72+
return gradient;
73+
}
74+
75+
}

IHMCJavaToolkit/test/us/ihmc/tools/color/GradientTest.java renamed to IHMCGraphicsDescription/test/us/ihmc/graphicsDescription/color/GradientTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package us.ihmc.tools.color;
1+
package us.ihmc.graphicsDescription.color;
22

33
import static org.junit.Assert.assertTrue;
44

@@ -8,6 +8,7 @@
88

99
import us.ihmc.continuousIntegration.ContinuousIntegrationAnnotations.ContinuousIntegrationPlan;
1010
import us.ihmc.continuousIntegration.ContinuousIntegrationAnnotations.ContinuousIntegrationTest;
11+
import us.ihmc.graphicsDescription.color.Gradient;
1112
import us.ihmc.commons.Assertions;
1213
import us.ihmc.commons.RunnableThatThrows;
1314
import us.ihmc.continuousIntegration.IntegrationCategory;

IHMCJavaFXToolkit/src/us/ihmc/javaFXToolkit/graphing/JavaFXHeatmapGraph.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import us.ihmc.euclid.transform.AffineTransform;
2626
import us.ihmc.euclid.tuple2D.Point2D;
2727
import us.ihmc.graphicsDescription.color.ColorConversions;
28+
import us.ihmc.graphicsDescription.color.Gradient;
2829
import us.ihmc.yoVariables.dataBuffer.DataEntry;
2930
import us.ihmc.yoVariables.dataBuffer.DataEntryHolder;
3031
import us.ihmc.yoVariables.dataBuffer.TimeDataHolder;
@@ -33,7 +34,6 @@
3334
import us.ihmc.robotics.MathTools;
3435
import us.ihmc.yoVariables.registry.YoVariableRegistry;
3536
import us.ihmc.yoVariables.variable.YoDouble;
36-
import us.ihmc.tools.color.Gradient;
3737

3838
public class JavaFXHeatmapGraph
3939
{
@@ -60,6 +60,8 @@ public class JavaFXHeatmapGraph
6060
private Point2D gridCenter;
6161
private Point2D plotPencil;
6262
private Point2D viewRange;
63+
64+
private final java.awt.Color[] rainbow = Gradient.createRainbow(500);
6365

6466
public JavaFXHeatmapGraph(YoVariableRegistry registry, GraphIndicesHolder graphIndicesHolder, SelectedVariableHolder selectedVariableHolder,
6567
DataEntryHolder dataEntryHolder, TimeDataHolder dataBuffer)
@@ -238,7 +240,7 @@ private Color getHeatColor(int heat)
238240
{
239241
double maxHeat = 30.0;
240242
int heatIndex = (int) MathTools.roundToPrecision(MathTools.clamp((heat / maxHeat) * 500.0, 0.0, 499.0), 1.0);
241-
return ColorConversions.awtToJfx(Gradient.GRADIENT_RAINBOW[heatIndex]);
243+
return ColorConversions.awtToJfx(rainbow[heatIndex]);
242244
}
243245

244246
private void fillRect(double x, double y, double width, double height)

IHMCJavaToolkit/src/us/ihmc/tools/color/Gradient.java

Lines changed: 0 additions & 172 deletions
This file was deleted.

0 commit comments

Comments
 (0)