java.lang.Object
org.jfree.svg.SvgColor

public final class SvgColorextends Object
Parses the CSS colour forms allowed for graph theme variables.

Graph colours are moving from values hard-coded into the SVG to CSS custom properties, so a theme can set them by name. That means the server has to read whatever the theme author wrote and turn it into a Color, and it has to accept the shorthand forms people actually write.

Supported, all with an optional alpha:

  • #rgb and #rgba — #f0a, #ee9d
  • #rrggbb and #rrggbbaa — #00ff99, #00ff9980
  • rgb(r,g,b) and rgba(r,g,b,a) — rgb(0,255,153), rgba(0,255,153,.5)

Alpha is 0–255 as written in hex, or 0–1 as written in rgb/rgba, matching CSS. Shorthand digits are doubled, so #ee9d is #eeee99dd — the CSS rule, not "expand each digit once".

Anything unrecognised returns null rather than a wrong colour: a theme with a typo in one variable should fall back to the built-in value for that one series, not silently render every plot in the default colour.

  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Utility class, not intended for instantiation.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static float
    clamp01(float f)
    Limits a fraction to the range 0 to 1.
    static String
    format(Color color)
    Render a colour the way a theme author would write it: hex, shortened to the smallest form that is still exact, with alpha appended when not opaque.
    private static String
    hex2(int v)
    Writes one byte as two hex digits.
    private static char
    hexDigit(int v)
    Writes one nibble as a hex digit.
    private static int
    hexValue(char c)
    Gives the value of one hex digit.
    private static boolean
    isShort(int v)
    Checks whether a byte can be written as a doubled hex digit.
    static Color
    parse(String value)
    Parse a CSS colour.
    private static Integer
    Parses one colour channel, percent or 0-255.
    private static Float
    Parse a decimal without Float.parseFloat, so a stray character yields null instead of an exception propagating out of a theme lookup.
    private static Color
    Parses an rgb(...) colour value.
    private static Color
    parseHex(String digits)
    Convert hex digits to a color.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SvgColor

      private SvgColor()
      Utility class, not intended for instantiation.
  • Method Details

    • parse

      public static Color parse(String value)
      Parse a CSS colour.
      Parameters:
      value - the text to parse, may be null
      Returns:
      the colour, or null if value is not a form this understands
    • parseHex

      private static Color parseHex(String digits)
      Convert hex digits to a color.
      Parameters:
      digits - the text after '#', 3, 4, 6 or 8 characters
      Returns:
      the color, or null if the digits are not valid hex
    • parseFunctional

      private static Color parseFunctional(String text)
      Parses an rgb(...) colour value.
      Parameters:
      text - the functional notation to parse
      Returns:
      the colour, null if malformed
    • parseChannel

      private static Integer parseChannel(String part)
      Parses one colour channel, percent or 0-255.
      Parameters:
      part - the text to parse
      Returns:
      the channel value, null if malformed
    • parseFloat

      private static Float parseFloat(String s)
      Parse a decimal without Float.parseFloat, so a stray character yields null instead of an exception propagating out of a theme lookup.
      Parameters:
      s - the text to parse
      Returns:
      the parsed value, or null if s is not a plain decimal number
    • clamp01

      private static float clamp01(float f)
      Limits a fraction to the range 0 to 1.
      Parameters:
      f - the fraction to clamp, 0 to 1
      Returns:
      the clamped fraction
    • hexValue

      private static int hexValue(char c)
      Gives the value of one hex digit.
      Parameters:
      c - the character to convert
      Returns:
      its value, -1 if not a hex digit
    • format

      public static String format(Color color)
      Render a colour the way a theme author would write it: hex, shortened to the smallest form that is still exact, with alpha appended when not opaque.
      Parameters:
      color - the colour to render, may be null
      Returns:
      a CSS colour string, or null when color is null
    • isShort

      private static boolean isShort(int v)
      Checks whether a byte can be written as a doubled hex digit.
      Parameters:
      v - the value to check
      Returns:
      true if the short hex form applies
    • hex2

      private static String hex2(int v)
      Writes one byte as two hex digits.
      Parameters:
      v - the value to write
      Returns:
      the hex text
    • hexDigit

      private static char hexDigit(int v)
      Writes one nibble as a hex digit.
      Parameters:
      v - the nibble, 0 to 15
      Returns:
      the hex digit for it