JavaFX 效果 - 顏色調整



您可以透過應用顏色調整效果來調整影像的顏色。這包括調整每個畫素的色相、飽和度、亮度對比度

名為ColorAdjust的類(位於javafx.scene.effect包中)表示顏色調整效果,此類包含五個屬性:

  • input - 此屬性為 Effect 型別,它表示顏色調整效果的輸入。

  • brightness - 此屬性為 Double 型別,它表示此效果的亮度調整值。

  • contrast - 此屬性為 Double 型別,它表示此效果的對比度調整值。

  • hue - 此屬性為 Double 型別,它表示此效果的色相調整值。

  • saturation - 此屬性為 Double 型別,它表示此效果的飽和度調整值。

示例

下面的程式是一個演示顏色調整效果的示例。在這裡,我們使用ImageImageView類將影像(Tutorialspoint 徽標)嵌入 JavaFX 場景中。這在位置 100, 70 進行,並且分別具有 200 和 400 的適應高度和適應寬度。

Image View

我們使用顏色調整效果來調整此影像的顏色。對比度、色相、亮度和飽和度值分別為 0.4、-0.05、0.9、0.8。

將此程式碼儲存到名為ColorAdjustEffectExample.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.effect.ColorAdjust; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.stage.Stage; 
         
public class ColorAdjustEffectExample extends Application {   
   @Override 
   public void start(Stage stage) {       
      //Creating an image 
      Image image = new Image("https://tutorialspoint.tw/green/images/logo.png");
      
      //Setting the image view 
      ImageView imageView = new ImageView(image); 
      
      //Setting the position of the image 
      imageView.setX(100);  
      imageView.setY(70); 
      
      //setting the fit height and width of the image view 
      imageView.setFitHeight(200); 
      imageView.setFitWidth(400); 
      
      //Setting the preserve ratio of the image view 
      imageView.setPreserveRatio(true); 
    
      //Instantiating the ColorAdjust class 
      ColorAdjust colorAdjust = new ColorAdjust(); 
      
      //Setting the contrast value 
      colorAdjust.setContrast(0.4);     
      
      //Setting the hue value 
      colorAdjust.setHue(-0.05);     
      
      //Setting the brightness value 
      colorAdjust.setBrightness(0.9);  
      
      //Setting the saturation value 
      colorAdjust.setSaturation(0.8);   
       
      //Applying coloradjust effect to the ImageView node 
      imageView.setEffect(colorAdjust);    
         
      //Creating a Group object  
      Group root = new Group(imageView);   
               
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage 
      stage.setTitle("Coloradjust effect example");
      
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show();         
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}      

使用以下命令從命令提示符編譯並執行儲存的 java 檔案。

javac ColorAdjustEffectExample.java 
java ColorAdjustEffectExample

執行後,上述程式將生成一個如下圖所示的 JavaFX 視窗。

ColorAdjust
javafx_effects.htm
廣告
© . All rights reserved.