public ColorPickerPopover() { getStyleClass().add("color-picker-popover"); popup.setAutoHide(true); // add this to popup popup.getContent().add(this); // load stylesheet getStylesheets().add(ColorPicker.class.getResource("ColorPicker.css").toString()); // create popover path for main shape final Path p = new Path(); p.getElements() .addAll( new MoveTo(PICKER_PADDING, PICKER_PADDING + ARROW_SIZE + RADIUS), new ArcTo( RADIUS, RADIUS, 90, PICKER_PADDING + RADIUS, PICKER_PADDING + ARROW_SIZE, false, true), new LineTo(PICKER_PADDING + ARROW_X - (ARROW_SIZE * 0.8), PICKER_PADDING + ARROW_SIZE), new LineTo(PICKER_PADDING + ARROW_X, PICKER_PADDING), new LineTo(PICKER_PADDING + ARROW_X + (ARROW_SIZE * 0.8), PICKER_PADDING + ARROW_SIZE), new LineTo(PICKER_PADDING + PICKER_WIDTH - RADIUS, PICKER_PADDING + ARROW_SIZE), new ArcTo( RADIUS, RADIUS, 90, PICKER_PADDING + PICKER_WIDTH, PICKER_PADDING + ARROW_SIZE + RADIUS, false, true), new LineTo( PICKER_PADDING + PICKER_WIDTH, PICKER_PADDING + ARROW_SIZE + PICKER_HEIGHT - RADIUS), new ArcTo( RADIUS, RADIUS, 90, PICKER_PADDING + PICKER_WIDTH - RADIUS, PICKER_PADDING + ARROW_SIZE + PICKER_HEIGHT, false, true), new LineTo(PICKER_PADDING + RADIUS, PICKER_PADDING + ARROW_SIZE + PICKER_HEIGHT), new ArcTo( RADIUS, RADIUS, 90, PICKER_PADDING, PICKER_PADDING + ARROW_SIZE + PICKER_HEIGHT - RADIUS, false, true), new ClosePath()); p.setFill( new LinearGradient( 0, 0, 0, 1, true, CycleMethod.NO_CYCLE, new Stop(0, Color.web("#313131")), new Stop(0.5, Color.web("#5f5f5f")), new Stop(1, Color.web("#313131")))); p.setStroke(null); p.setEffect(new DropShadow(15, 0, 1, Color.gray(0, 0.6))); p.setCache(true); // create rectangle to capture mouse events to hide Rectangle windowClickRect = RectangleBuilder.create() .width(PICKER_PADDING + PICKER_WIDTH + PICKER_PADDING) .height(PICKER_PADDING + PICKER_HEIGHT + PICKER_PADDING) .fill(Color.TRANSPARENT) .onMouseClicked( new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { System.out.println("x= " + event.getX()); System.out.println( "p.contains(event.getX(), event.getY()) = " + p.contains(event.getX(), event.getY())); if (!p.contains(event.getX(), event.getY())) { hide(); } } }) .build(); final Circle colorRectIndicator = CircleBuilder.create() .centerX(60) .centerY(60) .radius(5) .stroke(Color.WHITE) .fill(null) .effect(new DropShadow(2, 0, 1, Color.BLACK)) .build(); colorRectIndicator .centerXProperty() .bind( new DoubleBinding() { { bind(sat); } @Override protected double computeValue() { return (PICKER_PADDING + 10) + (RECT_SIZE * (sat.get() / 100)); } }); colorRectIndicator .centerYProperty() .bind( new DoubleBinding() { { bind(bright); } @Override protected double computeValue() { return (PICKER_PADDING + ARROW_SIZE + 10) + (RECT_SIZE * (1 - (bright.get() / 100))); } }); final Rectangle colorRect = RectangleBuilder.create() .x(PICKER_PADDING + 10) .y(PICKER_PADDING + ARROW_SIZE + 10) .width(RECT_SIZE) .height(RECT_SIZE) .build(); colorRect .fillProperty() .bind( new ObjectBinding<Paint>() { { bind(color); } @Override protected Paint computeValue() { return Color.hsb(hue.getValue(), 1, 1); } }); Rectangle colorRectOverlayOne = RectangleBuilder.create() .x(PICKER_PADDING + 10) .y(PICKER_PADDING + ARROW_SIZE + 10) .width(RECT_SIZE) .height(RECT_SIZE) .fill( new LinearGradient( 0, 0, 1, 0, true, CycleMethod.NO_CYCLE, new Stop(0, Color.rgb(255, 255, 255, 1)), new Stop(1, Color.rgb(255, 255, 255, 0)))) .build(); EventHandler<MouseEvent> rectMouseHandler = new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { final double x = event.getX() - colorRect.getX(); final double y = event.getY() - colorRect.getY(); sat.set(clamp(x / RECT_SIZE) * 100); bright.set(100 - (clamp(y / RECT_SIZE) * 100)); } }; Rectangle colorRectOverlayTwo = RectangleBuilder.create() .x(PICKER_PADDING + 10) .y(PICKER_PADDING + ARROW_SIZE + 10) .width(RECT_SIZE) .height(RECT_SIZE) .fill( new LinearGradient( 0, 0, 0, 1, true, CycleMethod.NO_CYCLE, new Stop(0, Color.rgb(0, 0, 0, 0)), new Stop(1, Color.rgb(0, 0, 0, 1)))) .onMouseDragged(rectMouseHandler) .onMouseClicked(rectMouseHandler) .build(); final Rectangle colorBar = RectangleBuilder.create() .x(PICKER_PADDING + PICKER_WIDTH - 30) .y(PICKER_PADDING + ARROW_SIZE + 10) .width(20) .height(RECT_SIZE) .fill(createHueGradient()) .build(); final Rectangle colorBarIndicator = RectangleBuilder.create() .x(PICKER_PADDING + PICKER_WIDTH - 32) .y(PICKER_PADDING + ARROW_SIZE + 15) .width(24) .height(10) .arcWidth(4) .arcHeight(4) .stroke(Color.WHITE) .fill(null) .effect(new DropShadow(2, 0, 1, Color.BLACK)) .build(); colorBarIndicator .yProperty() .bind( new DoubleBinding() { { bind(hue); } @Override protected double computeValue() { return (PICKER_PADDING + ARROW_SIZE + 5) + (RECT_SIZE * (hue.get() / 360)); } }); EventHandler<MouseEvent> barMouseHandler = new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { final double y = event.getY() - colorBar.getY(); hue.set(clamp(y / RECT_SIZE) * 360); } }; colorBar.setOnMouseDragged(barMouseHandler); colorBar.setOnMouseClicked(barMouseHandler); Label brightnessLabel = new Label("Brightness:"); brightnessLabel.setMinWidth(Control.USE_PREF_SIZE); GridPane.setConstraints(brightnessLabel, 0, 0); Slider brightnessSlider = SliderBuilder.create().min(0).max(100).id("BrightnessSlider").build(); brightnessSlider.valueProperty().bindBidirectional(bright); GridPane.setConstraints(brightnessSlider, 1, 0); IntegerField brightnessField = new IntegerField(); brightnessField.valueProperty().bindBidirectional(bright); brightnessField.setPrefColumnCount(7); GridPane.setConstraints(brightnessField, 2, 0); Label saturationLabel = new Label("Saturation:"); saturationLabel.setMinWidth(Control.USE_PREF_SIZE); GridPane.setConstraints(saturationLabel, 0, 1); Slider saturationSlider = SliderBuilder.create().min(0).max(100).id("SaturationSlider").build(); saturationSlider.valueProperty().bindBidirectional(sat); GridPane.setConstraints(saturationSlider, 1, 1); saturationSlider .styleProperty() .bind( new StringBinding() { { bind(color); } @Override protected String computeValue() { return "picker-color: hsb(" + hue.get() + ",100%,100%);"; } }); IntegerField saturationField = new IntegerField(); saturationField.valueProperty().bindBidirectional(sat); saturationField.setPrefColumnCount(7); GridPane.setConstraints(saturationField, 2, 1); Label webLabel = new Label("Web:"); webLabel.setMinWidth(Control.USE_PREF_SIZE); GridPane.setConstraints(webLabel, 0, 2); WebColorField webField = new WebColorField(); webField.valueProperty().bindBidirectional(color); GridPane.setConstraints(webField, 1, 2, 2, 1); GridPane controls = new GridPane(); controls.setVgap(5); controls.setHgap(5); controls .getChildren() .addAll( brightnessLabel, brightnessSlider, brightnessField, saturationLabel, saturationSlider, saturationField, webLabel, webField); controls.setManaged(false); controls.resizeRelocate( PICKER_PADDING + 10, PICKER_PADDING + ARROW_SIZE + 10 + 170 + 10, PICKER_WIDTH - 20, controls.getPrefHeight()); getChildren() .addAll( windowClickRect, p, colorRect, colorRectOverlayOne, colorRectOverlayTwo, colorBar, colorRectIndicator, colorBarIndicator, controls); }
private void resize() { width = getSkinnable().getWidth() - getSkinnable().getInsets().getLeft() - getSkinnable().getInsets().getRight(); height = getSkinnable().getHeight() - getSkinnable().getInsets().getTop() - getSkinnable().getInsets().getBottom(); if (ASPECT_RATIO * width > height) { width = 1 / (ASPECT_RATIO / height); } else if (1 / (ASPECT_RATIO / height) > width) { height = ASPECT_RATIO * width; } if (width > 0 && height > 0) { double centerX = width * 0.5; double centerY = height * 0.85; double barRadius = height * 0.54210526; double barWidth = width * 0.28472222; pane.setMaxSize(width, height); pane.relocate( (getSkinnable().getWidth() - width) * 0.5, (getSkinnable().getHeight() - height) * 0.5); barBackground.setCenterX(centerX); barBackground.setCenterY(centerY); barBackground.setRadiusX(barRadius); barBackground.setRadiusY(barRadius); barBackground.setStrokeWidth(barWidth); barBackground.setStartAngle(angleRange * 0.5 + 90); barBackground.setLength(-angleRange); if (sectionsVisible && sectionsAlwaysVisible) { sectionLayer.setPrefSize(width, height); drawSections(); } bar.setCenterX(centerX); bar.setCenterY(centerY); bar.setRadiusX(barRadius); bar.setRadiusY(barRadius); bar.setStrokeWidth(barWidth); bar.setStartAngle(angleRange * 0.5 + 90); bar.setLength(-(getSkinnable().getCurrentValue() - minValue) * angleStep); double needleWidth = height * 0.13157895; double needleHeight = height * 0.91315789; needle.setCache(true); needleMoveTo1.setX(0.0); needleMoveTo1.setY(0.927953890489914 * needleHeight); needleCubicCurveTo2.setControlX1(0); needleCubicCurveTo2.setControlY1(0.968299711815562 * needleHeight); needleCubicCurveTo2.setControlX2(0.22 * needleWidth); needleCubicCurveTo2.setControlY2(needleHeight); needleCubicCurveTo2.setX(0.5 * needleWidth); needleCubicCurveTo2.setY(needleHeight); needleCubicCurveTo3.setControlX1(0.78 * needleWidth); needleCubicCurveTo3.setControlY1(needleHeight); needleCubicCurveTo3.setControlX2(needleWidth); needleCubicCurveTo3.setControlY2(0.968299711815562 * needleHeight); needleCubicCurveTo3.setX(needleWidth); needleCubicCurveTo3.setY(0.927953890489914 * needleHeight); needleCubicCurveTo4.setControlX1(needleWidth); needleCubicCurveTo4.setControlY1(0.92507204610951 * needleHeight); needleCubicCurveTo4.setControlX2(0.6 * needleWidth); needleCubicCurveTo4.setControlY2(0.0144092219020173 * needleHeight); needleCubicCurveTo4.setX(0.6 * needleWidth); needleCubicCurveTo4.setY(0.0144092219020173 * needleHeight); needleCubicCurveTo5.setControlX1(0.6 * needleWidth); needleCubicCurveTo5.setControlY1(0.0144092219020173 * needleHeight); needleCubicCurveTo5.setControlX2(0.58 * needleWidth); needleCubicCurveTo5.setControlY2(0); needleCubicCurveTo5.setX(0.5 * needleWidth); needleCubicCurveTo5.setY(0); needleCubicCurveTo6.setControlX1(0.42 * needleWidth); needleCubicCurveTo6.setControlY1(0); needleCubicCurveTo6.setControlX2(0.4 * needleWidth); needleCubicCurveTo6.setControlY2(0.0144092219020173 * needleHeight); needleCubicCurveTo6.setX(0.4 * needleWidth); needleCubicCurveTo6.setY(0.0144092219020173 * needleHeight); needleCubicCurveTo7.setControlX1(0.4 * needleWidth); needleCubicCurveTo7.setControlY1(0.0144092219020173 * needleHeight); needleCubicCurveTo7.setControlX2(0); needleCubicCurveTo7.setControlY2(0.92507204610951 * needleHeight); needleCubicCurveTo7.setX(0); needleCubicCurveTo7.setY(0.927953890489914 * needleHeight); needle.setCache(true); needle.setCacheHint(CacheHint.ROTATE); needle.relocate( (width - needle.getLayoutBounds().getWidth()) * 0.5, centerY - needle.getLayoutBounds().getHeight() + needle.getLayoutBounds().getWidth() * 0.5); needleRotate.setPivotX(needle.getLayoutBounds().getWidth() * 0.5); needleRotate.setPivotY( needle.getLayoutBounds().getHeight() - needle.getLayoutBounds().getWidth() * 0.5); resizeStaticText(); } }