return col == null ? null : col.brighter().brighter().brighter();
}
+ /**
+ * Returns a color between minColour and maxColour; the RGB values are in
+ * proportion to where 'value' lies between minValue and maxValue
+ *
+ * @param value
+ * @param minValue
+ * @param minColour
+ * @param maxValue
+ * @param maxColour
+ * @return
+ */
+ public static Color getGraduatedColour(float value, float minValue,
+ Color minColour, float maxValue, Color maxColour)
+ {
+ if (value < minValue)
+ {
+ value = minValue;
+ }
+ if (value > maxValue)
+ {
+ value = maxValue;
+ }
+
+ /*
+ * prop = proportion of the way value is from minValue to maxValue
+ */
+ float prop = (value - minValue) / (maxValue - minValue);
+ float r = minColour.getRed() + prop
+ * (maxColour.getRed() - minColour.getRed());
+ float g = minColour.getGreen() + prop
+ * (maxColour.getGreen() - minColour.getGreen());
+ float b = minColour.getBlue() + prop
+ * (maxColour.getBlue() - minColour.getBlue());
+ return new Color(r / 255, g / 255, b / 255);
+ }
}
assertEquals("#800080", ColorUtils.toTkCode(new Color(128, 0, 128))); // purple
assertEquals("#00ff00", ColorUtils.toTkCode(new Color(0, 255, 0))); // lime
}
+
+ @Test(groups = { "Functional" })
+ public void testGetGraduatedColour()
+ {
+ Color minColour = new Color(100, 100, 100);
+ Color maxColour = new Color(180, 200, 220);
+
+ /*
+ * value half-way between min and max
+ */
+ Color col = ColorUtils.getGraduatedColour(20f, 10f, minColour, 30f,
+ maxColour);
+ assertEquals(140, col.getRed());
+ assertEquals(150, col.getGreen());
+ assertEquals(160, col.getBlue());
+
+ /*
+ * value two-thirds of the way between min and max
+ */
+ col = ColorUtils
+ .getGraduatedColour(30f, 10f, minColour, 40f, maxColour);
+ assertEquals(153, col.getRed());
+ // Color constructor rounds float value to nearest int
+ assertEquals(167, col.getGreen());
+ assertEquals(180, col.getBlue());
+
+ /*
+ * value = min
+ */
+ col = ColorUtils
+ .getGraduatedColour(10f, 10f, minColour, 30f, maxColour);
+ assertEquals(minColour, col);
+
+ /*
+ * value = max
+ */
+ col = ColorUtils
+ .getGraduatedColour(30f, 10f, minColour, 30f, maxColour);
+ assertEquals(maxColour, col);
+
+ /*
+ * value < min
+ */
+ col = ColorUtils.getGraduatedColour(0f, 10f, minColour, 30f, maxColour);
+ assertEquals(minColour, col);
+
+ /*
+ * value > max
+ */
+ col = ColorUtils
+ .getGraduatedColour(40f, 10f, minColour, 30f,
+ maxColour);
+ assertEquals(maxColour, col);
+ }
}