1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.scene.paint;
  27 
  28 import javafx.animation.Interpolatable;
  29 import java.lang.reflect.Field;
  30 import java.lang.reflect.Modifier;
  31 import java.util.ArrayList;
  32 import com.sun.javafx.util.Utils;
  33 import org.junit.Test;
  34 import static org.junit.Assert.assertEquals;
  35 import static org.junit.Assert.assertFalse;
  36 import static org.junit.Assert.assertNotNull;
  37 import static org.junit.Assert.assertTrue;
  38 
  39 public class ColorTest {
  40     
  41     @Test
  42     public void testRedIsBoundedBy0And1() {
  43         try {
  44             Color c = new Color(-1, 0, 0, 0);
  45             assertTrue(false);
  46         } catch (Exception e) {}
  47         
  48         try {
  49             Color c = new Color(100, 0, 0, 0);
  50             assertTrue(false);
  51         } catch (Exception ee) {}
  52     }
  53 
  54     @Test
  55     public void testGreenIsBoundedBy0And1() {
  56         try {
  57             Color c = new Color(0, -1, 0, 0);
  58             assertTrue(false);
  59         } catch (Exception e) {}
  60 
  61         try {
  62             Color c = new Color(0, 100, 0, 0);
  63             assertTrue(false);
  64         } catch (Exception ee) {}
  65     }
  66     
  67     @Test
  68     public void testBlueIsBoundedBy0And1() {
  69         try {
  70             Color c = new Color(0, 0, -1, 0);
  71             assertTrue(false);
  72         } catch (Exception e) {}
  73 
  74         try {
  75             Color c = new Color(0, 0, 100, 0);
  76             assertTrue(false);
  77         } catch (Exception ee) {}
  78     }
  79 
  80     @Test
  81     public void testOpacityIsBoundedBy0And1() {
  82         try {
  83             Color c = new Color(0, 0, 0, -1);
  84             assertTrue(false);
  85         } catch (Exception e) {}
  86 
  87         try {
  88             Color c = new Color(0, 0, 0, 100);
  89             assertTrue(false);
  90         } catch (Exception ee) {}
  91     }
  92     
  93     @Test
  94     public void testOfTheWay() {
  95         Color start = new Color(0, 0, 0, 0);
  96         Color end = new Color(1, 1, 1, 1);
  97         Color mid = start.interpolate(end, .5);
  98         assertEquals(mid.getRed(), .5, 0.0001);
  99         assertEquals(mid.getGreen(), .5, 0.0001);
 100         assertEquals(mid.getBlue(), .5, 0.0001);
 101         assertEquals(mid.getOpacity(), .5, 0.0001);
 102     }
 103 
 104     @Test
 105     public void testOfTheWayAll()
 106         throws IllegalArgumentException, IllegalAccessException
 107     {
 108         ArrayList<Color> colors = new ArrayList<Color>();
 109         for (Field f : Color.class.getDeclaredFields()) {
 110             if (Modifier.isStatic(f.getModifiers()) &&
 111                 f.getType() == Color.class)
 112             {
 113                 Color c = (Color) f.get(null);
 114                 colors.add(c);
 115             }
 116         }
 117         for (Color c1 : colors) {
 118             for (Color c2 : colors) {
 119                 c1.interpolate(c2, 0.0);
 120                 c1.interpolate(c2, Double.MIN_VALUE);
 121                 c1.interpolate(c2, 0.5);
 122                 c1.interpolate(c2, 1.0 - Double.MIN_VALUE);
 123                 c1.interpolate(c2, 1.0);
 124             }
 125         }
 126     }
 127 
 128     @Test
 129     public void testOfTheWayIndirect() {
 130         Interpolatable<Color> start = new Color(0, 0, 0, 0);
 131         Color end = new Color(1, 1, 1, 1);
 132         Color mid = start.interpolate(end, .5);
 133         assertEquals(mid.getRed(), .5, 0.0001);
 134         assertEquals(mid.getGreen(),.5, 0.0001);
 135         assertEquals(mid.getBlue(), .5, 0.0001);
 136         assertEquals(mid.getOpacity(), .5, 0.0001);
 137     }
 138     
 139     @Test
 140     public void testColorIsBoundedBy0And1() {
 141         try {
 142             Color color = Color.color(-1, 0, 0, 0);
 143             assertTrue(false);
 144         } catch (Exception e) {}
 145         
 146         try {
 147             Color color = Color.color(0, -1, 0, 0);
 148             assertTrue(false);
 149         } catch (Exception ee) {}
 150 
 151         try {
 152             Color color = Color.color(0, 0, -1, 0);
 153             assertTrue(false);
 154         } catch (Exception eee) {}
 155 
 156         try {
 157             Color color = Color.color(0, 0, 0, -1);
 158             assertTrue(false);
 159         } catch (Exception eeee) {}
 160 
 161         try {
 162             Color color = Color.color(2, 0, 0, 0);
 163             assertTrue(false);
 164         } catch (Exception eeeee) {}
 165 
 166         try {
 167             Color color = Color.color(0, 2, 0, 0);
 168             assertTrue(false);
 169         } catch (Exception eeeeee) {}
 170 
 171         try {
 172             Color color = Color.color(0, 0, 2, 0);
 173             assertTrue(false);
 174         } catch (Exception eeeeeee) {}
 175 
 176         try {
 177             Color color = Color.color(0, 0, 0, 2);
 178             assertTrue(false);
 179         } catch (Exception eeeeeeee) {}
 180     }
 181     
 182     @Test
 183     public void testColor() {
 184         Color color = Color.color(.1, .2, .3);
 185         assertEquals(.1, color.getRed(), 0.0001);
 186         assertEquals(.2, color.getGreen(), 0.0001);
 187         assertEquals(.3, color.getBlue(), 0.0001);
 188         assertEquals(1, color.getOpacity(), 0.0001);
 189     }
 190     
 191     @Test
 192     public void testColorWithOpacity() {
 193         Color color = Color.color(.1, .2, .3, .4);
 194         assertEquals(.1, color.getRed(), 0.0001);
 195         assertEquals(.2, color.getGreen(), 0.0001);
 196         assertEquals(.3, color.getBlue(), 0.0001);
 197         assertEquals(.4, color.getOpacity(), 0.0001);
 198     }
 199 
 200     @Test
 201     public void testRgbIsBoundedBy0And255() {
 202         try {
 203             Color color = Color.rgb(-1, 0, 0, 0);
 204             assertTrue(false);
 205         } catch (Exception e) {}
 206         
 207         try {
 208             Color color = Color.rgb(0, -1, 0, 0);
 209             assertTrue(false);
 210         } catch (Exception ee) {}
 211 
 212         try {
 213             Color color = Color.rgb(0, 0, -1, 0);
 214             assertTrue(false);
 215         } catch (Exception eee) {}
 216 
 217         try {
 218             Color color = Color.rgb(0, 0, 0, -1);
 219             assertTrue(false);
 220         } catch (Exception eeee) {}
 221 
 222         try {
 223             Color color = Color.rgb(300, 0, 0, 0);
 224             assertTrue(false);
 225         } catch (Exception eeeee) {}
 226 
 227         try {
 228             Color color = Color.rgb(0, 300, 0, 0);
 229             assertTrue(false);
 230         } catch (Exception eeeeee) {}
 231 
 232         try {
 233             Color color = Color.rgb(0, 0, 300, 0);
 234             assertTrue(false);
 235         } catch (Exception eeeeeee) {}
 236 
 237         try {
 238             Color color = Color.rgb(0, 0, 0, 300);
 239             assertTrue(false);
 240         } catch (Exception eeeeeeee) {}
 241     }
 242     
 243     @Test
 244     public void testRgb() {
 245         Color color = Color.rgb(255, 0, 255, 0);
 246         assertTrue(color.getRed() == 1);
 247         assertTrue(color.getGreen() == 0);
 248         assertTrue(color.getBlue() == 1);
 249         assertTrue(color.getOpacity() == 0);
 250         double[] hsb = Utils.RGBtoHSB(1.0, 0.0, 1.0);
 251         assertEquals(hsb[0], color.getHue(), 0.0001);
 252         assertEquals(hsb[1], color.getSaturation(), 0.0001);
 253         assertEquals(hsb[2], color.getBrightness(), 0.0001);
 254     }
 255     
 256     @Test
 257     public void testGray() {
 258         Color color = Color.gray(0.5);
 259         assertEquals(0.5, color.getRed(), 0.0001);
 260         assertEquals(0.5, color.getGreen(), 0.0001);
 261         assertEquals(0.5, color.getBlue(), 0.0001);
 262         assertEquals(1.0, color.getOpacity(), 0.0001);
 263     }
 264 
 265     @Test
 266     public void testGrayWithOpacity() {
 267         Color color = Color.gray(0.5, 0.3);
 268         assertEquals(0.5, color.getRed(), 0.0001);
 269         assertEquals(0.5, color.getGreen(), 0.0001);
 270         assertEquals(0.5, color.getBlue(), 0.0001);
 271         assertEquals(0.3, color.getOpacity(), 0.0001);
 272     }
 273 
 274     @Test
 275     public void testGrayRgb() {
 276         Color color = Color.grayRgb(255, 0.5);
 277         assertEquals(1.0, color.getRed(), 0.0001);
 278         assertEquals(1.0, color.getGreen(), 0.0001);
 279         assertEquals(1.0, color.getBlue(), 0.0001);
 280         assertEquals(0.5, color.getOpacity(), 0.0001);
 281     }
 282 
 283     @Test
 284     public void testHsbIsBounded() {
 285         Color.hsb(10000, 0, 0); // this should work!
 286     
 287         try {
 288             Color color = Color.hsb(100, -1, 0);
 289             assertTrue(false);
 290         } catch (Exception ee) {}
 291     
 292         try {
 293             Color color = Color.hsb(100, 0, -1);
 294             assertTrue(false);
 295         } catch (Exception eee) {}
 296     
 297         try {
 298             Color color = Color.hsb(100, 2, 0);
 299             assertTrue(false);
 300         } catch (Exception eeee) {}
 301     
 302         try {
 303             Color color = Color.hsb(100, 0, 2);
 304             assertTrue(false);
 305         } catch (Exception eeeee) {}
 306     }
 307     
 308     @Test
 309     public void testHsb() {
 310         Color color = Color.hsb(210, 1, .5);
 311         assertEquals(0.0, color.getRed(), .0001);
 312         assertEquals(0.25, color.getGreen(), .0001);
 313         assertEquals(0.5, color.getBlue(), .0001);
 314         assertEquals(1.0, color.getOpacity(), .0001);
 315         assertEquals(210, color.getHue(), 0.0001);
 316         assertEquals(1.0, color.getSaturation(), 0.0001);
 317         assertEquals(0.5, color.getBrightness(), 0.0001);
 318     }
 319 
 320     @Test
 321     public void testHsbWithOpacity() {
 322         Color color = Color.hsb(210, 1, .5, .4);
 323         assertEquals(0.0, color.getRed(), .0001);
 324         assertEquals(0.25, color.getGreen(), .0001);
 325         assertEquals(0.5, color.getBlue(), .0001);
 326         assertEquals(0.4, color.getOpacity(), .0001);
 327         assertEquals(210, color.getHue(), 0.0001);
 328         assertEquals(1.0, color.getSaturation(), 0.0001);
 329         assertEquals(0.5, color.getBrightness(), 0.0001);
 330     }
 331 
 332     @Test
 333     public void testWebPoundNotation() {
 334         Color color = Color.web("#aabbcc");
 335         assertEquals(color.getRed(), 170.0/255.0, 0.0001);
 336         assertEquals(color.getGreen(), 187.0/255.0, 0.0001);
 337         assertEquals(color.getBlue(), 204.0/255.0, 0.0001);
 338     }
 339 
 340     @Test
 341     public void testWebPoundNotationShort() {
 342         Color color = Color.web("#abc");
 343         assertEquals(color.getRed(), 10.0/15.0, 0.0001);
 344         assertEquals(color.getGreen(), 11.0/15.0, 0.0001);
 345         assertEquals(color.getBlue(), 12.0/15.0, 0.0001);
 346     }
 347 
 348     @Test
 349     public void testWebPoundNotationWithAlphaAndOpacity() {
 350         Color color = Color.web("#aabbcc80", 0.5);
 351         assertEquals(color.getRed(), 170.0/255.0, 0.0001);
 352         assertEquals(color.getGreen(), 187.0/255.0, 0.0001);
 353         assertEquals(color.getBlue(), 204.0/255.0, 0.0001);
 354         assertEquals((128.0/255.0)/2.0, color.getOpacity(), 0.0001);
 355     }
 356 
 357     @Test(expected=IllegalArgumentException.class)
 358     public void testWebPoundNotationIllegalValue() {
 359         Color.web("#aabbccddee");
 360     }
 361 
 362     @Test(expected=NullPointerException.class)
 363     public void testWebNullValue() {
 364         Color.web(null);
 365     }
 366 
 367     @Test(expected=IllegalArgumentException.class)
 368     public void testWebEmptyColor() {
 369         Color.web("", 0.5);
 370     }
 371     
 372     @Test
 373     public void testWebHexNotation() {
 374         Color color = Color.web("0xaabbcc");
 375         assertEquals(color.getRed(), 170.0/255.0, 0.0001);
 376         assertEquals(color.getGreen(), 187.0/255.0, 0.0001);
 377         assertEquals(color.getBlue(), 204.0/255.0, 0.0001);
 378     }
 379     
 380     @Test
 381     public void testWebHexNotationWithAlpha() {
 382         Color color = Color.web("0xaabbcc80");
 383         assertEquals(color.getRed(), 170.0/255.0, 0.0001);
 384         assertEquals(color.getGreen(), 187.0/255.0, 0.0001);
 385         assertEquals(color.getBlue(), 204.0/255.0, 0.0001);
 386         assertEquals(128.0/255.0, color.getOpacity(), 0.0001);
 387     }
 388 
 389     @Test(expected=IllegalArgumentException.class)
 390     public void testWebHexNotationIllegalValue() {
 391         Color.web("0xaabbccddee");
 392     }
 393     
 394     @Test
 395     public void testWebNamed() {
 396         Color color = Color.web("orangered");
 397         Color expected = Color.rgb(0xFF, 0x45, 0x00);
 398         assertTrue(expected.getRed() == color.getRed());
 399         assertTrue(expected.getGreen() == color.getGreen());
 400         assertTrue(expected.getBlue() == color.getBlue());
 401         assertTrue(expected.getOpacity() == color.getOpacity());
 402         double[] hsb = Utils.RGBtoHSB(1.0, 69.0/255.0, 0.0);
 403         assertEquals(hsb[0], color.getHue(), 0.001);
 404         assertEquals(hsb[1], color.getSaturation(), 0.001);
 405         assertEquals(hsb[2], color.getBrightness(), 0.001);
 406     }
 407     
 408     @Test
 409     public void testWebNamedWithOpacity() {
 410         Color color = Color.web("orangered", 0.4);
 411         Color expected = Color.rgb(0xFF, 0x45, 0x00, 0.4);
 412         assertTrue(expected.getRed() == color.getRed());
 413         assertTrue(expected.getGreen() == color.getGreen());
 414         assertTrue(expected.getBlue() == color.getBlue());
 415         assertTrue(expected.getOpacity() == color.getOpacity());
 416     }
 417 
 418     @Test
 419     public void testWebNamedMixedCase() {
 420         Color color = Color.web("oRAngEReD");
 421         Color expected = Color.rgb(0xFF, 0x45, 0x00);
 422         assertTrue(expected.getRed() == color.getRed());
 423         assertTrue(expected.getGreen() == color.getGreen());
 424         assertTrue(expected.getBlue() == color.getBlue());
 425         assertTrue(expected.getOpacity() == color.getOpacity());
 426     }
 427     
 428     @Test(expected=IllegalArgumentException.class)
 429     public void testWebNamedWrongName() {
 430         Color.web("foobar");
 431     }
 432 
 433     @Test
 434     public void testWebHex0xNotation() {
 435         Color color = Color.web("0xaabbcc");
 436         assertEquals(color.getRed(), 170.0/255.0, 0.0001);
 437         assertEquals(color.getGreen(), 187.0/255.0, 0.0001);
 438         assertEquals(color.getBlue(), 204.0/255.0, 0.0001);
 439     }
 440 
 441     @Test
 442     public void testWebHex0xNotationShort() {
 443         Color color = Color.web("0xabc");
 444         assertEquals(color.getRed(), 10.0/15.0, 0.0001);
 445         assertEquals(color.getGreen(), 11.0/15.0, 0.0001);
 446         assertEquals(color.getBlue(), 12.0/15.0, 0.0001);
 447     }
 448 
 449     @Test
 450     public void testWebHexNoLeadingSymbol() {
 451         Color color = Color.web("aAbBcC");
 452         assertEquals(color.getRed(), 170.0/255.0, 0.0001);
 453         assertEquals(color.getGreen(), 187.0/255.0, 0.0001);
 454         assertEquals(color.getBlue(), 204.0/255.0, 0.0001);
 455     }
 456 
 457     @Test
 458     public void testWebHexNoLeadingSymbolShort() {
 459         Color color = Color.web("aBc");
 460         assertEquals(color.getRed(), 10.0/15.0, 0.0001);
 461         assertEquals(color.getGreen(), 11.0/15.0, 0.0001);
 462         assertEquals(color.getBlue(), 12.0/15.0, 0.0001);
 463     }
 464 
 465     @Test
 466     public void testWebHexNoLeadingSymbolShortWithAlpha() {
 467         Color color = Color.web("aBc9");
 468         assertEquals(color.getRed(), 10.0/15.0, 0.0001);
 469         assertEquals(color.getGreen(), 11.0/15.0, 0.0001);
 470         assertEquals(color.getBlue(), 12.0/15.0, 0.0001);
 471         assertEquals(0.6, color.getOpacity(), 0.0001);
 472     }
 473 
 474     @Test
 475     public void testWebRgb() {
 476         Color color = Color.web("rgb(128, 64, 192)");
 477         assertEquals(128.0/255.0, color.getRed(), 0.0001);
 478         assertEquals( 64.0/255.0, color.getGreen(), 0.0001);
 479         assertEquals(192.0/255.0, color.getBlue(), 0.0001);
 480         assertEquals(1.0, color.getOpacity(), 0.0001);
 481     }
 482 
 483     @Test
 484     public void testWebRgbRange() {
 485         Color c1 = Color.web("rgb(255, 0, 255)");
 486         Color c2 = Color.web("rgb(256, -1, 256)");
 487         assertEquals(c1, c2);
 488         Color c3 = Color.web("rgb(0, 255, 0)");
 489         Color c4 = Color.web("rgb(-1, 256, -1)");
 490         assertEquals(c3, c4);
 491     }
 492 
 493     @Test
 494     public void testWebRgba() {
 495         Color color = Color.web("rgba(128, 64, 192, 0.6)");
 496         assertEquals(128.0/255.0, color.getRed(), 0.0001);
 497         assertEquals( 64.0/255.0, color.getGreen(), 0.0001);
 498         assertEquals(192.0/255.0, color.getBlue(), 0.0001);
 499         assertEquals(0.6, color.getOpacity(), 0.0001);
 500     }
 501 
 502     @Test
 503     public void testWebRgbaRange() {
 504         Color c1 = Color.web("rgba(255, 0, 255, 1.0)");
 505         Color c2 = Color.web("rgba(256, -1, 256, 1.1)");
 506         assertEquals(c1, c2);
 507         Color c3 = Color.web("rgba(0, 255, 0, 1.0)");
 508         Color c4 = Color.web("rgba(-1, 256, -1, 1.1)");
 509         assertEquals(c3, c4);
 510         Color c5 = Color.web("rgba(0, 0, 0, 0.0)");
 511         Color c6 = Color.web("rgba(0, 0, 0, -1)");
 512         assertEquals(c5, c6);
 513     }
 514 
 515     @Test
 516     public void testWebRgbPercent() {
 517         Color color = Color.web("rgb(60%, 40%, 100%)");
 518         assertEquals(0.6, color.getRed(), 0.0001);
 519         assertEquals(0.4, color.getGreen(), 0.0001);
 520         assertEquals(1.0, color.getBlue(), 0.0001);
 521         assertEquals(1.0, color.getOpacity(), 0.0001);
 522     }
 523 
 524     @Test
 525     public void testWebRgbPercentFloat() {
 526         Color color = Color.web("rgb(60.0%, 40.0%, 100.0%)");
 527         assertEquals(0.6, color.getRed(), 0.0001);
 528         assertEquals(0.4, color.getGreen(), 0.0001);
 529         assertEquals(1.0, color.getBlue(), 0.0001);
 530         assertEquals(1.0, color.getOpacity(), 0.0001);
 531     }
 532 
 533     @Test
 534     public void testWebRgbPercentRange() {
 535         Color c1 = Color.web("rgb(100%,  0%, 100%)");
 536         Color c2 = Color.web("rgb(101%, -1%, 101%)");
 537         assertEquals(c1, c2);
 538         Color c3 = Color.web("rgb( 0%, 100%,  0%)");
 539         Color c4 = Color.web("rgb(-1%, 101%, -1%)");
 540         assertEquals(c3, c4);
 541     }
 542 
 543     @Test
 544     public void testWebRgbaPercent() {
 545         Color color = Color.web("rgba(60%, 40%, 100%, 0.6)");
 546         assertEquals(0.6, color.getRed(), 0.0001);
 547         assertEquals(0.4, color.getGreen(), 0.0001);
 548         assertEquals(1.0, color.getBlue(), 0.0001);
 549         assertEquals(0.6, color.getOpacity(), 0.0001);
 550     }
 551     
 552     @Test
 553     public void testWebRgbaPercentFloat() {
 554         Color color = Color.web("rgba(60.0%, 40.0%, 100.0%, 0.6)");
 555         assertEquals(0.6, color.getRed(), 0.0001);
 556         assertEquals(0.4, color.getGreen(), 0.0001);
 557         assertEquals(1.0, color.getBlue(), 0.0001);
 558         assertEquals(0.6, color.getOpacity(), 0.0001);
 559     }
 560 
 561     @Test
 562     public void testWebRgbaPercentRange() {
 563         Color c1 = Color.web("rgba(100%,  0%, 100%, 1.0)");
 564         Color c2 = Color.web("rgba(101%, -1%, 101%, 1.1)");
 565         assertEquals(c1, c2);
 566         Color c3 = Color.web("rgba( 0%, 100%,  0%, 1.0)");
 567         Color c4 = Color.web("rgba(-1%, 101%, -1%, 1.1)");
 568         assertEquals(c3, c4);
 569         Color c5 = Color.web("rgba(0%, 0%, 0%, 0.0)");
 570         Color c6 = Color.web("rgba(0%, 0%, 0%, -1)");
 571         assertEquals(c5, c6);
 572     }
 573 
 574     @Test
 575     public void testWebRgbPercentMix() {
 576         Color color = Color.web("rgb(60%, 40.0%, 192)");
 577         assertEquals(0.6, color.getRed(), 0.0001);
 578         assertEquals(0.4, color.getGreen(), 0.0001);
 579         assertEquals(192.0/255.0, color.getBlue(), 0.0001);
 580         assertEquals(1.0, color.getOpacity(), 0.0001);
 581     }
 582 
 583     @Test
 584     public void testWebRgbaPercentMix() {
 585         Color color = Color.web("rgba(60%, 40.0%, 192, 0.6)");
 586         assertEquals(0.6, color.getRed(), 0.0001);
 587         assertEquals(0.4, color.getGreen(), 0.0001);
 588         assertEquals(192.0/255.0, color.getBlue(), 0.0001);
 589         assertEquals(0.6, color.getOpacity(), 0.0001);
 590     }
 591 
 592     @Test
 593     public void testWebHsl() {
 594         Color color = Color.web("hsl(180, 50%, 100%)");
 595         Color ref = Color.hsb(180, 0.5, 1.0);
 596         assertEquals(ref.getRed(),     color.getRed(),     0.0001);
 597         assertEquals(ref.getGreen(),   color.getGreen(),   0.0001);
 598         assertEquals(ref.getBlue(),    color.getBlue(),    0.0001);
 599         assertEquals(ref.getOpacity(), color.getOpacity(), 0.0001);
 600     }
 601 
 602     @Test
 603     public void testWebHslFloat() {
 604         Color color = Color.web("hsl(180.0, 50.0%, 100.0%)");
 605         Color ref = Color.hsb(180, 0.5, 1.0);
 606         assertEquals(ref.getRed(),     color.getRed(),     0.0001);
 607         assertEquals(ref.getGreen(),   color.getGreen(),   0.0001);
 608         assertEquals(ref.getBlue(),    color.getBlue(),    0.0001);
 609         assertEquals(ref.getOpacity(), color.getOpacity(), 0.0001);
 610     }
 611 
 612     @Test
 613     public void testWebHslRange() {
 614         Color c1 = Color.web("hsl( 240,  0%, 100%)");
 615         Color c2 = Color.web("hsl(-120, -1%, 101%)");
 616         assertEquals(c1, c2);
 617         Color c3 = Color.web("hsl(240, 100%,  0%)");
 618         Color c4 = Color.web("hsl(600, 101%, -1%)");
 619         assertEquals(c3, c4);
 620     }
 621 
 622     @Test
 623     public void testWebHsla() {
 624         Color color = Color.web("hsla(180, 50%, 100%, 0.6)");
 625         Color ref = Color.hsb(180, 0.5, 1.0, 0.6);
 626         assertEquals(ref.getRed(),     color.getRed(),     0.0001);
 627         assertEquals(ref.getGreen(),   color.getGreen(),   0.0001);
 628         assertEquals(ref.getBlue(),    color.getBlue(),    0.0001);
 629         assertEquals(ref.getOpacity(), color.getOpacity(), 0.0001);
 630     }
 631 
 632     @Test
 633     public void testWebHslaFloat() {
 634         Color color = Color.web("hsla(180.0, 50.0%, 100.0%, 0.6)");
 635         Color ref = Color.hsb(180, 0.5, 1.0, 0.6);
 636         assertEquals(ref.getRed(),     color.getRed(),     0.0001);
 637         assertEquals(ref.getGreen(),   color.getGreen(),   0.0001);
 638         assertEquals(ref.getBlue(),    color.getBlue(),    0.0001);
 639         assertEquals(ref.getOpacity(), color.getOpacity(), 0.0001);
 640     }
 641 
 642     @Test
 643     public void testWebHslaRange() {
 644         Color c1 = Color.web("hsla( 240,  0%, 100%, 1.0)");
 645         Color c2 = Color.web("hsla(-120, -1%, 101%, 1.1)");
 646         assertEquals(c1, c2);
 647         Color c3 = Color.web("hsla(240, 100%,  0%, 1.0)");
 648         Color c4 = Color.web("hsla(600, 101%, -1%, 1.1)");
 649         assertEquals(c3, c4);
 650         Color c5 = Color.web("hsla(240, 0%, 0%, 0.0)");
 651         Color c6 = Color.web("hsla(240, 0%, 0%, -1)");
 652         assertEquals(c5, c6);
 653     }
 654 
 655     @Test(expected=IllegalArgumentException.class)
 656     public void testWebRgb2Param() {
 657         Color.web("rgb(100, 100)");
 658     }
 659 
 660     @Test(expected=IllegalArgumentException.class)
 661     public void testWebRgb1Param() {
 662         Color.web("rgb(100)");
 663     }
 664 
 665     @Test(expected=IllegalArgumentException.class)
 666     public void testWebRgb0Param() {
 667         Color.web("rgb()");
 668     }
 669 
 670     @Test(expected=IllegalArgumentException.class)
 671     public void testWebRgbNoParen() {
 672         Color.web("rgb 100, 100, 100");
 673     }
 674 
 675     @Test(expected=IllegalArgumentException.class)
 676     public void testWebRgbNoCloseParen() {
 677         Color.web("rgb(100, 100, 100");
 678     }
 679 
 680     @Test(expected=IllegalArgumentException.class)
 681     public void testWebRgba3Param() {
 682         Color.web("rgba(100, 100, 100)");
 683     }
 684 
 685     @Test(expected=IllegalArgumentException.class)
 686     public void testWebRgba2Param() {
 687         Color.web("rgba(100, 100)");
 688     }
 689 
 690     @Test(expected=IllegalArgumentException.class)
 691     public void testWebRgba1Param() {
 692         Color.web("rgba(100)");
 693     }
 694 
 695     @Test(expected=IllegalArgumentException.class)
 696     public void testWebRgba0Param() {
 697         Color.web("rgba()");
 698     }
 699 
 700     @Test(expected=IllegalArgumentException.class)
 701     public void testWebRgbaNoParen() {
 702         Color.web("rgba 100, 100, 100");
 703     }
 704 
 705     @Test(expected=IllegalArgumentException.class)
 706     public void testWebRgbaNoCloseParen() {
 707         Color.web("rgba(100, 100, 100, 0.5");
 708     }
 709 
 710     @Test(expected=IllegalArgumentException.class)
 711     public void testWebHsl2Param() {
 712         Color.web("hsl(240, 50%)");
 713     }
 714 
 715     @Test(expected=IllegalArgumentException.class)
 716     public void testWebHsl1Param() {
 717         Color.web("hsl(240)");
 718     }
 719 
 720     @Test(expected=IllegalArgumentException.class)
 721     public void testWebHsl0Param() {
 722         Color.web("hsl()");
 723     }
 724 
 725     @Test(expected=IllegalArgumentException.class)
 726     public void testWebHslNoParen() {
 727         Color.web("hsl 240, 50%, 50%");
 728     }
 729 
 730     @Test(expected=IllegalArgumentException.class)
 731     public void testWebHslNoCloseParen() {
 732         Color.web("hsl(240, 50%, 50%");
 733     }
 734 
 735     @Test(expected=IllegalArgumentException.class)
 736     public void testWebHsla3Param() {
 737         Color.web("hsla(240, 50%, 50%)");
 738     }
 739 
 740     @Test(expected=IllegalArgumentException.class)
 741     public void testWebHsla2Param() {
 742         Color.web("hsla(240, 50%)");
 743     }
 744 
 745     @Test(expected=IllegalArgumentException.class)
 746     public void testWebHsla1Param() {
 747         Color.web("hsla(240)");
 748     }
 749 
 750     @Test(expected=IllegalArgumentException.class)
 751     public void testWebHsla0Param() {
 752         Color.web("hsla()");
 753     }
 754 
 755     @Test(expected=IllegalArgumentException.class)
 756     public void testWebHslaNoParen() {
 757         Color.web("hsla 240, 50%, 50%, 0.5");
 758     }
 759 
 760     @Test(expected=IllegalArgumentException.class)
 761     public void testWebHslaNoCloseParen() {
 762         Color.web("hsla(240, 50%, 50%, 0.5");
 763     }
 764 
 765     @Test
 766     public void testDeriveColor() {
 767         Color original = Color.hsb(180, 0.4, 0.8, 0.5);
 768         Color color = original.deriveColor(-90, 2, 0.5, 2);
 769         assertEquals(90, color.getHue(), 0.0001);
 770         assertEquals(0.8, color.getSaturation(), 0.0001);
 771         assertEquals(0.4, color.getBrightness(), 0.0001);
 772         assertEquals(1.0, color.getOpacity(), 0.0001);
 773     }
 774 
 775     @Test
 776     public void testDeriveColorFromRgb() {
 777         Color original = Color.rgb(128, 0, 255);
 778         double[] hsb = Utils.RGBtoHSB(128.0/255.0, 0.0, 1.0);
 779         Color color = original.deriveColor(-30, 0.5, 0.5, 0.5);
 780         assertEquals((hsb[0] - 30), color.getHue(), 0.0001);
 781         assertEquals(hsb[1] / 2, color.getSaturation(), 0.0001);
 782         assertEquals(hsb[2] / 2, color.getBrightness(), 0.0001);
 783         assertEquals(0.5, color.getOpacity(), 0.0001);
 784     }
 785 
 786     @Test
 787     public void testDeriveColorClipS() {
 788         Color original = Color.hsb(180, 0.4, 0.8, 0.5);
 789         Color color = original.deriveColor(-1170, -5, 20, -5);
 790         assertEquals(0, color.getHue(), 0.0001);
 791         assertEquals(0.0, color.getSaturation(), 0.0001);
 792         assertEquals(1.0, color.getBrightness(), 0.0001);
 793         assertEquals(0.0, color.getOpacity(), 0.0001);
 794     }
 795 
 796     @Test
 797     public void testDeriveColorClipHB() {
 798         Color original = Color.hsb(180, 0.4, 0.8, 0.5);
 799         Color color = original.deriveColor(-1170, 1.0, 20, -5);
 800         assertEquals(90, color.getHue(), 0.0001);
 801         assertEquals(0.4, color.getSaturation(), 0.0001);
 802         assertEquals(1.0, color.getBrightness(), 0.0001);
 803         assertEquals(0.0, color.getOpacity(), 0.0001);
 804     }
 805 
 806     @Test
 807     public void testDarker() {
 808         Color original = Color.hsb(180, 0.4, 0.8, 0.5);
 809         Color color = original.darker();
 810         assertEquals(180, color.getHue(), 0.0001);
 811         assertEquals(0.4, color.getSaturation(), 0.0001);
 812         assertEquals(0.56, color.getBrightness(), 0.0001);
 813         assertEquals(0.5, color.getOpacity(), 0.0001);
 814     }
 815 
 816     @Test
 817     public void testBrighter() {
 818         Color original = Color.hsb(180, 0.4, 0.4, 0.5);
 819         Color color = original.brighter();
 820         assertEquals(180, color.getHue(), 0.0001);
 821         assertEquals(0.4, color.getSaturation(), 0.0001);
 822         assertEquals(0.5714, color.getBrightness(), 0.0001);
 823         assertEquals(0.5, color.getOpacity(), 0.0001);
 824     }
 825 
 826     @Test
 827     public void testBlackBrighter() {
 828         Color color = Color.BLACK.brighter();
 829         assertTrue(color.getBrightness() > 0.0);
 830         assertEquals(color.getRed(), color.getGreen(), 0.0001);
 831         assertEquals(color.getRed(), color.getBlue(), 0.0001);
 832     }
 833 
 834     @Test
 835     public void testSaturate() {
 836         Color original = Color.hsb(180, 0.4, 0.4, 0.5);
 837         Color color = original.saturate();
 838         assertEquals(180, color.getHue(), 0.0001);
 839         assertEquals(0.5714, color.getSaturation(), 0.0001);
 840         assertEquals(0.4, color.getBrightness(), 0.0001);
 841         assertEquals(0.5, color.getOpacity(), 0.0001);
 842     }
 843 
 844     @Test
 845     public void testDesaturate() {
 846         Color original = Color.hsb(180, 0.8, 0.4, 0.5);
 847         Color color = original.desaturate();
 848         assertEquals(180, color.getHue(), 0.0001);
 849         assertEquals(0.56, color.getSaturation(), 0.0001);
 850         assertEquals(0.4, color.getBrightness(), 0.0001);
 851         assertEquals(0.5, color.getOpacity(), 0.0001);
 852     }
 853 
 854     @Test
 855     public void testInvert() {
 856         Color original = Color.color(0.2, 0.3, 0.4, 0.6);
 857         Color color = original.invert();
 858         assertEquals(0.8, color.getRed(), 0.0001);
 859         assertEquals(0.7, color.getGreen(), 0.0001);
 860         assertEquals(0.6, color.getBlue(), 0.0001);
 861         assertEquals(0.6, color.getOpacity(), 0.0001);
 862     }
 863 
 864     @Test
 865     public void testGreyscale() {
 866         Color original = Color.color(0.2, 0.3, 0.4, 0.6);
 867         Color color = original.grayscale();
 868         assertEquals(0.283, color.getRed(), 0.0001);
 869         assertEquals(0.283, color.getGreen(), 0.0001);
 870         assertEquals(0.283, color.getBlue(), 0.0001);
 871         assertEquals(0.6, color.getOpacity(), 0.0001);
 872     }
 873 
 874     @Test
 875     public void testEquals() {
 876         Color basic = Color.rgb(0, 0, 0, 0.5);
 877         Color equal = Color.rgb(0, 0, 0, 0.5);
 878         Color color1 = Color.rgb(0xAA, 0, 0, 0.5);
 879         Color color2 = Color.rgb(0, 0xAA, 0, 0.5);
 880         Color color3 = Color.rgb(0, 0, 0xAA, 0.5);
 881         Color color4 = Color.rgb(0, 0, 0, 0.6);
 882 
 883         assertFalse(basic.equals(null));
 884         assertFalse(basic.equals(new Object()));
 885         assertTrue(basic.equals(basic));
 886         assertTrue(basic.equals(equal));
 887         assertFalse(basic.equals(color1));
 888         assertFalse(basic.equals(color2));
 889         assertFalse(basic.equals(color3));
 890         assertFalse(basic.equals(color4));
 891     }
 892 
 893     @Test
 894     public void testHashCode() {
 895         Color basic = Color.rgb(0, 0, 0, 0.5);
 896         Color equal = Color.rgb(0, 0, 0, 0.5);
 897         Color diffColor = Color.rgb(0, 0xAA, 0, 0.5);
 898         Color diffOpacity = Color.rgb(0, 0, 0, 0.7);
 899         Color transparent = Color.rgb(0, 0, 0, 0.0);
 900 
 901         int code = basic.hashCode();
 902         int second = basic.hashCode();
 903         assertTrue(code == second);
 904         assertTrue(code == equal.hashCode());
 905         assertFalse(code == diffColor.hashCode());
 906         assertFalse(code == diffOpacity.hashCode());
 907         assertEquals(0, Color.TRANSPARENT.hashCode());
 908         assertEquals(0, transparent.hashCode());
 909     }
 910 
 911     @Test
 912     public void testToString() {
 913         Color color = Color.rgb(0, 0, 0, 0.0);
 914 
 915         String s = color.toString();
 916         assertNotNull(s);
 917         assertFalse(s.isEmpty());
 918     }
 919 
 920     @Test
 921     public void testToStringEquals() {
 922         Color color = Color.web("#aabbcc");
 923         assertEquals(color, Color.valueOf(color.toString()));
 924         color = Color.web("#abc");
 925         assertEquals(color, Color.valueOf(color.toString()));
 926         color = Color.web("#aabbcc80", 0.5);
 927         assertEquals(color, Color.valueOf(color.toString()));
 928         color = Color.web("0xaabbcc");
 929         assertEquals(color, Color.valueOf(color.toString()));
 930         color = Color.web("0xaabbcc80");
 931         assertEquals(color, Color.valueOf(color.toString()));
 932         color = Color.web("orangered");
 933         assertEquals(color, Color.valueOf(color.toString()));
 934         color = Color.web("orangered", 0.4);
 935         assertEquals(color, Color.valueOf(color.toString()));
 936         color = Color.web("oRAngEReD");
 937         assertEquals(color, Color.valueOf(color.toString()));
 938         color = Color.web("0xaabbcc");
 939         assertEquals(color, Color.valueOf(color.toString()));
 940         color = Color.web("0xabc");
 941         assertEquals(color, Color.valueOf(color.toString()));
 942         color = Color.web("aAbBcC");
 943         assertEquals(color, Color.valueOf(color.toString()));
 944         color = Color.web("aBc");
 945         assertEquals(color, Color.valueOf(color.toString()));
 946         color = Color.web("aBc9");
 947         assertEquals(color, Color.valueOf(color.toString()));
 948     }
 949 
 950     //function testOfTheWayHandlesNegatives() {
 951         // TODO should this be tested? What does it mean?
 952     //}
 953     
 954     //function testOfTheWayHandlesLargeNumbers() {
 955         // TODO What should happen for numbers > 1?
 956     //}
 957 }