X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.network%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fprofile%2FColors.java;fp=org.simantics.district.network%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fnetwork%2Fprofile%2FColors.java;h=9ead5c38f3d223883bf41c5c7df313722e50772c;hb=00803fe68b14b5dce310a5a88b9c2a55bd1c85e6;hp=0000000000000000000000000000000000000000;hpb=b654d9f368d601565303bcf646fc4fbdfbc37090;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.network/src/org/simantics/district/network/profile/Colors.java b/org.simantics.district.network/src/org/simantics/district/network/profile/Colors.java new file mode 100644 index 00000000..9ead5c38 --- /dev/null +++ b/org.simantics.district.network/src/org/simantics/district/network/profile/Colors.java @@ -0,0 +1,55 @@ +package org.simantics.district.network.profile; + +import java.awt.Color; + +/** + * @author Tuukka Lehtonen + * @since 1.35.0 + */ +public class Colors { + + private static class Cache implements ColorGradient { + private final ColorGradient proxy; + private final int discretization; + private final Color[] cache; + + public Cache(ColorGradient proxy, int discretization) { + this.proxy = proxy; + this.discretization = discretization; + this.cache = new Color[discretization+1]; + } + + private int discretize(double t) { + return (int) (saturate(t) * discretization); + } + + @Override + public Color get(double t) { + int d = discretize(t); + Color cached = cache[d]; + if (cached != null) + return cached; + cached = cache[d] = proxy.get(t); + return cached; + } + } + + public static final ColorGradient cached(ColorGradient cg) { + return new Cache(cg, 512); + } + + public static final ColorGradient hsvGradient(float hue, float saturation) { + return t -> { + return Color.getHSBColor(hue, saturation, saturate((float) t)); + }; + } + + private static float saturate(float t) { + return Math.max(0f, Math.min(t, 1f)); + } + + private static double saturate(double t) { + return Math.max(0, Math.min(t, 1)); + } + +}