And Finally Color Literals...

14 views
Skip to first unread message

Stephen Chin

unread,
Jan 3, 2011, 8:56:29 AM1/3/11
to visag...@googlegroups.com
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,
--
--Steve
blog: http://steveonjava.com/

James Weaver

unread,
Jan 3, 2011, 9:40:16 AM1/3/11
to visag...@googlegroups.com
Nice work, and thanks Stephen!

Jim Weaver

Stephen Chin

unread,
Jan 3, 2011, 4:58:26 PM1/3/11
to visag...@googlegroups.com
I was asleep at the wheel (literally), and only checked in one file last night.  :(

Here is the rest of the implementation:
http://code.google.com/p/visage/source/detail?r=20e6ed44a8e2a614a6eaf0b8ff2b8e62b6bfe159

--Steve

Peter Pilgrim

unread,
Jan 4, 2011, 9:00:35 AM1/4/11
to visag...@googlegroups.com
Really great and interesting work. Visagé lives onwards
--
Peter Pilgrim,
**Java Champion**,
JavaFX / Java EE Software Development / Design / Architect
Certified SCRUM MASTER, Scala Enthusiast

++++++++++++++++++++++++++++++++++++++
+++     HAPPY NEW YEAR 2011        +++
+++   ( Beyond Java on the JVM )   +++
++++++++++++++++++++++++++++++++++++++

:: http://www.xenonique.co.uk/blog  ::
:: http://twitter.com/peter_pilgrim ::
:: https://java-champions.dev.java.net/ ::
:: http://audio.fm/profile/peter_pilgrim  ::

::  A Oracle Certified Enterprise Architect for Java EE 5 platform ::

Alain Béarez

unread,
Feb 16, 2011, 10:23:32 AM2/16/11
to visag...@googlegroups.com
I was trying to port my demo app from JavaFX 1.3.1 to Visage but I got stuck on the first Rectangle:

                    Rectangle {
                        height: 400
                        width: Math.sqrt(3) * RADIUS
                        strokeWidth: 3
                        stroke: Color.DARKRED
                        fill: Color.TRANSPARENT
                    }
 
where the compiler reports the following errors:

C:\Users\Bearez_Alain\Documents\NetBeansProjects\TI\src\cua\li\ti\Demo.fx:13: javafx.lang.Color is already defined in a single-type import
import javafx.scene.paint.Color;
C:\Users\Bearez_Alain\Documents\NetBeansProjects\TI\src\cua\li\ti\Demo.fx:37: incompatible types
found   : javafx.lang.Color
required: javafx.scene.paint.Paint
                        stroke: Color.DARKRED
C:\Users\Bearez_Alain\Documents\NetBeansProjects\TI\src\cua\li\ti\Demo.fx:38: incompatible types
found   : javafx.lang.Color
required: javafx.scene.paint.Paint
                        fill: Color.TRANSPARENT

The first one is easily resolved by removing the import statement. However the incompatibility of the types is persisting.

In order to prepare my Visage SDK, I installed a JavaFX 1.3.1 SDK, then downloaded and build the Visage compiler from the source repository, and finally followed the instructions from the wiki about InstallingPreviewBuilds copying the generated dist/lib/ jars into the corresponding JavaFX-SDK lib/ subdirectories.

What would the correct approach be? That is... apart from explicitly stating the javax.scene.paint package for the Color class.

                        fill: javafx.scene.paint.Color.TRANSPARENT

I am really interested in turning my color picker component into a Visage component. Using both default properties and color literals could reduce the noise level in my code:


thx in advance,
-- 
Alain

Reply all
Reply to author
Forward
0 new messages