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