[Solved] Changing color attributes


The constructor for ColorPane will
Create the rectangle and set the fill color to something medium: not too bright or too dark, not too saturated or unsaturated.
Bind the width and height of the rectangle to the width and the height of the pane. That way the rectangle will cover the entire pane
Set the position of the rectangle to (0,0)
Add the rectangle to the pane (it’s not a child just because it is an instance variable)

To do this, you need to use this code:

Rectangle rectangle = new Rectangle(0, 0, pane.getPrefWidth(), pane.getPrefHeight());
pane.getChildren().add(rectangle);

There will be six methods that change the color of the rectangle. Each of them will follow approximately the same approach:

1: Get the Fill of the rectangle and cast it to Color

2: Get the hue, saturation, and brightness of the fill color (methods in Color)

3: Modify the component that the method is changing

4: Recreate the color (Color.hsb)

5: Set the fill of the rectangle

To get the color, do this:

Color color = (Color) rectangle.getFill();

To get the hue, saturation, and brightness of the color, do this:

double hue = color.getHue();
double saturation = color.getSaturation();
double brightness = color.getBrightness();

To set the fill of the rectangle, do this:

rectangle.setFill(color);

Hue up:

rectangle.setFill(Color.hsb(color.getHue() + 30, color.getSaturation(), color.getBrightness()));

Hue Down:

rectangle.setFill(Color.hsb(color.getHue() - 30, color.getSaturation(), color.getBrightness()));

More Saturated:

rectangle.setFill(Color.hsb(color.getHue(), color.getSaturation()^2, color.getBrightness()));

Less Saturated:

rectangle.setFill(Color.hsb(color.getHue(), Math.sqrt(color.getSaturation()), color.getBrightness()));

Lighter:

rectangle.setFill(Color.hsb(color.getHue(), color.getSaturation(), color.getBrightness()^2));

Darker:

rectangle.setFill(Color.hsb(color.getHue(), color.getSaturation(), Math.sqrt(color.getBrightness())));

1

solved Changing color attributes