Using ColorAdjust

The ColorAdjust effect allows us to change the following properties of any node (usually, it is applied to images):

  • Hue
  • Brightness
  • Contrast
  • Saturation

Refer to the following screenshot:

You can really only see how it works in action, so I advise you to run the following sample and try the corresponding controls:

// chapter8/colors/ColorAdjustDemo
public void start(Stage primaryStage) {
// you can use any local image here, I've just provided web one to simplify project stucture
ImageView iv = new ImageView("https://raw.githubusercontent.com/sgrinev/mastering-javafx-9-10-book/master/resources/sample.jpg");
iv.setFitHeight(240);
iv.setFitWidth(240);
ColorAdjust ca = new ColorAdjust();
iv.setEffect(ca);

VBox root = new VBox(10);
for (DoubleProperty prop : new DoubleProperty[] {
ca.hueProperty(), ca.contrastProperty(), ca.brightnessProperty(), ca.saturationProperty()
}) {
Slider slider = new Slider(-1, 1, 0.);
prop.bind(slider.valueProperty());
root.getChildren().add(new HBox(5, slider, new Label(prop.getName())));
}
root.getChildren().add(iv);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));

primaryStage.setTitle("Color Adjust Demo");
primaryStage.setScene(new Scene(root, 300, 400));
primaryStage.show();
}
A popular question is how to use ColorAdjust to convert an image into a monochrome one. You can achieve that by setting the saturation to -1: ca.setSaturation(-1);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.123.34