注意我建議使用Rectangle
s而不是Label
s。
此外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();
}