1 package org.opentrafficsim.draw;
2
3 import java.awt.Color;
4 import java.io.Serializable;
5 import java.util.Arrays;
6
7 import org.djutils.exceptions.Throw;
8 import org.djutils.logger.CategoryLogger;
9
10
11
12
13
14
15
16
17
18
19
20 public class BoundsPaintScale implements ColorPaintScale, Serializable
21 {
22
23
24 public static final Color[] GREEN_RED = new Color[] {Color.GREEN, Color.YELLOW, Color.RED};
25
26
27 public static final Color[] GREEN_RED_DARK =
28 new Color[] {Color.GREEN.darker(), Color.GREEN, Color.YELLOW, Color.RED, Color.RED.darker()};
29
30
31 private static final long serialVersionUID = 20181008L;
32
33
34 private double[] bounds;
35
36
37 private Color[] boundColors;
38
39
40
41
42
43
44
45 public BoundsPaintScale(final double[] bounds, final Color[] boundColors) throws IllegalArgumentException
46 {
47 Throw.when(bounds.length < 2, IllegalArgumentException.class, "bounds must have >= 2 entries");
48 Throw.when(bounds.length != boundColors.length, IllegalArgumentException.class,
49 "bounds must have same length as boundColors");
50 this.bounds = new double[bounds.length];
51 this.boundColors = new Color[bounds.length];
52
53
54 for (int nextBound = 0; nextBound < bounds.length; nextBound++)
55 {
56
57 double currentLowest = Double.POSITIVE_INFINITY;
58 int bestIndex = -1;
59 int index;
60 for (index = 0; index < bounds.length; index++)
61 {
62 if (bounds[index] < currentLowest && (nextBound == 0 || bounds[index] > this.bounds[nextBound - 1]))
63 {
64 bestIndex = index;
65 currentLowest = bounds[index];
66 }
67 }
68 Throw.when(bestIndex < 0, IllegalArgumentException.class, "duplicate value in bounds");
69 this.bounds[nextBound] = bounds[bestIndex];
70 this.boundColors[nextBound] = boundColors[bestIndex];
71 }
72 }
73
74
75
76
77
78
79 public static Color[] reverse(final Color[] colors)
80 {
81 Color[] out = new Color[colors.length];
82 for (int i = 0; i < colors.length; i++)
83 {
84 out[colors.length - i - 1] = colors[i];
85 }
86 return out;
87 }
88
89
90
91
92
93
94 public static Color[] hue(final int n)
95 {
96 Color[] out = new Color[n];
97 for (int i = 0; i < n; i++)
98 {
99 out[i] = new Color(Color.HSBtoRGB(((float) i) / n, 1.0f, 1.0f));
100 }
101 return out;
102 }
103
104 @Override
105 public Color getPaint(final double value)
106 {
107 if (Double.isNaN(value))
108 {
109 return Color.BLACK;
110 }
111 if (value < this.bounds[0])
112 {
113 return this.boundColors[0];
114 }
115 if (value > this.bounds[this.bounds.length - 1])
116 {
117 return this.boundColors[this.bounds.length - 1];
118 }
119 int index;
120 for (index = 0; index < this.bounds.length - 1; index++)
121 {
122 if (value < this.bounds[index + 1])
123 {
124 break;
125 }
126 }
127 final double ratio;
128 if (index >= this.bounds.length - 1)
129 {
130 index = this.bounds.length - 2;
131 ratio = 1.0;
132 }
133 else
134 {
135 ratio = (value - this.bounds[index]) / (this.bounds[index + 1] - this.bounds[index]);
136 }
137 if (Double.isInfinite(ratio))
138 {
139 CategoryLogger.always().error("Interpolation value for color is infinite based on {} in {} obtaining index {}.",
140 value, this.bounds, index);
141 }
142 Color mix = ColorInterpolator.interpolateColor(this.boundColors[index], this.boundColors[index + 1], ratio);
143 return mix;
144 }
145
146 @Override
147 public final double getLowerBound()
148 {
149 return this.bounds[0];
150 }
151
152 @Override
153 public final double getUpperBound()
154 {
155 return this.bounds[this.bounds.length - 1];
156 }
157
158 @Override
159 public String toString()
160 {
161 return "BoundsPaintScale [bounds=" + Arrays.toString(this.bounds) + ", boundColors=" + Arrays.toString(this.boundColors)
162 + "]";
163 }
164
165 }