下面列出了javafx.scene.paint.Color#getOpacity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static final List<Color> createColorPalette(final Color FROM_COLOR, final Color TO_COLOR,
final int NO_OF_COLORS) {
int steps = clamp(1, 12, NO_OF_COLORS) - 1;
double step = 1.0 / steps;
double deltaRed = (TO_COLOR.getRed() - FROM_COLOR.getRed()) * step;
double deltaGreen = (TO_COLOR.getGreen() - FROM_COLOR.getGreen()) * step;
double deltaBlue = (TO_COLOR.getBlue() - FROM_COLOR.getBlue()) * step;
double deltaOpacity = (TO_COLOR.getOpacity() - FROM_COLOR.getOpacity()) * step;
List<Color> palette = new ArrayList<>(NO_OF_COLORS);
Color currentColor = FROM_COLOR;
palette.add(currentColor);
for (int i = 0; i < steps; i++) {
double red = clamp(0d, 1d, (currentColor.getRed() + deltaRed));
double green = clamp(0d, 1d, (currentColor.getGreen() + deltaGreen));
double blue = clamp(0d, 1d, (currentColor.getBlue() + deltaBlue));
double opacity = clamp(0d, 1d, (currentColor.getOpacity() + deltaOpacity));
currentColor = Color.color(red, green, blue, opacity);
palette.add(currentColor);
}
return palette;
}
/**
* Recreates the heatmap based on the current monochrome map.
* Using this approach makes it easy to change the used color
* mapping.
*/
private void updateHeatMap() {
monochrome.snapshot(SNAPSHOT_PARAMETERS, monochromeImage);
int width = monochromeImage.widthProperty().intValue();
int height = monochromeImage.heightProperty().intValue();
heatMap = new WritableImage(width, height);
Color colorFromMonoChromeImage;
double brightness;
Color mappedColor;
PixelWriter pixelWriter = heatMap.getPixelWriter();
PixelReader pixelReader = monochromeImage.getPixelReader();
for (int y = 0 ; y < height ; y++) {
for (int x = 0 ; x < width ; x++) {
colorFromMonoChromeImage = pixelReader.getColor(x, y);
brightness = colorFromMonoChromeImage.getOpacity();
mappedColor = Helper.getColorAt(mappingGradient, brightness);
pixelWriter.setColor(x, y, fadeColors ? Color.color(mappedColor.getRed(), mappedColor.getGreen(), mappedColor.getBlue(), brightness) : mappedColor);
}
}
setImage(heatMap);
}
public static final List<Color> createColorPalette(final Color FROM_COLOR, final Color TO_COLOR, final int NO_OF_COLORS) {
int steps = clamp(1, 50, NO_OF_COLORS) - 1;
double step = 1.0 / steps;
double deltaRed = (TO_COLOR.getRed() - FROM_COLOR.getRed()) * step;
double deltaGreen = (TO_COLOR.getGreen() - FROM_COLOR.getGreen()) * step;
double deltaBlue = (TO_COLOR.getBlue() - FROM_COLOR.getBlue()) * step;
double deltaOpacity = (TO_COLOR.getOpacity() - FROM_COLOR.getOpacity()) * step;
List<Color> palette = new ArrayList<>(NO_OF_COLORS);
Color currentColor = FROM_COLOR;
palette.add(currentColor);
for (int i = 0 ; i < steps ; i++) {
double red = clamp(0d, 1d, (currentColor.getRed() + deltaRed));
double green = clamp(0d, 1d, (currentColor.getGreen() + deltaGreen));
double blue = clamp(0d, 1d, (currentColor.getBlue() + deltaBlue));
double opacity = clamp(0d, 1d, (currentColor.getOpacity() + deltaOpacity));
currentColor = Color.color(red, green, blue, opacity);
palette.add(currentColor);
}
return palette;
}
public static final List<Color> createColorPalette(final Color FROM_COLOR, final Color TO_COLOR, final int NO_OF_COLORS) {
int steps = clamp(1, 12, NO_OF_COLORS) - 1;
double step = 1.0 / steps;
double deltaRed = (TO_COLOR.getRed() - FROM_COLOR.getRed()) * step;
double deltaGreen = (TO_COLOR.getGreen() - FROM_COLOR.getGreen()) * step;
double deltaBlue = (TO_COLOR.getBlue() - FROM_COLOR.getBlue()) * step;
double deltaOpacity = (TO_COLOR.getOpacity() - FROM_COLOR.getOpacity()) * step;
List<Color> palette = new ArrayList<>(NO_OF_COLORS);
Color currentColor = FROM_COLOR;
palette.add(currentColor);
for (int i = 0 ; i < steps ; i++) {
double red = clamp(0d, 1d, (currentColor.getRed() + deltaRed));
double green = clamp(0d, 1d, (currentColor.getGreen() + deltaGreen));
double blue = clamp(0d, 1d, (currentColor.getBlue() + deltaBlue));
double opacity = clamp(0d, 1d, (currentColor.getOpacity() + deltaOpacity));
currentColor = Color.color(red, green, blue, opacity);
palette.add(currentColor);
}
return palette;
}
public static java.awt.Color fxColorToAWT(final Color color) {
final int r = (int) (color.getRed() * 255d);
final int g = (int) (color.getGreen() * 255d);
final int b = (int) (color.getBlue() * 255d);
final int opacity = (int) (color.getOpacity() * 255d);
return new java.awt.Color(r, g, b, opacity);
}
public void setColor(Color color) {
double opacity = color.getOpacity();
opacity = opacity * 100D;
slider.setValue(opacity);
colorPicker.setValue(color);
super.setBackgroundColor(color);
}
public SerializableColor(Color color) {
if (color != null) {
r = color.getRed();
g = color.getGreen();
b = color.getBlue();
a = color.getOpacity();
}
}
@Override
public void drawOverlays(final GraphicsContext g)
{
final Color color = this.color.get();
if (color != null && color.getOpacity() > 0 && isVisible.get())
{
g.setStroke(color);
g.setLineWidth(strokeWidth);
g.strokeLine(0, h / 2, w, h / 2);
g.strokeLine(w / 2, 0, w / 2, h);
}
}
public static java.awt.Color toAwtColor(Color color) {
java.awt.Color newColor = new java.awt.Color((int) (color.getRed() * 255),
(int) (color.getGreen() * 255),
(int) (color.getBlue() * 255),
(int) (color.getOpacity() * 255));
return newColor;
}
public static int compareColor(Color o1, Color o2) {
double diff = o2.getHue() - o1.getHue();
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
} else {
diff = o2.getSaturation() - o1.getSaturation();
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
} else {
diff = o2.getBrightness() - o1.getBrightness();
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
} else {
diff = o2.getOpacity() - o1.getOpacity();
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
} else {
return 0;
}
}
}
}
}
/** Convert JFX color into model color
* @param color {@link Color}
* @return {@link WidgetColor}
*/
public static WidgetColor convert(final Color color)
{
return new WidgetColor((int) (color.getRed() * 255),
(int) (color.getGreen() * 255),
(int) (color.getBlue() * 255),
(int) (color.getOpacity() * 255));
}
public static final Color interpolateColor(final Color COLOR1, final Color COLOR2, final double FRACTION, final double TARGET_OPACITY) {
double fraction = clamp(0, 1, FRACTION);
double targetOpacity = TARGET_OPACITY < 0 ? TARGET_OPACITY : clamp(0, 1, FRACTION);
final double RED1 = COLOR1.getRed();
final double GREEN1 = COLOR1.getGreen();
final double BLUE1 = COLOR1.getBlue();
final double OPACITY1 = COLOR1.getOpacity();
final double RED2 = COLOR2.getRed();
final double GREEN2 = COLOR2.getGreen();
final double BLUE2 = COLOR2.getBlue();
final double OPACITY2 = COLOR2.getOpacity();
final double DELTA_RED = RED2 - RED1;
final double DELTA_GREEN = GREEN2 - GREEN1;
final double DELTA_BLUE = BLUE2 - BLUE1;
final double DELTA_OPACITY = OPACITY2 - OPACITY1;
double red = RED1 + (DELTA_RED * fraction);
double green = GREEN1 + (DELTA_GREEN * fraction);
double blue = BLUE1 + (DELTA_BLUE * fraction);
double opacity = targetOpacity < 0 ? OPACITY1 + (DELTA_OPACITY * fraction) : targetOpacity;
red = clamp(0, 1, red);
green = clamp(0, 1, green);
blue = clamp(0, 1, blue);
opacity = clamp(0, 1, opacity);
return Color.color(red, green, blue, opacity);
}
/**
* Convert a color from {@link Color} to {@link ColorRGBA}.
*
* @param color the color
* @return the jme color
*/
@FxThread
public static @Nullable ColorRGBA from(@Nullable final Color color) {
if (color == null) return null;
return new ColorRGBA((float) color.getRed(), (float) color.getGreen(),
(float) color.getBlue(), (float) color.getOpacity());
}
public static java.awt.Color convertColorToAWT(Color col) {
int r = (int) (col.getRed() * 255);
int g = (int) (col.getGreen() * 255);
int b = (int) (col.getBlue() * 255);
int a = (int) (col.getOpacity() * 255);
return new java.awt.Color(r, g, b, a);
}
public static String rgb2css(Color color) {
return "rgba(" + (int) (color.getRed() * 255) + ","
+ (int) (color.getGreen() * 255) + ","
+ (int) (color.getBlue() * 255) + ","
+ color.getOpacity() + ")";
}
public ArrayEditorPopup(@Nullable Color initialColor) {
VBox root = new VBox(5);
root.setPadding(new Insets(10));
getContent().add(root);
root.setStyle("-fx-background-color:-fx-background");
root.setBorder(new Border(
new BorderStroke(Color.GRAY, BorderStrokeStyle.SOLID,
CornerRadii.EMPTY, BorderStroke.THIN
)
)
);
Canvas canvas = new Canvas(128, 32);
StackPane stackPaneCanvas = new StackPane(canvas);
stackPaneCanvas.setBorder(root.getBorder());
stackPaneCanvas.setMaxWidth(canvas.getWidth());
stackPaneCanvas.setAlignment(Pos.CENTER_LEFT);
HBox paneHeader = new HBox(5, stackPaneCanvas, tfAsArray);
root.getChildren().add(paneHeader);
HBox.setHgrow(tfAsArray, Priority.ALWAYS);
tfAsArray.setEditable(false);
GraphicsContext gc = canvas.getGraphicsContext2D();
ValueListener<Double> valListener = (observer, oldValue, newValue) -> {
Color c = getCurrentColor();
if (c != null) {
if (c.getOpacity() < 1) {
//draw a grid to show theres transparency
gc.setGlobalAlpha(1);
gc.setFill(Color.WHITE);
Region.paintCheckerboard(
gc, 0, 0, (int) canvas.getWidth(), (int) canvas.getHeight(), Color.GRAY, Color.WHITE,
5);
}
gc.setGlobalAlpha(c.getOpacity());
gc.setFill(c);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
tfAsArray.setText(SVColor.toStringF(c.getRed(), c.getGreen(), c.getBlue(), c.getOpacity()));
}
};
r.getValueObserver().addListener(valListener);
g.getValueObserver().addListener(valListener);
b.getValueObserver().addListener(valListener);
a.getValueObserver().addListener(valListener);
if (initialColor != null) {
r.getValueObserver().updateValue(getRounded(initialColor.getRed()));
g.getValueObserver().updateValue(getRounded(initialColor.getGreen()));
b.getValueObserver().updateValue(getRounded(initialColor.getBlue()));
a.getValueObserver().updateValue(getRounded(initialColor.getOpacity()));
}
//r
root.getChildren().add(getColorEditor("r", r));
//g
root.getChildren().add(getColorEditor("g", g));
//b
root.getChildren().add(getColorEditor("b", b));
//a
root.getChildren().add(getColorEditor("a", a));
//footer
root.getChildren().add(new Separator(Orientation.HORIZONTAL));
GenericResponseFooter footer = new GenericResponseFooter(true, true, false,
null,
cancelEvent -> {
cancelled = true;
ArrayEditorPopup.this.hide();
},
okEvent -> {
if (!hasInvalid()) {
return;
}
cancelled = false;
ArrayEditorPopup.this.hide();
}
);
ResourceBundle bundle = Lang.ApplicationBundle();
footer.getBtnOk().setText(bundle.getString("ValueEditors.ColorArrayEditor.use"));
root.getChildren().add(footer);
setAutoHide(true);
setHideOnEscape(true); //when push esc key, hide it
valListener.valueUpdated(r.getValueObserver(), null, null);
}
/** Set the color from a JavaFX Color instance */
public SVColorArray(@NotNull Color newValue) {
this(newValue.getRed(), newValue.getGreen(), newValue.getBlue(), newValue.getOpacity());
}
/** Set the color from a JavaFX Color instance */
public SVColorInt(@NotNull Color newValue) {
this(newValue.getRed(), newValue.getGreen(), newValue.getBlue(), newValue.getOpacity());
}
/** Set the color from a JavaFX Color instance */
public SVColorIntArray(@NotNull Color newValue) {
this(newValue.getRed(), newValue.getGreen(), newValue.getBlue(), newValue.getOpacity());
}
/**
* Return the color for a value as an integer with the color values in its bytes. For use e.g. with an IntBuffer
* backed PixelBuffer.
*
* @param value z-Value
* @return integer with one byte each set to alpha, red, green, blue
*/
public int getIntColor(final double value) {
final Color color = getColor(value);
return ((byte) (color.getOpacity() * 255) << 24) + ((byte) (color.getRed() * 255) << 16)
+ ((byte) (color.getGreen() * 255) << 8) + ((byte) (color.getBlue() * 255));
}