Ranides Atterwim 10 年 前
コミット
7d0aaef6b3

+ 2 - 1
assira/pom.xml

@@ -29,7 +29,8 @@
                             <includes>
                                 <include>net/ranides/assira/annotations/Meta*</include>
                                 <include>net/ranides/assira/annotations/javac/**</include>
-                                <include>net/ranides/assira/awt/**</include>
+                                <include>net/ranides/assira/awt/ImageLoader*</include>
+                                <include>net/ranides/assira/awt/AWTInvoker*</include>
                                 <include>net/ranides/assira/collection/IntCollectionUtils*</include>
                                 <include>net/ranides/assira/collection/CollectionUtils*</include>
                                 <include>net/ranides/assira/collection/Swapper*</include>

+ 10 - 9
assira/src/main/java/net/ranides/assira/awt/ColorUtils.java

@@ -8,15 +8,16 @@ package net.ranides.assira.awt;
 
 import java.awt.Color;
 import java.awt.Transparency;
+import java.util.Locale;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import net.ranides.assira.math.MathUtils;
 
 public final class ColorUtils {
     
-    private static final Pattern RE_RGBA = Pattern.compile("rgba? *\\( *([0-9]+) *, *([0-9]+) *, *([0-9]+)(?: *, *([0-9\\.]+) *)\\)");
+    private static final Pattern RE_RGBA = Pattern.compile("rgba? *\\( *([0-9]+) *, *([0-9]+) *, *([0-9]+)(?: *, *([0-9\\.]+) *)?\\)");
     
-    private static final Pattern RE_HSLA = Pattern.compile("hsla? *\\( *([0-9]+) *, *([0-9]+) *% *, *([0-9]+) *%(?: *, *([0-9\\.]+) *)\\)");
+    private static final Pattern RE_HSLA = Pattern.compile("hsla? *\\( *([0-9]+) *, *([0-9]+) *% *, *([0-9]+) *%(?: *, *([0-9\\.]+) *)?\\)");
 
     private ColorUtils() { }
 
@@ -43,7 +44,7 @@ public final class ColorUtils {
     }
 
     public static Color copy(Color color) {
-        return new Color( color.getRGB() );
+        return new Color( color.getRGB(), true);
     }
     
     public static String asCSS2(Color color) {
@@ -51,10 +52,10 @@ public final class ColorUtils {
     }
     
     public static String asCSS3(Color color) {
-        if( color.getTransparency() == Transparency.TRANSLUCENT && color.getAlpha()!=255) {
-            return String.format("rgba(%x,%x,%x,%x)", color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha() );
+        if(color.getAlpha()!=255) {
+            return String.format(Locale.ROOT, "rgba(%d,%d,%d,%.2f)", color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()/255.0 );
         } else {
-            return String.format("rgb(%x,%x,%x)", color.getRed(), color.getGreen(), color.getBlue() );
+            return String.format(Locale.ROOT, "rgb(%d,%d,%d)", color.getRed(), color.getGreen(), color.getBlue() );
         }
     }
     
@@ -69,7 +70,7 @@ public final class ColorUtils {
             Matcher hit = RE_RGBA.matcher(value);
             if(hit.matches()) {
                 return fromCSS_RGBA(hit);
-            }
+            }                    
         }
         if(value.startsWith("hsl")) {
             Matcher hit = RE_HSLA.matcher(value);
@@ -125,10 +126,10 @@ public final class ColorUtils {
             return (c-'0');
         }
         if( c>='a' && c<='z') {
-            return (c-'a');
+            return 10+(c-'a');
         }
         if( c>='A' && c<='Z') {
-            return (c-'A');
+            return 10+(c-'A');
         }
         throw new IllegalArgumentException("Invalid digit: " + c);
     }

+ 136 - 0
assira/src/test/java/net/ranides/assira/awt/ColorUtilsTest.java

@@ -0,0 +1,136 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.awt;
+
+import java.awt.Color;
+import java.awt.color.ColorSpace;
+import net.ranides.assira.junit.NewAssert;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class ColorUtilsTest {
+    
+    public ColorUtilsTest() {
+    }
+
+    @Test
+    public void testInvert() {
+        assertEquals(new Color(255-17, 255-23, 255-37, 125), ColorUtils.invert( new Color(17,23,37,125) ));
+        assertEquals(new Color(0, 1, 2, 17), ColorUtils.invert( new Color(255,254,253,17) ));
+        assertEquals(new Color(128, 127, 0, 128), ColorUtils.invert( new Color(127,128,255,128) ));
+    }
+    
+    @Test
+    public void testAlpha() {
+        assertEquals(new Color(17, 23, 150, 30), ColorUtils.alpha( new Color(17,23,150,30), 30));
+        assertEquals(new Color(17, 23, 150, 50), ColorUtils.alpha( new Color(17,23,150,30), 50));
+        assertEquals(new Color(0x20, 0x30, 0x40, 50), ColorUtils.alpha( new Color(0x20, 0x30, 0x40), 50));
+        assertEquals(new Color(0x20, 0x30, 0x40, 50), ColorUtils.alpha( new Color(0x203040, false), 50));
+        assertEquals(new Color(0x20, 0x30, 0x40, 0x40), ColorUtils.alpha( new Color(0x203040, true), 64));
+        
+        assertEquals(new Color(0x40203050, true), ColorUtils.alpha( new Color(0x88203050, true), 64));
+        assertEquals(new Color(0x40203050, true), ColorUtils.alpha( new Color(0x88203050, false), 64));
+    }
+    
+    @Test
+    public void testMerge() {
+        Color c1 = new Color(30, 60, 90, 120);
+        Color c2 = new Color(90, 60, 10, 200);
+        
+         assertEquals(new Color(45, 60, 70, 140), ColorUtils.merge(c1, c2, 0.25));
+         assertEquals(new Color(60, 60, 50, 160), ColorUtils.merge(c1, c2, 0.5));
+         assertEquals(new Color(75, 60, 30, 180), ColorUtils.merge(c1, c2, 0.75));
+         assertEquals(c1, ColorUtils.merge(c1, c2, 0.0));
+         assertEquals(c2, ColorUtils.merge(c1, c2, 1.0));
+    }
+    
+    @Test
+    public void testCopy() {
+        Color c1 = new Color(30, 60, 90, 120);
+        Color c2 = new Color(90, 60, 10, 200);
+        Color c3 = new Color(10, 20, 30);
+        
+        assertEquals(c1, ColorUtils.copy(c1));
+        assertEquals(c2, ColorUtils.copy(c2));
+        assertEquals(c3, ColorUtils.copy(c3));
+    }
+    
+    @Test
+    public void testAsCSS2() {
+        assertEquals("#103472", ColorUtils.asCSS2(new Color(0x10, 0x34, 0x72)));
+        assertEquals("#103472", ColorUtils.asCSS2(new Color(0x10, 0x34, 0x72, 100)));
+    }
+    
+    @Test
+    public void testAsCSS3() {
+        assertEquals("rgb(10,34,72)", ColorUtils.asCSS3(new Color(10, 34, 72)));
+        assertEquals("rgb(10,34,72)", ColorUtils.asCSS3(new Color(10, 34, 72, 255)));
+        
+        assertEquals("rgba(10,34,72,0.00)", ColorUtils.asCSS3(new Color(10, 34, 72, 0)));
+        assertEquals("rgba(10,34,72,0.10)", ColorUtils.asCSS3(new Color(10, 34, 72, 25)));
+        assertEquals("rgba(10,34,72,0.50)", ColorUtils.asCSS3(new Color(10, 34, 72, 128)));
+    }
+    
+    @Test
+    public void testFromCSS() {
+        assertEquals(new Color(0x10, 0x34, 0x72), ColorUtils.fromCSS("#103472"));
+        assertEquals(new Color(0x11, 0x33, 0x44), ColorUtils.fromCSS("#134"));
+        assertEquals(new Color(0x11, 0xCC, 0xAA), ColorUtils.fromCSS("#1Ca"));
+        
+        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
+            ColorUtils.fromCSS("nocolor");
+        });
+        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
+            ColorUtils.fromCSS("#1C!");
+        });
+        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
+            ColorUtils.fromCSS("#11");
+        });
+        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
+            ColorUtils.fromCSS("#1034728");
+        });
+        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
+            ColorUtils.fromCSS("#10347280");
+        });
+        
+        assertEquals(new Color(10, 34, 72), ColorUtils.fromCSS("rgb(10,34,72)"));
+        assertEquals(new Color(10, 34, 72, 255), ColorUtils.fromCSS("rgb(10,34,72)"));
+
+        assertEquals(new Color(10, 34, 72, 0), ColorUtils.fromCSS("rgba(10,34,72,0.00)"));
+        assertEquals(new Color(10, 34, 72, 0), ColorUtils.fromCSS("rgba (10, 34,  72, 0)"));
+        
+        assertEquals(new Color(10, 34, 72, 64), ColorUtils.fromCSS("rgba(10,34,72,0.25)"));
+        assertEquals(new Color(10, 34, 72, 191), ColorUtils.fromCSS("rgba(10,34,72,0.75 )"));
+          
+        assertEquals(new Color(0,128,0), ColorUtils.fromCSS("hsl(120, 100%, 50%)"));
+        assertEquals(new Color(120,191,48), ColorUtils.fromCSS("hsl(90, 75%, 75%)"));
+        assertEquals(new Color(191,191,48), ColorUtils.fromCSS("hsl(60, 75%, 75%)"));
+        
+        assertEquals(new Color(0,128,0,0), ColorUtils.fromCSS("hsl(120, 100%, 50%, 0)"));
+        assertEquals(new Color(120,191,48,64), ColorUtils.fromCSS("hsl(90, 75%, 75%, 0.25)"));
+        assertEquals(new Color(191,191,48,191), ColorUtils.fromCSS("hsl(60, 75%, 75%, 0.75)"));
+        
+        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
+            ColorUtils.fromCSS("rgb(x");
+        });
+        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
+            ColorUtils.fromCSS("rgba(x");
+        });
+        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
+            ColorUtils.fromCSS("hsl(x");
+        });
+        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
+            ColorUtils.fromCSS("hsla(x");
+        });
+        
+    }
+    
+}