FileDocCategorySizeDatePackage
ColorCache.javaAPI DocAzureus 3.0.3.43131Thu Jun 07 12:00:54 BST 2007com.aelitis.azureus.ui.swt.utils

ColorCache

public class ColorCache extends Object
author
TuxPaper
created
Jun 30, 2006

Fields Summary
private static final Map
mapColors
Constructors Summary
Methods Summary
private static voidaddColor(java.lang.Long key, org.eclipse.swt.graphics.Color color)

		mapColors.put(key, color);
	
public static org.eclipse.swt.graphics.ColorgetColor(org.eclipse.swt.graphics.Device device, int red, int green, int blue)

	
	 
		AEDiagnostics.addEvidenceGenerator(new AEDiagnosticsEvidenceGenerator() {
			public void generate(IndentWriter writer) {
				writer.println("Colors:");
				writer.indent();
				writer.println("# cached: " + mapColors.size());
				writer.exdent();
			}
		});
	
		if (mapColors.size() == 0) {
			for (int i = 1; i <= 16; i++) {
				Color color = device.getSystemColor(i);
				Long key = new Long(((long) color.getRed() << 16)
						+ (color.getGreen() << 8) + color.getBlue());
				addColor(key, color);
			}
		}

		Long key = new Long(((long) red << 16) + (green << 8) + blue);

		Color color = (Color) mapColors.get(key);
		if (color == null || color.isDisposed()) {
			try {
				color = new Color(device, red, green, blue);
			} catch (IllegalArgumentException e) {
				Debug.out("One Invalid: " + red + ";" + green + ";" + blue, e);
			}
			addColor(key, color);
		}

		return color;
	
public static org.eclipse.swt.graphics.ColorgetColor(org.eclipse.swt.graphics.Device device, java.lang.String value)

		int[] colors = new int[3];

		if (value == null || value.length() == 0) {
			return null;
		}

		try {
			if (value.charAt(0) == '#") {
				// hex color string
				long l = Long.parseLong(value.substring(1), 16);
				colors[0] = (int) ((l >> 16) & 255);
				colors[1] = (int) ((l >> 8) & 255);
				colors[2] = (int) (l & 255);
			} else {
				StringTokenizer st = new StringTokenizer(value, ",");
				colors[0] = Integer.parseInt(st.nextToken());
				colors[1] = Integer.parseInt(st.nextToken());
				colors[2] = Integer.parseInt(st.nextToken());
			}
		} catch (Exception e) {
			Debug.out(value, e);
			return null;
		}

		return getColor(device, colors[0], colors[1], colors[2]);