/*
 * @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");
        });
        
    }
    
}
