package ij.util;
import java.awt.Color;
import java.util.*;
import java.io.*;
import java.util.Comparator;
public class Tools {
public static final char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static String c2hex(Color c) {
int i = c.getRGB();
char[] buf7 = new char[7];
buf7[0] = '#';
for (int pos=6; pos>=1; pos--) {
buf7[pos] = hexDigits[i&0xf];
i >>>= 4;
}
return new String(buf7);
}
public static String f2hex(float f) {
int i = Float.floatToIntBits(f);
char[] buf9 = new char[9];
buf9[0] = '#';
for (int pos=8; pos>=1; pos--) {
buf9[pos] = hexDigits[i&0xf];
i >>>= 4;
}
return new String(buf9);
}
public static String int2hex(int i, int digits) {
char[] buf = new char[digits];
for (int pos=buf.length-1; pos>=0; pos--) {
buf[pos] = hexDigits[i&0xf];
i >>>= 4;
}
return new String(buf);
}
public static double[] getMinMax(double[] a) {
double min = Double.MAX_VALUE;
double max = -Double.MAX_VALUE;
double value;
for (int i=0; i<a.length; i++) {
value = a[i];
if (value<min)
min = value;
if (value>max)
max = value;
}
double[] minAndMax = new double[2];
minAndMax[0] = min;
minAndMax[1] = max;
return minAndMax;
}
public static double[] getMinMax(float[] a) {
double min = Double.MAX_VALUE;
double max = -Double.MAX_VALUE;
double value;
for (int i=0; i<a.length; i++) {
value = a[i];
if (value<min)
min = value;
if (value>max)
max = value;
}
double[] minAndMax = new double[2];
minAndMax[0] = min;
minAndMax[1] = max;
return minAndMax;
}
public static double[] toDouble(float[] a) {
int len = a.length;
double[] d = new double[len];
for (int i=0; i<len; i++)
d[i] = a[i];
return d;
}
public static float[] toFloat(double[] a) {
if (a==null)
return null;
int len = a.length;
float[] f = new float[len];
for (int i=0; i<len; i++)
f[i] = (float)a[i];
return f;
}
public static String fixNewLines(String s) {
if (s==null)
return null;
char[] chars = s.toCharArray();
for (int i=0; i<chars.length; i++)
{if (chars[i]=='\r') chars[i] = '\n';}
return new String(chars);
}
public static double parseDouble(String s, double defaultValue) {
if (s==null) return defaultValue;
try {
defaultValue = Double.parseDouble(s);
} catch (NumberFormatException e) {}
return defaultValue;
}
public static double parseDouble(String s) {
return parseDouble(s, Double.NaN);
}
public static int getDecimalPlaces(double n) {
if ((int)n==n || Double.isNaN(n))
return 0;
String s = ""+n;
if (s.contains("E"))
return -2;
while (s.endsWith("0"))
s = s.substring(0,s.length()-1);
int index = s.indexOf(".");
if (index==-1) return 0;
int digits = s.length() - index - 1;
if (digits>4) digits=4;
return digits;
}
public static int getDecimalPlaces(double n1, double n2) {
if ((int)n1==n1 && (int)n2==n2)
return 0;
int digits = getDecimalPlaces(n1);
int digits2 = getDecimalPlaces(n2);
if (digits==0)
return digits2;
if (digits2==0)
return digits;
if (digits<0 || digits2<0)
return digits;
if (digits2>digits)
digits = digits2;
return digits;
}
public static String[] split(String str) {
return split(str, " \t\n\r");
}
public static String[] split(String str, String delim) {
if (delim.equals("\n"))
return splitLines(str);
StringTokenizer t = new StringTokenizer(str, delim);
int tokens = t.countTokens();
String[] strings;
if (tokens>0) {
strings = new String[tokens];
for(int i=0; i<tokens; i++)
strings[i] = t.nextToken();
} else
strings = new String[0];
return strings;
}
static String[] splitLines(String str) {
Vector v = new Vector();
try {
BufferedReader br = new BufferedReader(new StringReader(str));
String line;
while (true) {
line = br.readLine();
if (line == null) break;
v.addElement(line);
}
br.close();
} catch(Exception e) { }
String[] lines = new String[v.size()];
v.copyInto((String[])lines);
return lines;
}
public static int[] rank(double[] values) {
int n = values.length;
final Integer[] indexes = new Integer[n];
final Double[] data = new Double[n];
for (int i=0; i<n; i++) {
indexes[i] = new Integer(i);
data[i] = new Double(values[i]);
}
Arrays.sort(indexes, new Comparator<Integer>() {
public int compare(final Integer o1, final Integer o2) {
return data[o1].compareTo(data[o2]);
}
});
int[] indexes2 = new int[n];
for (int i=0; i<n; i++)
indexes2[i] = indexes[i].intValue();
return indexes2;
}
public static int[] rank(final String[] data) {
int n = data.length;
final Integer[] indexes = new Integer[n];
for (int i=0; i<n; i++)
indexes[i] = new Integer(i);
Arrays.sort(indexes, new Comparator<Integer>() {
public int compare(final Integer o1, final Integer o2) {
return data[o1].compareToIgnoreCase(data[o2]);
}
});
int[] indexes2 = new int[n];
for (int i=0; i<n; i++)
indexes2[i] = indexes[i].intValue();
return indexes2;
}
public static String openFromIJJarAsString(String path) {
return (new ij.plugin.MacroInstaller()).openFromIJJar(path);
}
}