/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. 
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Sun Microsystems nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * ColorPicker.fx
 *
 * Created on Jan 9, 2009, 11:21:30 AM
 */

package sketch;

import java.awt.image.BufferedImage;
import javafx.ext.swing.SwingUtils;
import javafx.scene.CustomNode;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;
import javafx.scene.paint.Color;


/**
 * @author jp202575
 */

public class ColorPicker extends CustomNode{

    var img:BufferedImage;
    var iv:ImageView = ImageView{
        onMouseClicked: function(e:MouseEvent) {
            if (img!=null) {
                var c:java.awt.Color = new java.awt.Color(img.getRGB(e.x,e.y));
                color = Color.rgb(c.getRed(), c.getGreen(), c.getBlue());
            }
        }
        onMouseMoved: function(e:MouseEvent) {
            if (img!=null) {
                var c:java.awt.Color = new java.awt.Color(img.getRGB(e.x,e.y));
                previewColor = Color.rgb(c.getRed(), c.getGreen(), c.getBlue());
            }
        }
        onMouseDragged: function(e:MouseEvent) {
            if (img!=null) {
                var c:java.awt.Color = new java.awt.Color(img.getRGB(e.x,e.y));
                color = Color.rgb(c.getRed(), c.getGreen(), c.getBlue());
            }
        }
        onMouseExited: function(e:MouseEvent) {
            previewColor = null;
        }
    };

    public-init var width:Integer;
    public-init var height:Integer;
    public-read var color:Color = Color.RED;
    public-read var previewColor:Color;

    public override function create():Node {
        img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        var halfW:Number = width/2.0;
        var vSpacing:Number = 1.0/height;
        var hSpacing:Number = 1.0/halfW;
        println("vSpacing={vSpacing} hSpacing={hSpacing}");
        for (y in [0..(height-1)], x in [0..(halfW-1)]) {
            img.setRGB(x,y,java.awt.Color.HSBtoRGB(new java.lang.Float(vSpacing*y), 1, new java.lang.Float(hSpacing*x)));
        }
        for (y in [0..(height-1)], x in [halfW..(width-1)]) {
            img.setRGB(x,y,java.awt.Color.HSBtoRGB(new java.lang.Float(vSpacing*y), new java.lang.Float(1-(hSpacing*(x-halfW))), 1));
        }
        iv.image = SwingUtils.toFXImage(img);
        return iv;
    }
}