Class X11Colors

java.lang.Object
edu.cnu.mdi.ui.colors.X11Colors

public final class X11Colors extends Object
Look up colors by their standard web ("X11") name.

Despite the class name, the values here are the HTML4/CSS/SVG web color set (the so-called "X11 web colors"), not the classic X.Org rgb.txt palette. The two clash for a handful of names: web gray/green/maroon/purple differ from the brighter X11 originals (for example X11 green is 0,255,0, which CSS calls lime). All 148-ish web names resolve to the CSS standard RGB values.

Lookups are forgiving. A queried name is matched after:

  • trimming and lower-casing
  • removing all internal whitespace, so "Deep Sky Blue", "deepskyblue" and "deep sky blue" all resolve to the same color
British grey spellings are accepted as aliases for every gray name. getAllColors() still exposes the canonical names in their readable, single-spaced form.
 Color c1 = X11Colors.getX11Color("Deep Sky Blue");
 Color c2 = X11Colors.getX11Color("deepskyblue", 128); // half transparent
 
  • Method Details

    • getX11Color

      public static Color getX11Color(String name, int alpha)
      Gets a web/X11 color and applies the given alpha.
      Parameters:
      name - the color name (case- and whitespace-insensitive)
      alpha - alpha channel, 0..255 (255 is fully opaque)
      Returns:
      a new Color with the given alpha, or null if the name is unknown
      Throws:
      IllegalArgumentException - if alpha is outside 0..255
    • getX11Color

      public static Color getX11Color(String name)
      Gets a color by its web/X11 name.

      Matching ignores case and all whitespace, and accepts British grey spellings, so "Light Gray", "lightgrey" and " LIGHT GREY " all resolve to the same color.

      Parameters:
      name - the color name; may be null
      Returns:
      the named color, or null if name is null or unknown
    • getAllColors

      public static Map<String,Color> getAllColors()
      Returns an unmodifiable view of all available colors, keyed by their canonical display names (lower-case, single-spaced).
      Returns:
      the immutable name-to-color map