Class Palette


  • public final class Palette
    extends java.lang.Object
    A theme-aware palette of standard colours. The same standard set of named colours is available with variations suitable for different contexts. The colours are divided into subsets, first by brightness, then by opacity. For example, you can request Palette.get.dark.opaque.blue.

    The special subsets foreground and background will be either light or dark depending on the theme. For example, if the theme is dark, then asking for a background colour returns a dark colour, matching the theme's dark background; asking for a foreground colour returns a light colour (to contrast with the dark background).

    The special colours white and black do not have light and dark versions: these return white or black as requested. The special colours text and fill return colours that can be used for text and a solid background, respectively.

    Since:
    3.4
    Author:
    Chris Jennings
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Palette.Subset
      Encapsulates a palette subset broken into opaque and slightly translucent groups.
      static class  Palette.Variant  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Palette.Subset contrasting​(int rgb)
      Returns either the light or dark palette subset, whichever will contrast best with the specified colour.
      Palette.Subset contrasting​(java.awt.Component c)
      Returns either the light or dark palette subset, whichever will contrast best with the specified background.
      static Palette createDefaultPalette()
      Returns a new palette filled with default values.
      void debugDump()
      Inserts a chart of this palette's colours into the script console.
      static Palette fromImage​(java.awt.image.BufferedImage im)
      Creates a palette from an image.
      Palette.Subset harmonizing​(int rgb)
      Returns either the light or dark palette subset, whichever will harmonize best with the specified colour.
      Palette.Subset harmonizing​(java.awt.Component c)
      Returns either the light or dark palette subset, whichever will harmonize best with the specified background.
      java.awt.image.BufferedImage toImage()
      Returns the colours of this palette as an image in which y-index is the base colour and the x-index is the variant.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • NUM_COLORS

        public static final int NUM_COLORS
        Returns the number of base colours (red, orange, etc.). Each base colour is available as either light or dark, and opaque or translucent.
        See Also:
        Constant Field Values
      • get

        public static Palette get
        The current palette.
      • pastel

        public final Palette.Subset pastel
        The pastel subset of the palette. These are brighter and paler than the light subset.
      • light

        public final Palette.Subset light
        The light subset of the palette.
      • dark

        public final Palette.Subset dark
        The dark subset of the palette.
      • foreground

        public final Palette.Subset foreground
        Either the light or dark subset, chosen to contrast with the background based on the theme.
      • background

        public final Palette.Subset background
        Either the light or dark subset, chosen to contrast with the foreground based on the theme.
    • Method Detail

      • contrasting

        public final Palette.Subset contrasting​(java.awt.Component c)
        Returns either the light or dark palette subset, whichever will contrast best with the specified background. If the component is null or its background cannot be determined, a subset based on the theme is returned.
        Parameters:
        c - the component to consider as the source of the background
      • contrasting

        public final Palette.Subset contrasting​(int rgb)
        Returns either the light or dark palette subset, whichever will contrast best with the specified colour. Any alpha component is ignored.
        Parameters:
        rgb - the RGB value of the colour to contrast with
      • harmonizing

        public final Palette.Subset harmonizing​(java.awt.Component c)
        Returns either the light or dark palette subset, whichever will harmonize best with the specified background. This is simply the opposite of the subset returned by contrasting(java.awt.Component).
        Parameters:
        c - the component to consider as the source of the background
      • harmonizing

        public final Palette.Subset harmonizing​(int rgb)
        Returns either the light or dark palette subset, whichever will harmonize best with the specified colour. This is simply the opposite of the subset returned by contrasting(int).
        Parameters:
        c - the RGB value of the colour to contrast with
      • createDefaultPalette

        public static Palette createDefaultPalette()
        Returns a new palette filled with default values. When using the palette to render content, use the current palette.
        Returns:
        a new default palette
      • toImage

        public java.awt.image.BufferedImage toImage()
        Returns the colours of this palette as an image in which y-index is the base colour and the x-index is the variant. The order of the variants is as follows: opaque (light then dark), followed by translucent (light then dark).
        Returns:
        a non-null image in which each pixel represents one colour in the palette
      • fromImage

        public static Palette fromImage​(java.awt.image.BufferedImage im)
        Creates a palette from an image. The image uses the same layout as that of toImage(). The image may be larger than necessary, but an exception will be thrown if any colours are missing.
        Parameters:
        im - the image to convert into a palette
        Returns:
        the palette image
      • debugDump

        public void debugDump()
        Inserts a chart of this palette's colours into the script console.