1 package org.simantics.district.network.profile;
6 * @author Tuukka Lehtonen
11 private static class Cache implements ColorGradient {
12 private final ColorGradient proxy;
13 private final int discretization;
14 private final Color[] cache;
16 public Cache(ColorGradient proxy, int discretization) {
18 this.discretization = discretization;
19 this.cache = new Color[discretization+1];
22 private int discretize(double t) {
23 return (int) (saturate(t) * discretization);
27 public Color get(double t) {
28 int d = discretize(t);
29 Color cached = cache[d];
32 cached = cache[d] = proxy.get(t);
37 public static final ColorGradient cached(ColorGradient cg) {
38 return new Cache(cg, 2048);
41 public static final ColorGradient hsvGradient(float hue, float saturation) {
43 return Color.getHSBColor(hue, saturation, saturate((float) t));
47 private static float saturate(float t) {
48 return Math.max(0f, Math.min(t, 1f));
51 private static double saturate(double t) {
52 return Math.max(0, Math.min(t, 1));