2016-12-01 8 views
1

여기에서 알 수 있듯이 http://www.rapidtables.com/web/color/RGB_Color.htm 색상은 알고리즘 패턴에서 빨간색에서 파란색으로, 어두운 색상에서 흰색으로 바뀝니다. 기본 루프에서 어떻게 코딩 하시겠습니까? Im이 JavaFX 교육을 위해 제작했습니다!색상 팔레트 빨간색의 색상 그리드를 blue'ish로 출력하는 알고리즘

는이 링크에 표시되는 색상 팔레트는 HSV (색상, 채도, 값) 색상
for(int x = 0;x<12;x++) { 
     for(int y = 0; y< 10; y++) { 
      Random random = new Random(); 
      int r = random.nextInt(256); 
      int g = random.nextInt(256); 
      int b = random.nextInt(256); 

      Label label = new Label(); 
      label.setPrefSize(30,30); 
      label.setStyle("-fx-background-color: rgb(" + r + "," + g + "," + b + ")"); 
      colorPane.add(label, x,y); 
     } 
    } 

답변

1

: 여기

는 지금까지 임의의 색상으로, 알고리즘로 대체해야 한 기본 구조입니다 팔레트. RGB (빨강, 초록, 파랑)를 사용하여 그것을 재현하려고하면 몇 가지 두통이 생길 것입니다.

예 HSV 구현 (안된) : 자바를 호출하는

int h = 0; 
for(int x = 0; x < 10; x++) { 
    for(int y = 0; y < 10; y++) { 
     int s = 10 * y; 
     int b = 10 * x; 

     Label label = new Label(); 
     label.setPrefSize(30,30); 
     label.setStyle("-fx-background-color: hsb(" + h + "," + s + "%," + b + "%)"); 
     colorPane.add(label, x,y); 
    } 
} 

주 HSB (색조, 채도, 밝기) 어떤 이유로 대신 HSV의 (아마도 "값이"매우 비 설명이기 때문에) . 코드 내 참조는 Javadoc (정적 인 hsb(double h, double s, double b) 메소드 참조)을 확인하고 CSS에서 정의하려면 CSS guide (여기에서 설명하는 것처럼)을 확인하십시오.

1

참고 Label 대신 Rectangle을 사용하는 것이 좋습니다.

또한 HSB 값이 더 적합 할 것 같다 :

  • 모든 열은 동일한 색상을 가지고
  • 열 전반 휘도 증가하지만 채도 1
  • 후반 유지 열의 밝기는 1로 유지되지만 채도는 1에서 0으로 감소합니다.

증분이있는 회색 음영 만 표시되므로 마지막 열은 예외입니다 g 밝기.

다음 코드는 팔레트를 만들 수 있습니다 (또는 적어도 충분히 가깝게) :

@Override 
public void start(Stage primaryStage) { 
    GridPane gridPane = new GridPane(); 
    gridPane.setHgap(4); 
    gridPane.setVgap(4); 

    final int columns = 12; 
    final int rows = 10; 
    final int fullBrightness = (rows - 1)/2; 
    final int columnCount = columns - 1; 

    // fill upper half with full saturation but increasing brightness 
    for (int y = 0; y <= fullBrightness; y++) { 
     double brightness = y/(double) fullBrightness; 
     for (int x = 0; x < columnCount; x++) { 
      Rectangle rect = new Rectangle(15, 15, Color.hsb(x * (360d/(columns - 1)), 1, brightness)); 
      rect.setStroke(Color.BLACK); 
      gridPane.add(rect, x, y); 
     } 
    } 

    // fill lower half with full brightness but decreasing saturation 
    for (int y = fullBrightness + 1; y < rows; y++) { 
     double saturation = 1 - ((double) (y - fullBrightness))/(columns - 1 - fullBrightness); 
     for (int x = 0; x < columnCount; x++) { 
      Rectangle rect = new Rectangle(15, 15, Color.hsb(x * (360d/(columns - 1)), saturation, 1)); 
      rect.setStroke(Color.BLACK); 
      gridPane.add(rect, x, y); 
     } 
    } 

    // fill last column with grayscale 
    for (int y = 0, maxIndex = rows - 1; y < rows; y++) { 
     Rectangle rect = new Rectangle(15, 15, Color.hsb(0, 0, y/(double) maxIndex)); 
     rect.setStroke(Color.BLACK); 
     gridPane.add(rect, columnCount, y); 
    } 

    Scene scene = new Scene(gridPane); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
}