下面列出了javafx.scene.paint.Color#hsb ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void renderImage(WritableImage img, double gridSize, double hueFactor, double hueOffset) {
PixelWriter pw = img.getPixelWriter();
double w = img.getWidth();
double h = img.getHeight();
double xRatio = 0.0;
double yRatio = 0.0;
double hue = 0.0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
xRatio = x/w;
yRatio = y/h;
hue = Math.sin(yRatio*(gridSize*Math.PI))*Math.sin(xRatio*(gridSize*Math.PI))*Math.tan(hueFactor/20.0)*360.0 + hueOffset;
Color c = Color.hsb(hue, 1.0, 1.0);
pw.setColor(x, y, c);
}
}
}
/**
* Draws a color wheel into the given {@link WritableImage}, starting at the
* given offsets, in the given size (in pixel).
*
* @param image
* The {@link WritableImage} in which the color wheel is drawn.
* @param offsetX
* The horizontal offset (in pixel).
* @param offsetY
* The vertical offset (in pixel).
* @param size
* The size (in pixel).
*/
private void renderColorWheel(WritableImage image, int offsetX, int offsetY,
int size) {
PixelWriter px = image.getPixelWriter();
double radius = size / 2;
Point2D mid = new Point2D(radius, radius);
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
double d = mid.distance(x, y);
if (d <= radius) {
// compute hue angle
double angleRad = d == 0 ? 0
: Math.atan2(y - mid.getY(), x - mid.getX());
// compute saturation depending on distance to
// middle
// ([0;1])
double sat = d / radius;
Color color = Color.hsb(angleRad * 180 / Math.PI, sat, 1);
px.setColor(offsetX + x, offsetY + y, color);
} else {
px.setColor(offsetX + x, offsetY + y, Color.TRANSPARENT);
}
}
}
}
private static void renderImage(WritableImage img, double gridSize, double hueFactor, double hueOffset) {
PixelWriter pw = img.getPixelWriter();
double w = img.getWidth();
double h = img.getHeight();
double xRatio = 0.0;
double yRatio = 0.0;
double hue = 0.0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
xRatio = x/w;
yRatio = y/h;
hue = Math.sin(yRatio*(gridSize*Math.PI))*Math.sin(xRatio*(gridSize*Math.PI))*Math.tan(hueFactor/20.0)*360.0 + hueOffset;
Color c = Color.hsb(hue, 1.0, 1.0);
pw.setColor(x, y, c);
}
}
}
public synchronized Color next()
{ // Change hue, which gives the most dramatic difference
hue += 120.0;
if (hue >= 360)
{ // Run different set of hues
hue = (((int) hue)+30) % 360;
if (hue == 120.0)
{ // Cycle darker colors
brightness -= 0.5;
if (brightness <= 0.0)
{ // All the same starting at a weaker and darker color
// This scheme will then be repeated...
hue = 0.0;
saturation = 0.7;
brightness = 0.9;
}
}
}
return Color.hsb(hue, saturation, brightness);
}
@Override
public Color createColor() {
final Color color = Color.hsb(365.0 * (double) hue / (double) total, saturation, brightness);
hue += 2;
if (hue >= total) {
hue = 1;
total *= 2;
}
return color;
}
public static final Color[] createColorVariations(final Color COLOR, final int NO_OF_COLORS) {
int noOfColors = clamp(1, 12, NO_OF_COLORS);
double step = 0.8 / noOfColors;
double hue = COLOR.getHue();
double brg = COLOR.getBrightness();
Color[] colors = new Color[noOfColors];
for (int i = 0; i < noOfColors; i++) {
colors[i] = Color.hsb(hue, 0.2 + i * step, brg);
}
return colors;
}
public SimpleHSBColorPicker() {
getChildren().addAll(hsbRect,lightRect);
lightRect.setStroke(Color.GRAY);
lightRect.setStrokeType(StrokeType.OUTSIDE);
EventHandler<MouseEvent> ml = new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
double w = getWidth();
double h = getHeight();
double x = Math.min(w, Math.max(0, e.getX()));
double y = Math.min(h, Math.max(0, e.getY()));
double hue = (360/w)*x;
double vert = (1/h)*y;
double sat = 0;
double bright = 0;
if (vert<0.5) {
bright = 1;
sat = vert * 2;
} else {
bright = sat = 1- ((vert-0.5)*2);
}
// convert back to color
Color c = Color.hsb((int)hue,sat,bright);
color.set(c);
e.consume();
}
};
lightRect.setOnMouseDragged(ml);
lightRect.setOnMouseClicked(ml);
}
private LinearGradient buildHueBar() {
double offset;
Stop[] stops = new Stop[255];
for (int y = 0; y < 255; y++) {
offset = (double) (1.0 / 255) * y;
int h = (int)((y / 255.0) * 360);
stops[y] = new Stop(offset, Color.hsb(h, 1.0, 1.0));
}
return new LinearGradient(0f, 0f, 1f, 0f, true, CycleMethod.NO_CYCLE, stops);
}
public static Node createIconContent() {
double offset;
Stop[] stops = new Stop[255];
for (int y = 0; y < 255; y++) {
offset = (double) (1.0 / 255) * y;
int h = (int)((y / 255.0) * 360);
stops[y] = new Stop(offset, Color.hsb(h, 0.8, 0.9));
}
Rectangle rect = new Rectangle(80,80,
new LinearGradient(0f, 0f, 1f, 1f, true, CycleMethod.NO_CYCLE, stops));
rect.setArcWidth(20);
rect.setArcHeight(20);
return rect;
}
private Color[] getSectionColors(final Color LCD_BACKGROUND_COLOR, final Color LCD_FOREGROUND_COLOR) {
double hue = LCD_BACKGROUND_COLOR.getHue();
double sat = LCD_BACKGROUND_COLOR.getSaturation();
Color[] colors;
if (Helper.isMonochrome(LCD_BACKGROUND_COLOR)) {
// Section color is monochrome
colors = new Color[]{
Color.hsb(hue, 0, 0.69),
Color.hsb(hue, 0, 1.0),
Color.hsb(hue, 0, 0.76),
Color.hsb(hue, 0, 0.76),
Color.hsb(hue, sat, 0.69),
Helper.isDark(LCD_BACKGROUND_COLOR) ? Color.WHITE : Color.BLACK,
Helper.isDark(LCD_BACKGROUND_COLOR) ? Color.rgb(255, 255, 255, 0.1) : Color.rgb(0, 0, 0, 0.1)
};
} else {
// Section color is not monochrome
colors = new Color[]{
Color.hsb(hue, sat, 0.69),
Color.hsb(hue, sat, 1.0),
Color.hsb(hue, sat, 0.76),
Color.hsb(hue, sat, 0.76),
Color.hsb(hue, sat, 0.69),
LCD_FOREGROUND_COLOR,
Color.color(LCD_BACKGROUND_COLOR.getRed(), LCD_BACKGROUND_COLOR.getGreen(), LCD_BACKGROUND_COLOR.getBlue(), 0.1)
};
}
return colors;
}
private Color getColor(int iColor){
double fact=(double) iColor / (double) numColors;
switch(colorPalette){
case HSB:
// There are 6*255=1530 distinct pure colors, 255 colors every 60º, with 100% saturation and value
return Color.hsb(360d*fact, 1d, 1d);
case GREEN:
return GREEN_COLORS.get((int)(fact*GREEN_COLORS.size()));
}
return Color.rgb((iColor >> 16) & 0xFF, (iColor >> 8) & 0xFF, iColor & 0xFF);
}
public static Node createIconContent() {
double offset;
Stop[] stops = new Stop[255];
for (int y = 0; y < 255; y++) {
offset = (double) (1.0 / 255) * y;
int h = (int)((y / 255.0) * 360);
stops[y] = new Stop(offset, Color.hsb(h, 0.8, 0.9));
}
Rectangle rect = new Rectangle(80,80,
new LinearGradient(0f, 0f, 1f, 1f, true, CycleMethod.NO_CYCLE, stops));
rect.setArcWidth(20);
rect.setArcHeight(20);
return rect;
}
@Override public Node create3dContent() {
Xform sceneRoot = new Xform();
cubeXform = new Xform[128];
cube = new Cube[128];
int i;
for (i = 0; i < 128; i++) {
cubeXform[i] = new Xform();
cubeXform[i].setTranslateX((double) 2);
cube[i] = new Cube(1.0, Color.hsb((double) i*1.2, 1.0, 1.0, 0.3), 1.0);
if (i == 0) {
sceneRoot.getChildren().add(cubeXform[i]);
}
else if (i >= 1) {
cubeXform[i-1].getChildren().add(cubeXform[i]);
}
cubeXform[i].getChildren().add(cube[i]);
}
audioSpectrumListener = this;
getAudioMediaPlayer().setAudioSpectrumListener(audioSpectrumListener);
getAudioMediaPlayer().play();
getAudioMediaPlayer().setAudioSpectrumInterval(0.02);
getAudioMediaPlayer().setAudioSpectrumNumBands(128);
getAudioMediaPlayer().setCycleCount(Timeline.INDEFINITE);
sceneRoot.setRotationAxis(Rotate.X_AXIS);
sceneRoot.setRotate(180.0);
sceneRoot.setTranslateY(-100.0);
return sceneRoot;
}
public static final Color[] getColorRangeMinMax(final Color COLOR, final int STEPS) {
double hue = COLOR.getHue();
double saturation = COLOR.getSaturation();
double brightness = COLOR.getBrightness();
double saturationStep = saturation / STEPS;
double brightnessStep = brightness / STEPS;
double halfSteps = STEPS / 2;
Color fromColor = COLOR.hsb(hue, saturation, clamp(0, 1, brightness + brightnessStep * halfSteps));
Color toColor = COLOR.hsb(hue, saturation, clamp(0, 1, brightness - brightnessStep * halfSteps));
return new Color[] { fromColor, toColor };
}
public static final Color[] createColorVariations(final Color COLOR, final int NO_OF_COLORS) {
int noOfColors = clamp(1, 12, NO_OF_COLORS);
double step = 0.8 / noOfColors;
double hue = COLOR.getHue();
double brg = COLOR.getBrightness();
Color[] colors = new Color[noOfColors];
for (int i = 0 ; i < noOfColors ; i++) { colors[i] = Color.hsb(hue, 0.2 + i * step, brg); }
return colors;
}
private void updateSectionColors() {
int listSize = sections.size();
sectionColorMap.clear();
for (int i = 0 ; i < listSize ; i++) {
Color sectionColor = sections.get(i).getColor();
Color lcdForegroundColor;
if (Helper.isMonochrome(sectionColor)) {
lcdForegroundColor = Helper.isDark(sectionColor) ? Color.WHITE : Color.BLACK;
} else {
lcdForegroundColor = Color.hsb(sectionColor.getHue(), sectionColor.getSaturation(), sectionColor.getBrightness() * 0.3);
}
Color lcdBackgroundColor = Color.color(sectionColor.getRed(), sectionColor.getGreen(), sectionColor.getBlue(), 0.1);
sectionColorMap.put(sections.get(i), getSectionColors(lcdBackgroundColor, lcdForegroundColor));
}
}
public SanFranciscoFireworks() {
// create a color palette of 180 colors
colors = new Paint[181];
colors[0] = new RadialGradient(0, 0, 0.5, 0.5, 0.5, true, CycleMethod.NO_CYCLE,
new Stop(0, Color.WHITE),
new Stop(0.2, Color.hsb(59, 0.38, 1)),
new Stop(0.6, Color.hsb(59, 0.38, 1,0.1)),
new Stop(1, Color.hsb(59, 0.38, 1,0))
);
for (int h=0;h<360;h+=2) {
colors[1+(h/2)] = new RadialGradient(0, 0, 0.5, 0.5, 0.5, true, CycleMethod.NO_CYCLE,
new Stop(0, Color.WHITE),
new Stop(0.2, Color.hsb(h, 1, 1)),
new Stop(0.6, Color.hsb(h, 1, 1,0.1)),
new Stop(1, Color.hsb(h, 1, 1,0))
);
}
// create canvas
canvas = new Canvas(1024, 500);
canvas.setBlendMode(BlendMode.ADD);
canvas.setEffect(new Reflection(0,0.4,0.15,0));
background = new ImageView(getClass().getResource("sf.jpg").toExternalForm());
getChildren().addAll(background,canvas);
// create animation timer that will be called every frame
// final AnimationTimer timer = new AnimationTimer() {
timer = new AnimationTimer() {
@Override public void handle(long now) {
GraphicsContext gc = canvas.getGraphicsContext2D();
// clear area with transparent black
gc.setFill(Color.rgb(0, 0, 0, 0.2));
gc.fillRect(0, 0, 1024, 708);
// draw fireworks
drawFireworks(gc);
// countdown to launching the next firework
if (countDownTillNextFirework == 0) {
countDownTillNextFirework = 10 + (int)(Math.random()*30);
fireParticle();
}
countDownTillNextFirework --;
}
};
}
public SanFranciscoFireworks() {
// create a color palette of 180 colors
colors = new Paint[181];
colors[0] = new RadialGradient(0, 0, 0.5, 0.5, 0.5, true, CycleMethod.NO_CYCLE,
new Stop(0, Color.WHITE),
new Stop(0.2, Color.hsb(59, 0.38, 1)),
new Stop(0.6, Color.hsb(59, 0.38, 1,0.1)),
new Stop(1, Color.hsb(59, 0.38, 1,0))
);
for (int h=0;h<360;h+=2) {
colors[1+(h/2)] = new RadialGradient(0, 0, 0.5, 0.5, 0.5, true, CycleMethod.NO_CYCLE,
new Stop(0, Color.WHITE),
new Stop(0.2, Color.hsb(h, 1, 1)),
new Stop(0.6, Color.hsb(h, 1, 1,0.1)),
new Stop(1, Color.hsb(h, 1, 1,0))
);
}
// create canvas
canvas = new Canvas(1024, 500);
canvas.setBlendMode(BlendMode.ADD);
canvas.setEffect(new Reflection(0,0.4,0.15,0));
background = new ImageView(getClass().getResource("sf.jpg").toExternalForm());
getChildren().addAll(background,canvas);
// create animation timer that will be called every frame
// final AnimationTimer timer = new AnimationTimer() {
timer = new AnimationTimer() {
@Override public void handle(long now) {
GraphicsContext gc = canvas.getGraphicsContext2D();
// clear area with transparent black
gc.setFill(Color.rgb(0, 0, 0, 0.2));
gc.fillRect(0, 0, 1024, 708);
// draw fireworks
drawFireworks(gc);
// countdown to launching the next firework
if (countDownTillNextFirework == 0) {
countDownTillNextFirework = 10 + (int)(Math.random()*30);
fireParticle();
}
countDownTillNextFirework --;
}
};
}
public void setXYZData(ArrayList<Double> xData, ArrayList<Double> yData, ArrayList<Double> zData) {
xAxisData = xData;
yAxisData = yData;
zAxisData = zData;
getChildren().clear();
//for now we will always default to x axis
//later we could maybe dynamically determine the smallest axis and then
//uses 0's for the other axes that are larger.
ArrayList<Point3D> point3DList = new ArrayList<>();
for(int i=0;i<xAxisData.size();i++) {
//some safety checks for array sizes
double translateY = 0.0;
double translateZ = 0.0;
if(!yAxisData.isEmpty() && yAxisData.size() > i)
translateY = yAxisData.get(i);
if(!zAxisData.isEmpty() && zAxisData.size() > i)
translateZ = zAxisData.get(i);
setTranslateX(xAxisData.get(i));
//Convert to Floats and build list of adjusted points
point3DList.add(new Point3D(new Float(xAxisData.get(i)), new Float(translateY), new Float(translateZ)));
float width = 1;
final TriangleMesh mesh = new TriangleMesh();
//add each point. For each point add another point shifted on Z axis by width
//This extra point allows us to build triangles later
for(Point3D point: point3DList) {
//Rear points
//top right rear point
mesh.getPoints().addAll(point.x+width,point.y+width,point.z+width);
//top left rear point
mesh.getPoints().addAll(point.x-width,point.y+width,point.z+width);
//bottom right rear point
mesh.getPoints().addAll(point.x+width,point.y-width,point.z+width);
//bottom left rear point
mesh.getPoints().addAll(point.x-width,point.y-width,point.z+width);
//Front points
//top right front point
mesh.getPoints().addAll(point.x+width,point.y+width,point.z-width);
//top left front point
mesh.getPoints().addAll(point.x-width,point.y+width,point.z-width);
//bottom right front point
mesh.getPoints().addAll(point.x+width,point.y-width,point.z-width);
//bottom left front point
mesh.getPoints().addAll(point.x-width,point.y-width,point.z-width);
}
//add dummy Texture Coordinate
mesh.getTexCoords().addAll(0,0);
//Now generate nodes for each point
for(int p=8;p<point3DList.size()*7;p+=8) { //add each segment
//Wind the next 8 vertices as a cube. The cube itself will represent the data
//Vertices wound counter-clockwise which is the default front face of any Triangle
//Rear triangle faces should be wound clockwise to face away from center
mesh.getFaces().addAll(p,0,p+3,0,p+2,0); //TRR,BLR,BRR
mesh.getFaces().addAll(p+3,0,p,0,p+1,0); //BLR,TRR,TLR
//left side faces
mesh.getFaces().addAll(p+1,0,p+5,0,p+3,0); //TLR,TLF,BLR
mesh.getFaces().addAll(p+5,0,p+7,0,p+3,0); //TLF,BLR,BLF
//front side faces
mesh.getFaces().addAll(p+5,0,p+7,0,p+4,0); //TLF,BLF,TLR
mesh.getFaces().addAll(p+4,0,p+7,0,p+6,0); //TRF,BLF,BRF
//front side faces
mesh.getFaces().addAll(p+4,0,p+6,0,p+2,0); //TRF,BRF,BRR
mesh.getFaces().addAll(p+4,0,p+2,0,p,0); //TRF,BRR,TRR
//Top faces
mesh.getFaces().addAll(p,0,p+1,0,p+3,0); //TRR,TLR,TRF
mesh.getFaces().addAll(p+1,0,p+5,0,p+3,0); //TLR,TLF,TRF
//bottom faces
mesh.getFaces().addAll(p+3,0,p+7,0,p+6,0); //BLR,BLF,BRF
mesh.getFaces().addAll(p+3,0,p+6,0,p+2,0); //BLR,BRF,BRR
}
//Need to add the mesh to a MeshView before adding to our 3D scene
MeshView meshView = new MeshView(mesh);
meshView.setDrawMode(DrawMode.FILL); //Fill so that the line shows width
Color hsb = Color.hsb((new Double(i) / 12) * 360, 1.0, 1.0, 0.5);
PhongMaterial material = new PhongMaterial(hsb);
material.setDiffuseColor(hsb);
material.setSpecularColor(hsb);
meshView.setMaterial(material);
//Make sure you Cull the Back so that no black shows through
meshView.setCullFace(CullFace.BACK);
// //Add some ambient light so folks can see it
// Group line = new Group();
// AmbientLight light = new AmbientLight(Color.WHITE);
// light.getScope().add(meshView);
// line.getChildren().add(light);
// line.getChildren().add(meshView);
getChildren().addAll(meshView);
}
}
public static final Color getComplementaryColor(final Color COLOR) {
return Color.hsb(COLOR.getHue() + 180, COLOR.getSaturation(), COLOR.getBrightness());
}