I was on a roll with adding literals to the compiler, so why not one
more...
Color literals let you specify colors in your application using the
common hex notation. For example you can define color constants:
public def DODGERBLUE = #1E90FF;
Or use colors in object literals:
Rectangle {
color: #CCCCCC // gray
}
You can also specify the alpha/opacity value by using a pipe
delimited value:
#0000FF|88 // transparent blue
The shorthand form of the above is also supported (digits are
repeated to make the full color value):
#CCC == #CCCCCC
#00F|8 == #OOOOFF|88
Operator overloading of colors is also supported, allowing color
arithmetic. For example, you can blend colors by doing a
mathematical average:
(Color.RED + Color.BLUE) / 2 // makes purple
or even three or more colors:
(Color.WHITE + Color.GREEN + Color.RED) / 3 // light orange
You can also lighten or darken colors simply by multiplying or
dividing by constants:
Color.BROWN * 1.2 // light brown
Color.RED / 1.2 // dark brown
All of this is backed by a new Color class that is original code,
but ended up having a very similar API to the JavaFX Color class.
Like JavaFX, the Visage Color class is an immutable class with an
sRGB color tuple + transparency. The same SVG 1.0 set of standard
color names is used. The major differences are as follows:
- I added support for an HSL color model (which is part of the
proposed CSS3 standard) in addition to the HSB/HSV color model
that JavaFX uses.
- sRGB ranges are relaxed to allow arbitrary values during
mathematical operations and storage of scRGB values
- I didn't bother implementing the named color lookup capability
(does anyone really need/use this?)
- I added Java Formatter support with a default output format of
color(r%, g%, b%, a%?) and an alternative format of #RRGGBB|AA?
I did consider implementing some more complex color space
algorithms, such as cielab, cieluv, XYZ, or something else exotic.
The advantage of going down this route would be more accurate
construction of perceptually related colors. However, there is a
relatively steep learning curve for end users to numerically
construct colors in these spaces. This could always be added in the
future and mapped onto the scRGB color space if there is a need.
If you are interested in doing a code review of my changes, here is
the changelist:
http://code.google.com/p/visage/source/detail?r=30fbcab10176b3167a3d6c257ae21054f2322f42
Cheers,