HTML Color parsen

tomkruse

Erfahrenes Mitglied
Hi!

Neulich habe ich irgendwo in der API-Doc eine Möglichkeit gefunden, wie man HTML-Colors die als String vorliegen(#ff34e5, red, green, #5562e1 etc.) in eine java.awt.Color umwandeln kann. Jetzt würde ich das brauchen und kanns nicht mehr finden. Mist! Weiß zufällig wer von Euch wo diese Methode drinnensteckt?

Cu - Tom.
 
Hallo!

Finde die Klasse im Moment auch nicht, aber wie wärs damit?
Code:
import java.awt.Color;
	import java.io.*;
	class ColorConvert {
		public static String HTMLFromColor(Color theColor) {
			return Integer.toString(theColor.getRGB() & 0xffffff, 16);
		}
		public static Color ColorFromHTML(String theColor) {
			if (theColor.length() != 6)
				throw new IllegalArgumentException("Not a valid HTML color");
			return new Color(
				Integer.valueOf(theColor.substring(0, 2), 16).intValue(),
				Integer.valueOf(theColor.substring(2, 4), 16).intValue(),
				Integer.valueOf(theColor.substring(4, 6), 16).intValue());
		}
	}
	//	  Quelle: http://onesearch.sun.com/ClickThru?qt=html+color&url=http%3A%2F%2Fforum.java.sun.com%2Fthread.jsp%3Fforum%3D31%26thread%3D55815&pathInfo=%2Fsearch%2Fdevelopers%2Findex.jsp&hitNum=3&col=devforums

Gruß tom
 
Hi!

Tja, das würde natürlich klappen, aber nicht in allen Fällen, so funktioniert es nicht wenn "red" oder "green" etc. übergeben wird. Habe aber irgendwo eine Methode gesehen, die das kann.

Kann man irgendwo eine Übersicht ALLER Methoden von java 2 finden? Dann müßte ich nur noch nach einer Methode suchen, die String als Parameter hat und Color zurückgibt. Da wird es nicht allzuviele geben.

viele Grüße
Tom.
 
Hallo!

Versuchs mal mit einer der folgenden Klassen:

Code:
com.sun.java.swing.plaf.gtk.GTKParser : parseColorString ( ... )
com.sun.java.swing.plaf.gtk.XColors : lookupColor ( ... )
com.sun.java.swing.plaf.windows.XPStyle : getColor ( ... )
java.awt.Color : decode ( ... )
java.awt.Color : getColor ( ... )
java.awt.Color : getColor ( ... )
java.awt.Color : getColor ( ... )
java.awt.Color : brighter ( ... )
java.awt.Color : darker ( ... )
java.awt.Color : getHSBColor ( ... )
javax.swing.JColorChooser : showDialog ( ... )
javax.swing.text.html.CSS : stringToColor ( ... )
javax.swing.text.html.CSS : hexToColor ( ... )
javax.swing.text.html.CSS : parseRGB ( ... )
javax.swing.text.html.StyleSheet : stringToColor ( ... )




Ermittelt mit:

Einfach die Datei rt.jar, welche für gewöhnlich im %JAVA_HOME%\jre\lib
liegt, in ein Verzeichniss entpacken wie hier c:\java_src dann folgendes
Programm laufen lassen.

In der Klasse eben noch gewünschten return und parameter Wert angeben.

Code:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;

/*
 * Created on 04.03.2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

/**
 * @author Darimont
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class JavaInfo {

	private String lookForReturnType = "java.awt.Color";
	private String lookForParamType = "java.lang.String";
	private boolean paramMatch = false;
	private boolean returnMatch = false;

	private ClassLoader loader;

	static int i = 0;

	private FileWriter fw;
	private BufferedWriter bw;

	public static void main(String[] args) {

		new JavaInfo().doIt();
		System.out.println(i);
	}

	/**
	 * 
	 */
	private void doIt() {
		// TODO Auto-generated method stub

		loader = getClass().getClassLoader();
		try {
			fw = new FileWriter("c:/all_classes.txt");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (fw == null) {
			System.err.println("fw == null");
			return;
		}

		bw = new BufferedWriter(fw);

		scan(new File("c:/java_src"));

		try {
			bw.flush();
			bw.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		System.out.println("Klassen durchsucht: " + i);
	}

	public void scan(File dir) {
		// Liste aller Dateien und Unterverzeichnisse holen
		String[] entries = dir.list();
		if (entries == null || entries.length < 1) {
			return;
		}
		for (int i = 0; i < entries.length; i++) {
			File entry = new File(dir, entries[i]);
			if (entry.isDirectory()) {
				scan(entry); // rekursiv ins Unterverzeichnis verzweigen
			} else {
				examineEntry(entry);
			}
		}
	}

	/**
	 * @param entry
	 */
	private void examineEntry(File entry) {

		returnMatch = false;
		paramMatch = false;

		if (i % 500 == 0) {
			System.out.println("finished to " + i / 90 + " %");
			// Rund 9000 Klassfiles
		}

		if (!entry.toString().endsWith(".class"))
			return;
		i++;
		//StringBuffer buffer = new StringBuffer(80);
		String entryStr = entry.toString();
		int pos = entryStr.indexOf('.');
		String fqcn = entryStr.substring(12, pos); //full qualified class name
		fqcn = fqcn.replace('\\', '.');
		Class clazz = null;

		try {
			clazz = loader.loadClass(fqcn);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (clazz == null)
			return;

		Method[] methods = clazz.getDeclaredMethods();

		for (int i = 0; i < methods.length; i++) {
			if (methods[i]
				.getReturnType()
				.getName()
				.equals(lookForReturnType)) {

				returnMatch = true;

				Class[] cParams = methods[i].getParameterTypes();
				for (int j = 0; j < cParams.length; j++) {
					if (cParams[j].getName().equals(lookForParamType))
						paramMatch = true;
				}

				if (returnMatch && paramMatch) {
					try {
						bw.write(fqcn +" : "+ methods[i].getName() +" ( ... )" + '\r' + '\n');
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}

			} else {
				continue;
			}
		}
	}

}

Ps.: Ich glaube du suchst: javax.swing.text.html.StyleSheet : stringToColor ( ... )
;-)

Gruß Tom
 
Hi!

Ja, genau das hab ich gesucht ... und mittlerweile auch schon selber wieder gefunden ;-)

Trotzdem Danke!

Cu - Tom.
 

Neue Beiträge

Zurück