1 /*
   2  * Copyright (c) 2014, 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 package javafx.scene.control.test.utils;
  26 
  27 import javafx.application.Application;
  28 import javafx.event.ActionEvent;
  29 import javafx.event.EventHandler;
  30 import javafx.scene.Scene;
  31 import javafx.scene.control.Button;
  32 import javafx.scene.control.TextArea;
  33 import javafx.scene.control.TextField;
  34 import javafx.scene.layout.VBox;
  35 import javafx.scene.paint.Color;
  36 import javafx.stage.Stage;
  37 import javafx.stage.StageStyle;
  38 import org.jemmy.action.GetAction;
  39 import org.jemmy.fx.Root;
  40 
  41 /**
  42  * @author Alexander Kirov
  43  *
  44  * This helper helps to determine, what say awt and glass robots about color at
  45  * some coordinate. Enter coordinates. X and Y of stage will be set at that
  46  * position, and you can see the result of robot calls.
  47  */
  48 public class ColorHelper extends Application {
  49 
  50     public static void main(String[] args) {
  51         launch(args);
  52     }
  53 
  54     @Override
  55     public void start(final Stage stage) throws Exception {
  56         final TextField xField = new TextField();
  57         xField.setPromptText("x");
  58         final TextField yField = new TextField();
  59         yField.setPromptText("y");
  60 
  61         final TextArea awtField = new TextArea();
  62         awtField.setPromptText("awt");
  63         final TextArea glassField = new TextArea();
  64         glassField.setPromptText("glass");
  65 
  66         Button act = new Button("Get colors");
  67         act.setOnAction(new EventHandler<ActionEvent>() {
  68             public void handle(ActionEvent t) {
  69                 try {
  70                     int x = Integer.parseInt(xField.getText());
  71                     int y = Integer.parseInt(yField.getText());
  72 
  73                     stage.setX(x + 1);
  74                     stage.setY(y + 1);
  75 
  76                     java.awt.Robot robotAwt = new java.awt.Robot();
  77                     com.sun.glass.ui.Robot robotGlass = new GetAction<com.sun.glass.ui.Robot>() {
  78                         @Override
  79                         public void run(Object... os) throws Exception {
  80                             setResult(com.sun.glass.ui.Application.GetApplication().createRobot());
  81                         }
  82                     }.dispatch(Root.ROOT.getEnvironment());
  83 
  84                     java.awt.Color glassColor = new java.awt.Color(robotGlass.getPixelColor((int) Math.round(x), (int) Math.round(y)));
  85                     java.awt.Color awtColor = robotAwt.getPixelColor((int) Math.round(x), (int) Math.round(y));
  86 
  87                     awtField.setText("AWT robot " + getColorDescription(awtColor));
  88                     glassField.setText("Glass robot " + getColorDescription(glassColor));
  89                 } catch (Throwable ex) {
  90                     ex.printStackTrace();
  91                 }
  92             }
  93         });
  94 
  95         stage.initStyle(StageStyle.UNDECORATED);
  96         stage.setScene(new Scene(new VBox(5, xField, yField, act, awtField, glassField)));
  97         stage.show();
  98     }
  99 
 100     static public String getColorDescription(java.awt.Color colorAwt) {
 101         Color color = AWTtoFXcolorConvert(colorAwt);
 102         StringBuilder b = new StringBuilder("Color : \n");
 103 
 104         b.append(" R : ").append(color.getRed() * 255).append(" G : ").append(color.getGreen() * 255).append(" B : ").append(color.getBlue() * 255).append(";\n");
 105         b.append(" H : ").append(color.getHue() * 360).append(" S : ").append(color.getSaturation() * 100).append(" B : ").append(color.getBrightness() * 100).append(";\n");
 106         b.append(" W : ").append(color.toString()).append(".\n");
 107 
 108         return b.toString();
 109     }
 110 
 111     static public Color AWTtoFXcolorConvert(java.awt.Color col) {
 112         return new Color(col.getRed() / 255.0, col.getGreen() / 255.0, col.getBlue() / 255.0, col.getAlpha() / 255.0);
 113     }
 114 }