private Color interpolateColor( final Stop LOWER_BOUND, final Stop UPPER_BOUND, final double POSITION) { final double POS = (POSITION - LOWER_BOUND.getOffset()) / (UPPER_BOUND.getOffset() - LOWER_BOUND.getOffset()); final double DELTA_RED = (UPPER_BOUND.getColor().getRed() - LOWER_BOUND.getColor().getRed()) * POS; final double DELTA_GREEN = (UPPER_BOUND.getColor().getGreen() - LOWER_BOUND.getColor().getGreen()) * POS; final double DELTA_BLUE = (UPPER_BOUND.getColor().getBlue() - LOWER_BOUND.getColor().getBlue()) * POS; final double DELTA_OPACITY = (UPPER_BOUND.getColor().getOpacity() - LOWER_BOUND.getColor().getOpacity()) * POS; double red = Helper.clamp(0d, 1d, (LOWER_BOUND.getColor().getRed() + DELTA_RED)); double green = Helper.clamp(0d, 1d, (LOWER_BOUND.getColor().getGreen() + DELTA_GREEN)); double blue = Helper.clamp(0d, 1d, (LOWER_BOUND.getColor().getBlue() + DELTA_BLUE)); double opacity = Helper.clamp(0d, 1d, (LOWER_BOUND.getColor().getOpacity() + DELTA_OPACITY)); return Color.color(red, green, blue, opacity); }
// ******************** Methods ******************************************* public Color getColorAt(final double POSITION_OF_COLOR) { if (stops.isEmpty()) return Color.BLACK; final double POSITION = Helper.clamp(0d, 1d, POSITION_OF_COLOR); final Color COLOR; if (stops.size() == 1) { final Map<Double, Color> ONE_ENTRY = (Map<Double, Color>) stops.entrySet().iterator().next(); COLOR = stops.get(ONE_ENTRY.keySet().iterator().next()).getColor(); } else { Stop lowerBound = stops.get(0.0); Stop upperBound = stops.get(1.0); for (Double fraction : stops.keySet()) { if (Double.compare(fraction, POSITION) < 0) { lowerBound = stops.get(fraction); } if (Double.compare(fraction, POSITION) > 0) { upperBound = stops.get(fraction); break; } } COLOR = interpolateColor(lowerBound, upperBound, POSITION); } return COLOR; }