Tutorials     About     RSS
Tech and Media Labs

JavaFX Color

Jakob Jenkov
Last update: 2020-04-07

To work with colors in JavaFX, you have the JavaFX Paint, Color, ImagePattern, LinearGradient and RadialGradient. The JavaFX Paint class is the superclass of the JavaFX Color, ImagePattern, LinearGradient and RadialGradient class. The JavaFX color classes are all found in the JavaFX package javafx.scene.paint . Here is how the JavaFX color class hierarchy looks:

  • Paint
    • Color
    • ImagePattern
    • LinearGradient
    • RadialGradient

Each of these classes and color effects will be explained in the following sections.

Paint

The JavaFX Paint class is the superclass of the JavaFX Color, ImagePattern, LinearGradient and RadialGradient classes. You can use the Paint class directly to create Paint instances from CSS color strings. See the JavaFX CSS tutorial for more information about CSS colors. Here is an example of creating a JavaFX Paint instance using the static factory method of the Paint class:

Paint paint = Paint.valueOf("blue");

Color

The JavaFX Color class represents a solid (uniform) color. To create a JavaFX Color instance you use its constructor. The JavaFX Color constructor takes 4 parameters:

  • Red
  • Green
  • Blue
  • Alpha

The Red, Green and Blue parameters are the amount of red, green and blue tone to use in the final color. The Alpha parameter, also called the Opacity parameter, specifies the opacity of the final color. Each parameter takes a value between 0.0 and 1.0.

double red   = 1.0;
double green = 0.8;
double blue  = 0.6;
double alpha = 1.0;

Color color = new Color(red, green, blue, alpha);

The JavaFX Color class also has a set of static factory methods that can help you create Color instances using a variety of different parameters. These factory methods are shown in the JavaFX Color factory methods examples below:

Color color1  = Color.web("#ff00ff");
Color color2  = Color.web("#ff00ff", 0.5);

Color color3  = Color.rgb(255, 0, 255);
Color color4  = Color.rgb(255, 0, 255, 0.5);

Color color5  = Color.grayRgb(255);
Color color6  = Color.grayRgb(255, 0.5);

Color color7  = Color.hsb(1.0, 0.5, 0.8);
Color color8  = Color.hsb(1.0, 0.5, 0.8, 0.5);

Color color9  = Color.color(1.0, 0.0, 1.0);
Color color10 = Color.color(1.0, 0.0, 1.0, 0.5);

As you can see, each method exists in two versions. One that takes the color value itself, and another that takes the color value + alpha value (opacity).

The Color.web() methods creates a Color instance based on a traditional web color code as also used in CSS Colors .

The Color.rgb() methods creates a Color instance from red, green and blue color values.

The Color.grayRgb() methods creates a Color instance representing a gray color. Red, green and blue will be set to the same value, resulting in a gray color.

The Color.hsb() methods creates a Color instance based on Hue, Saturation and Brightness (HSB). Sometimes this is also referred to an HSL color - Hue, Saturation and Lightness (instead of HSB).

The Color.color() methods creates a Color similarly to how the standard Color constructor does it.

Jakob Jenkov

Featured Videos

















Sponsored Ads

Maildroppa - Smart Email Marketing Solution
Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next