[Schmitzm-commits] r844 - in trunk: src/schmitzm/swing src_junit/schmitzm src_junit/schmitzm/swing

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed May 19 14:41:02 CEST 2010


Author: alfonx
Date: 2010-05-19 14:41:02 +0200 (Wed, 19 May 2010)
New Revision: 844

Added:
   trunk/src_junit/schmitzm/swing/
   trunk/src_junit/schmitzm/swing/TestSwingUtil.java
Modified:
   trunk/src/schmitzm/swing/SwingUtil.java
Log:
Die Mehtode convertColorToHex in SwingUtil erweitert, dass optinoal auch der AlphaKanal convertiert werden kann. Weiterhin f?\195?\188r die Methode einen Test hinzugef?\195?\188gt.

Modified: trunk/src/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/src/schmitzm/swing/SwingUtil.java	2010-05-17 17:14:42 UTC (rev 843)
+++ trunk/src/schmitzm/swing/SwingUtil.java	2010-05-19 12:41:02 UTC (rev 844)
@@ -861,30 +861,43 @@
 	}
 
 	/**
-	 * Versucht, aus einem String eine Farbe zu erstellen. Drei Moeglichekeiten
-	 * gibt es fuer das Format des Strings:
-	 * <ol>
-	 * <li><code>"RGB(<i>red</i>,<i>green</i>,<i>blue</i>)"</code><br>
-	 * wobei <i>red</i>,<i>green</i> und <i>blue</i> dezimale Werte zwischen 0
-	 * und 255 sind.</li>
-	 * <li>Der String stellt einen Integer-Wert im dezimalen, oktalen oder
-	 * hexadezimalen Format dar, aus dem die 3 RGB-Werte extrahiert werden
-	 * (siehe {@link Color#decode(String) Color.decode(..)}).</li>
-	 * <li>Der String spezifiziert ein statisches Feld der Klasse {@link Color}.
-	 * <br>
-	 * z.B. steht <code>"RED"</code> fuer {@link Color#RED Color.RED},
-	 * <code>"darkGray"</code> fuer {@link Color#darkGray Color.darkGray} oder
-	 * <code>"LIGHT_GRAY"</code> fuer {@link Color#LIGHT_GRAY Color.LIGHT_GRAY}</li>
-	 * </ol>
+	 * Wandelt ein {@link Color} Objekt in die RGB HEX Schreibweise mit
+	 * führendem Hash-Zeichen um, z.B. in "#22F208". Die Alpha-Kanal wird
+	 * ignoriert.
+	 */
+	public static String convertColorToHex(Color color) {
+		return convertColorToHex(color, false, true);
+//		int r = color.getRed();
+//		int g = color.getGreen();
+//		int b = color.getBlue();
+//
+//		// Convert RGB to 2-digit Hex
+//		String rHex = Integer.toHexString(r);
+//		if (rHex.length() < 2)
+//			rHex = "0" + rHex;
+//		String gHex = Integer.toHexString(g);
+//		if (gHex.length() < 2)
+//			gHex = "0" + gHex;
+//		String bHex = Integer.toHexString(b);
+//		if (bHex.length() < 2)
+//			bHex = "0" + bHex;
+//
+//		// combine RGB to Hex string
+//		return "#" + rHex + gHex + bHex;
+	}
+
+	/**
+	 * Wandelt ein {@link Color} Objekt in die RGB HEX Schreibweise um.
 	 * 
-	 * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
-	 *         (University of Bonn/Germany)
+	 * @param withAlpha wenn <code>true</code>, dann wird der alpha Kanal auch convertiert
+	 * @param withHash wenn <code>true</code>, dann wird das #-Zeichen vorne angefügt.  
 	 * 
-	 * @throws IllegalArgumentException
-	 *             wenn der uebergebene String nicht interpretiert werden kann.
-	 * 
 	 */
-	public static String convertColorToHex(Color color) {
+	public static String convertColorToHex(Color color, boolean withAlpha,
+			boolean withHash) {
+		
+		StringBuffer result = new StringBuffer(9);
+		
 		int r = color.getRed();
 		int g = color.getGreen();
 		int b = color.getBlue();
@@ -893,15 +906,32 @@
 		String rHex = Integer.toHexString(r);
 		if (rHex.length() < 2)
 			rHex = "0" + rHex;
+		result.append(rHex);
+		
 		String gHex = Integer.toHexString(g);
 		if (gHex.length() < 2)
 			gHex = "0" + gHex;
+		result.append(gHex);
+		
 		String bHex = Integer.toHexString(b);
 		if (bHex.length() < 2)
 			bHex = "0" + bHex;
+		result.append(bHex);		
 
+		if (withAlpha) {
+			int a = color.getAlpha();
+			String aHex = Integer.toHexString(a);
+			if (aHex.length() < 2)
+				aHex = "0" + aHex;
+			result.append(aHex);			
+		}
+		
+		if (withHash) {
+			result.insert(0,"#");			
+		}
+
 		// combine RGB to Hex string
-		return "#" + rHex + gHex + bHex;
+		return result.toString();
 	}
 
 	/**
@@ -1169,18 +1199,18 @@
 				if (e.getWheelRotation() < 0 && upIsUp
 						|| e.getWheelRotation() > 0 && !upIsUp) {
 					if (comboBox.getSelectedIndex() < comboBox.getItemCount() - 1) {
-//						System.out.println("switching from "
-//								+ comboBox.getSelectedIndex() + " to "
-//								+ (comboBox.getSelectedIndex() + 1));
+						// System.out.println("switching from "
+						// + comboBox.getSelectedIndex() + " to "
+						// + (comboBox.getSelectedIndex() + 1));
 
 						comboBox
 								.setSelectedIndex(comboBox.getSelectedIndex() + 1);
 					}
 				} else {
 					if (comboBox.getSelectedIndex() > 0) {
-//						System.out.println("switching from "
-//								+ comboBox.getSelectedIndex() + " to "
-//								+ (comboBox.getSelectedIndex() - 1));
+						// System.out.println("switching from "
+						// + comboBox.getSelectedIndex() + " to "
+						// + (comboBox.getSelectedIndex() - 1));
 						comboBox
 								.setSelectedIndex(comboBox.getSelectedIndex() - 1);
 					}

Added: trunk/src_junit/schmitzm/swing/TestSwingUtil.java
===================================================================
--- trunk/src_junit/schmitzm/swing/TestSwingUtil.java	2010-05-17 17:14:42 UTC (rev 843)
+++ trunk/src_junit/schmitzm/swing/TestSwingUtil.java	2010-05-19 12:41:02 UTC (rev 844)
@@ -0,0 +1,25 @@
+package schmitzm.swing;
+
+import static org.junit.Assert.*;
+
+import java.awt.Color;
+
+import org.junit.Test;
+
+public class TestSwingUtil {
+
+	@Test
+	public void testConvertColorToHexColorBooleanBoolean() {
+		assertEquals("#ff0000ff", SwingUtil.convertColorToHex(Color.RED, true,
+				true));
+		assertEquals("#00ff00", SwingUtil.convertColorToHex(Color.GREEN, false,
+				true));
+		assertEquals("0000ff", SwingUtil.convertColorToHex(Color.BLUE, false,
+				false));
+	}
+
+	@Test
+	public void testConvertColorToHexColor() {
+		assertEquals("#0000ff", SwingUtil.convertColorToHex(Color.BLUE));
+	}
+}
\ No newline at end of file


Property changes on: trunk/src_junit/schmitzm/swing/TestSwingUtil.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id URL
Name: svn:eol-style
   + native



More information about the Schmitzm-commits mailing list