package net.clgd.ccemux;

import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import dan200.computercraft.shared.util.Colour;

public class Utils {

	public static final String BASE_16 = "0123456789abcdef";

	public static int base16ToInt(char c) {
		return BASE_16.indexOf(String.valueOf(c).toLowerCase());
	}

	public static char intToBase16(int p) {
		return BASE_16.charAt(p);
	}

	public static Color getCCColourFromInt(int i) {
		Colour col = Colour.fromInt(15 - i);
		return col == null ? Color.WHITE : new Color(col.getR(), col.getG(), col.getB());
	}

	public static Color getCCColourFromChar(char c) {
		return getCCColourFromInt(base16ToInt(c));
	}

	public static double[] clampColor(double[] col) {
		return new double[]{clamp(col[0], 0f, 1f), clamp(col[1], 0f, 1f), clamp(col[2], 0f, 1f)};
	}

	public static double clamp(double f, double min, double max) {
		if (f > max) {
			return max;
		} else if (f < min) {
			return min;
		} else {
			return f;
		}
	}

	public static BufferedImage makeTintedCopy(BufferedImage bi, Color tint) {
		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
				.getDefaultConfiguration();

		BufferedImage tinted = gc.createCompatibleImage(bi.getWidth(), bi.getHeight(), Transparency.TRANSLUCENT);

		for (int y = 0; y < bi.getHeight(); y++) {
			for (int x = 0; x < bi.getWidth(); x++) {
				int rgb = bi.getRGB(x, y);

				if (rgb != 0)
					tinted.setRGB(x, y, tint.getRGB());
			}
		}

		return tinted;
	}
}

@@ -19,16 +19,86 @@
 cemux;%0A%0A
+import static com.google.common.primitives.Doubles.constrainToRange;%0A%0A
 import j
@@ -915,14 +915,27 @@
 le%5B%5D
-%7Bclamp
+ %7B constrainToRange
 (col
@@ -944,229 +944,83 @@
 %5D, 0
-f
 , 1
-f
 ), c
-lamp(col%5B1%5D, 0f, 1f), clamp
+onstrainToRange
 (col%5B
-2
+1
 %5D, 0
-f
 , 1
-f)%7D;%0A%09%7D%0A%0A%09public static double clamp(double f, double min, double max) %7B%0A%09%09if (f %3E max) %7B%0A%09%09%09return max;%0A%09%09%7D else if (f %3C min) %7B%0A%09%09%09return min;%0A%09%09%7D else %7B%0A%09%09%09return f;%0A%09%09%7D
+),%0A%09%09%09%09constrainToRange(col%5B2%5D, 0, 1) %7D;
 %0A%09%7D%0A
@@ -1485,14 +1485,9 @@
 = 0)
-%0A%09%09%09%09%09
+ 
 tint

Improve Utils.clampColor