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 http://java-buddy.blogspot.com/
 */
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;
    
    @Override
    public void start(Stage primaryStage) {
        
        Image image = new Image("http://goo.gl/kYEQl");
        ImageView imageView = new ImageView();
        imageView.setImage(image);
        
        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()
                .prefWidth(300)
                .min(-360)
                .max(360)
                .majorTickUnit(30)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderHue.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Hue = new HBox();
        hBox_Hue.getChildren().addAll(imageViewHue, sliderHue);
        
        //Control box for Saturation
        sliderSaturation = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderSaturation.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Saturation = new HBox();
        hBox_Saturation.getChildren().addAll(imageViewSaturation, sliderSaturation);
        
        //Control box for Blue
        sliderBrightness = SliderBuilder.create()
                .prefWidth(300)
                .min(-1)
                .max(1)
                .majorTickUnit(0.2)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(0)
                .build();
        sliderBrightness.valueProperty().addListener(sliderChangeListener);
        HBox hBox_Brightness = new HBox();
        hBox_Brightness.getChildren().addAll(imageViewBrightness, sliderBrightness);
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(hBox_Image, 
                hBox_Hue, hBox_Saturation, hBox_Brightness);

        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        Scene scene = new Scene(root, 350, 330);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
        
        updateImage();
    }
    
    ChangeListener<Number> sliderChangeListener
            = new ChangeListener<Number>(){

        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            updateImage();
        }
    };
    
    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);

            }
        }
        
        imageViewHue.setImage(writableImageHue);
        imageViewSaturation.setImage(writableImageSaturation);
        imageViewBrightness.setImage(writableImageBrightness);
        destImageView.setImage(writableImage);
    }

    public static void main(String[] args) {
        launch(args);
    }
}




Related: Set effect on ImageView with ColorAdjust


No comments:

Post a Comment

Post a Comment