Skip to content

Colors and styling

JOID imposes no look: every pixel takes the color you give it. This page shows how to create colors, derive a palette from one of them, fill shapes with gradients and keep shared colors safe.

You will learn

  • How to create a color from components or from a string
  • How to derive a palette from one color
  • How to fill a shape with a gradient
  • How to keep a palette safe from changes
  • Which colors change on their own

A Color is an RGBA value. Pass it to the color(...) setter of a shape, to a TextInfo for text, or to an effect:

RectNode.create(0, 130, 1200, 130).color(ColorsUI.BAND).attach(card);
RectNode.create(60, 60, 140, 140).color(Color.GRAY).attach(card);
RectNode.create(260, 60, 140, 140).color(new Color(0.2F, 0.4F, 0.8F)).attach(card);
RectNode.create(460, 60, 140, 140).color(new Color(230, 120, 50)).attach(card);
RectNode.create(660, 60, 140, 140).color(Color.decode("#55AA55")).attach(card);
RectNode.create(860, 60, 140, 140).color(Color.decode("rgba(51, 102, 204, 0.5)")).attach(card);

Full file: ColorsUI.java

Five squares on a dark card whose lower half is a light grey band: grey, a medium blue, an orange, a green and a half-transparent blue that lets the band show through

Every form gives the same kind of Color: pick the one that matches the value you copy.

There are four ways to build a color:

  • a preset such as Color.WHITE, Color.GRAY or Color.TRANSPARENT;
  • float components from 0F to 1F: new Color(0.2F, 0.4F, 0.8F), with an optional alpha as a fourth argument;
  • integer components from 0 to 255: new Color(230, 120, 50), with an optional alpha from 0 to 255;
  • a string read by Color.decode: "#RRGGBB", "#RRGGBBAA", "rgb(r, g, b)", or "rgba(r, g, b, a)" with an alpha from 0 to 1.

Color.decode is the shortest way to copy a color from a design tool, and throws a NumberFormatException on a string it cannot read. Color.rgb and Color.argb read a packed int such as 0x3366CC, and Color.encode writes #RRGGBBAA back.

The constructor picks its scale from the type of its arguments: new Color(1, 0, 0) is a nearly black red (1 out of 255), new Color(1F, 0F, 0F) a full red.

darker, brighter, withAlpha and to return a new color and leave the original as it is, so one base color gives a consistent palette:

RectNode.create(0, 130, 1200, 130).color(ColorsUI.BAND).attach(card);
RectNode.create(60, 60, 140, 140).color(ColorsUI.BRAND).attach(card);
RectNode.create(240, 60, 140, 140).color(ColorsUI.BRAND.darker()).attach(card);
RectNode.create(420, 60, 140, 140).color(ColorsUI.BRAND.darker(0.25F)).attach(card);
RectNode.create(600, 60, 140, 140).color(ColorsUI.BRAND.brighter(0.6F)).attach(card);
RectNode.create(780, 60, 140, 140).color(ColorsUI.BRAND.withAlpha(0.5F)).attach(card);
RectNode.create(960, 60, 140, 140).color(ColorsUI.BRAND.to(Color.WHITE, 0.5F)).attach(card);

Full file: ColorsUI.java

Six blue squares on a dark card whose lower half is a light grey band: the base, two darker shades, a lighter tint, a half-transparent copy that lets the band show through, and an opaque mix with white

One base color gives the whole palette, and the base itself stays as it was.
  • darker(scale) multiplies the red, green and blue components by 1 - scale (0.5F without argument).
  • brighter(scale) multiplies them by 1 + scale (0.2F without argument); a component never goes above 1F.
  • withAlpha(alpha) keeps the color and replaces its opacity; withRed, withGreen and withBlue replace one component.
  • to(target, progress) blends toward target: 0F gives the color itself, 1F the target, 0.5F the middle.

multiply, add and scale combine colors component by component.

Color.toGradient turns a color into a linear gradient from that color to another. A gradient is a Color too, so it goes where a color goes: fills, borders, text and image tints.

RectNode.create(0, 130, 1200, 130).color(ColorsUI.BAND).attach(card);
final Point top = Point.of(0D, 0D);
final Point bottom = Point.of(0D, 1D);
final Point corner = Point.of(1D, 1D);
final Color clear = ColorsUI.BRAND.withAlpha(0F);
RectNode.create(60, 60, 240, 140).color(ColorsUI.BRAND.toGradient(Color.WHITE)).attach(card);
RectNode.create(340, 60, 240, 140).color(ColorsUI.BRAND.toGradient(Color.WHITE, top, bottom)).attach(card);
RectNode.create(620, 60, 240, 140).color(ColorsUI.BRAND.toGradient(Color.WHITE, top, corner)).attach(card);
RectNode.create(900, 60, 240, 140).color(ColorsUI.BRAND.toGradient(clear)).attach(card);

Full file: ColorsUI.java

Four gradients from blue on a dark card whose lower half is a light grey band: to white left to right, to white top to bottom, to white along the diagonal, and blue fading to transparent, which lets the card and the band show through

Two points turn the same gradient in any direction, and a target with an alpha of 0 fades it out.

Without points, the gradient goes from the left edge to the right edge. Two Point values set another direction, in fractions of the drawn shape: Point.of(0D, 0D) is the top-left corner and Point.of(0D, 1D) the bottom-left one. Color.decode reads gradients as well: "gradient(#3366CC, #FFFFFF)", with four optional numbers after the colors for the two points.

Fading to the same color with an alpha of 0 keeps the hue; Color.TRANSPARENT is a transparent black, which greys the middle of the gradient.

A color that you create with new can change in place: red(...), green(...), blue(...) and alpha(...) modify it, and every node that uses it shows the change at its next frame. LockableValue.lock freezes a color, so that a palette shared by your UIs never changes by accident:

private static final Color BRAND = Color.decode("#3366CC").lock();

Full file: ColorsUI.java

A locked color refuses alpha(...) and the other changes with an IllegalStateException that names the copy to use, such as withAlpha(...). The presets of Color are locked, and Color.copy gives an unlocked copy of any color.

The color of a node can also follow the state of your UI: give color(...) an expression that reads a signal, or a lambda (Signals). A second color for the hovered state is set with hoveredColor(...) (Hover, tooltips and cursors).

Two presets change with the clock of JOID:

  • Color.RAINBOW cycles through the hues every 3 s;
  • Color.LOADING pulses slowly between two dark greys, for loading placeholders (Rebuilding the tree).

A color derived from them changes with them: Color.RAINBOW.darker() is a darker rainbow, and Color.LOADING.withAlpha(0.5F) a half-transparent pulse.

RectNode.create(60, 60, 160, 160).color(Color.RAINBOW).attach(card);
RectNode.create(260, 60, 160, 160).color(Color.RAINBOW.darker()).attach(card);
RectNode.create(460, 60, 280, 160).color(Color.LOADING).attach(card);

Full file: LiveColorsUI.java

No animator and no signal drive these nodes: the colors read the clock of JOID on every frame.

Color.RAINBOW() and Color.LOADING() return the shade of this instant, as a color that stays put.

  • Build a Color from a preset, float components (0F to 1F), integer components (0 to 255) or Color.decode.
  • Derive a palette with darker, brighter, withAlpha and to: each call returns a new color.
  • A gradient from toGradient is a color, with a direction given by two points in fractions of the shape.
  • lock() your shared colors: a locked color refuses in-place changes, as the presets do.
  • RAINBOW and LOADING change on their own, and so do the colors derived from them.

Fade a card out. Fill a card with #3366CC at the top, fading to transparent at the bottom.

Solution
final Color blue = Color.decode("#3366CC");
final Color fade = blue.toGradient(blue.withAlpha(0F), Point.of(0D, 0D), Point.of(0D, 1D));
RectNode.create(0, 0, 300, 200).color(fade).attach(this);

Darken on hover. Make a button 25 % darker while the mouse is over it, without writing the darker color by hand.

Solution
RectNode.create(0, 0, 200, 60).color(ColorsUI.BRAND).hoveredColor(ColorsUI.BRAND.darker(0.25F)).attach(this);

Report a problem with this page