Thursday, December 27, 2012

Create and adjust Color using hue, saturation, brightness

To create Color using hue, saturation, brightness, we can call static method Color.hsb(double hue, double saturation, double brightness) or Color.hsb(double hue, double saturation, double brightness, double opacity). Such that we can adjust color of a image in hue, saturation, brightness.

Create and adjust Color using hue, saturation, brightness

package testjavafx;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

 * @web
public class TestJavaFX extends Application {
    PixelReader pixelReader;
    int width, height;
    WritableImage writableImage, writableImageHue, writableImageSaturation, writableImageBrightness;
    PixelWriter pixelWriter, pixelWriterHue, pixelWriterSaturation, pixelWriterBrightness;
    ImageView destImageView, imageViewHue, imageViewSaturation, imageViewBrightness;
    Slider sliderHue, sliderSaturation, sliderBrightness;
    double adjHue, adjSaturation, adjBrightness;
    public void start(Stage primaryStage) {
        Image image = new Image("");
        ImageView imageView = new ImageView();
        pixelReader = image.getPixelReader();
        width = (int)image.getWidth();
        height = (int)image.getHeight();
        //Copy from source to destination pixel by pixel
        writableImage = new WritableImage(width, height);
        pixelWriter = writableImage.getPixelWriter();
        writableImageHue = new WritableImage(width, height);
        pixelWriterHue = writableImageHue.getPixelWriter();
        writableImageSaturation = new WritableImage(width, height);
        pixelWriterSaturation = writableImageSaturation.getPixelWriter();
        writableImageBrightness = new WritableImage(width, height);
        pixelWriterBrightness = writableImageBrightness.getPixelWriter();
        destImageView = new ImageView();
        imageViewHue = new ImageView();
        imageViewSaturation = new ImageView();
        imageViewBrightness = new ImageView();
        HBox hBox_Image = new HBox();
        hBox_Image.getChildren().addAll(imageView, destImageView);
        //Control box for Hue
        sliderHue = SliderBuilder.create()
        HBox hBox_Hue = new HBox();
        hBox_Hue.getChildren().addAll(imageViewHue, sliderHue);
        //Control box for Saturation
        sliderSaturation = SliderBuilder.create()
        HBox hBox_Saturation = new HBox();
        hBox_Saturation.getChildren().addAll(imageViewSaturation, sliderSaturation);
        //Control box for Blue
        sliderBrightness = SliderBuilder.create()
        HBox hBox_Brightness = new HBox();
        hBox_Brightness.getChildren().addAll(imageViewBrightness, sliderBrightness);
        VBox vBox = new VBox();
                hBox_Hue, hBox_Saturation, hBox_Brightness);

        StackPane root = new StackPane();
        Scene scene = new Scene(root, 350, 330);
    ChangeListener<Number> sliderChangeListener
            = new ChangeListener<Number>(){

        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
    private void updateImage(){
        adjHue = sliderHue.valueProperty().doubleValue();
        adjSaturation = sliderSaturation.valueProperty().doubleValue();
        adjBrightness = sliderBrightness.valueProperty().doubleValue();
        for (int y = 0; y < height; y++){
            for (int x = 0; x < width; x++){
                Color color = pixelReader.getColor(x, y);
                pixelWriter.setColor(x, y, color);
                double hue = color.getHue() + adjHue;
                if(hue > 360.0){
                    hue = hue - 360;
                }else if(hue < 0.0){
                    hue = hue + 360.0;
                double saturation = color.getSaturation() + adjSaturation;
                if(saturation > 1.0){
                    saturation = 1.0;
                }else if(saturation < 0.0){
                    saturation = 0.0;
                double brightness = color.getBrightness() + adjBrightness;
                if(brightness > 1.0){
                    brightness = 1.0;
                }else if(brightness < 0.0){
                    brightness = 0.0;
                double opacity = color.getOpacity();
                pixelWriterHue.setColor(x, y, new Color(hue/360.0, hue/360.0, hue/360.0, opacity));
                pixelWriterSaturation.setColor(x, y, new Color(saturation, saturation, saturation, opacity));
                pixelWriterBrightness.setColor(x, y, new Color(brightness, brightness, brightness, opacity));
                Color newColor = Color.hsb(hue, saturation, brightness, opacity);
                pixelWriter.setColor(x, y, newColor);


    public static void main(String[] args) {

Related: Set effect on ImageView with ColorAdjust

No comments:

Post a Comment

Post a Comment