Class SNTColor

java.lang.Object
sc.fiji.snt.util.SNTColor

public class SNTColor extends Object
A simple class for handling Colors including the ability to map an AWT Color to a SWC type integer tag.
Author:
Tiago Ferreira
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    SNTColor(Color color)
    Instantiates a new SNT color without SWC type association
    SNTColor(Color color, int swcType)
    Instantiates a new SNT color.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Color
    alphaColor(Color c, double percent)
    Adds an alpha component to an AWT color.
    static Color
    Averages a collection of colors
    Retrieves the AWT color
    static String
    Returns the color encoded as hex string with the format #rrggbbaa.
    static Color
    Returns a suitable 'contrast' color.
    boolean
     
    static Color
    Returns an AWT Color from a (#)RRGGBB(AA) hex string.
    static Color
    fromString(String colorName)
    Returns an AWT Color from any css-valid co
    static org.scijava.util.ColorRGB[]
    getDistinctColors(int nColors)
    Returns distinct colors based on Kenneth Kelly's 22 colors of maximum contrast (black and white excluded).
    static org.scijava.util.ColorRGB[]
    getDistinctColors(int nColors, String excludedHue)
    Returns distinct colors based on Kenneth Kelly's 22 colors of maximum contrast (black and white excluded).
    static Color[]
    getDistinctColorsAWT(int nColors)
     
    static String[]
    getDistinctColorsHex(int nColors)
    Returns distinct colors based on Kenneth Kelly's 22 colors of maximum contrast (black and white excluded) as Hex values.
    static String[]
    getDistinctColorsHex(int nColors, String excludedHue)
    Returns distinct colors based on Kenneth Kelly's 22 colors of maximum contrast (black and white excluded) as Hex values.
    int
     
    static void
    Replaces null colors in an array with the average of flanking non-null colors.
    boolean
    Checks if an SWC type has been defined.
    void
    Re-assigns an AWT color.
    void
    setSWCType(int swcType)
    Re-assigns a SWC type integer flag
     
    int
    Retrieves the SWC type
    static org.scijava.util.ColorRGB
    valueOf(String colorSpec)
    Parses a color from the given string.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • SNTColor

      public SNTColor(Color color, int swcType)
      Instantiates a new SNT color.
      Parameters:
      color - the AWT color
      swcType - the SWC type integer flag to be associated with color
    • SNTColor

      public SNTColor(Color color)
      Instantiates a new SNT color without SWC type association
      Parameters:
      color - the AWT color
  • Method Details

    • color

      public Color color()
      Retrieves the AWT color
      Returns:
      the AWT color
    • type

      public int type()
      Retrieves the SWC type
      Returns:
      the SWC type integer flag
    • isTypeDefined

      public boolean isTypeDefined()
      Checks if an SWC type has been defined.
      Returns:
      true, if an SWC integer flag has been specified
    • setAWTColor

      public void setAWTColor(Color color)
      Re-assigns an AWT color.
      Parameters:
      color - the new color
    • setSWCType

      public void setSWCType(int swcType)
      Re-assigns a SWC type integer flag
      Parameters:
      swcType - the new SWC type
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • colorToString

      public static String colorToString(Object color)
      Returns the color encoded as hex string with the format #rrggbbaa.
      Parameters:
      color - the input AWT color
      Returns:
      the converted string
    • fromString

      public static Color fromString(String colorName)
      Returns an AWT Color from any css-valid co
      Parameters:
      colorName - the input string
      Returns:
      the converted AWT color
    • fromHex

      public static Color fromHex(String hex)
      Returns an AWT Color from a (#)RRGGBB(AA) hex string.
      Parameters:
      hex - the input string
      Returns:
      the converted AWT color
    • average

      public static Color average(Collection<Color> colors)
      Averages a collection of colors
      Parameters:
      colors - the colors to be averaged
      Returns:
      the averaged color. Note that an average will never be accurate because the RGB space is not linear. Color.BLACK is returned if all colors in input collection are null;
    • interpolateNullEntries

      public static void interpolateNullEntries(Color[] colors)
      Replaces null colors in an array with the average of flanking non-null colors.
      Parameters:
      colors - the color array
    • alphaColor

      public static Color alphaColor(Color c, double percent)
      Adds an alpha component to an AWT color.
      Parameters:
      c - the input color
      percent - alpha value in percentage
      Returns:
      the color with an alpha component
    • contrastColor

      public static Color contrastColor(Color c)
      Returns a suitable 'contrast' color.
      Parameters:
      c - the input color
      Returns:
      Either white or black, as per hue of input color.
    • getDistinctColors

      public static org.scijava.util.ColorRGB[] getDistinctColors(int nColors)
      Returns distinct colors based on Kenneth Kelly's 22 colors of maximum contrast (black and white excluded). More details on this SO discussion
      Parameters:
      nColors - the number of colors to be retrieved.
      Returns:
      the maximum contrast colors
    • getDistinctColors

      public static org.scijava.util.ColorRGB[] getDistinctColors(int nColors, String excludedHue)
      Returns distinct colors based on Kenneth Kelly's 22 colors of maximum contrast (black and white excluded). More details on this SO discussion
      Parameters:
      nColors - the number of colors to be retrieved
      excludedHue - an optional string defining a hue to be excluded. Either 'red', 'green', 'blue', or 'dim'
      Returns:
      the maximum contrast colors
    • valueOf

      public static org.scijava.util.ColorRGB valueOf(String colorSpec)
      Parses a color from the given string.

      The following formats are supported: Hex format [HTML color codes starting with hash (#)], Color presets (e.g., 'blue', 'pink', 'silver', etc.), and integer triples of the form r,g,b, with each element in the range [0, 255].

      Parameters:
      colorSpec - string defining the color value
      Returns:
      the color
    • getDistinctColorsHex

      public static String[] getDistinctColorsHex(int nColors, String excludedHue)
      Returns distinct colors based on Kenneth Kelly's 22 colors of maximum contrast (black and white excluded) as Hex values. More details on this SO discussion
      Parameters:
      nColors - the number of colors to be retrieved
      excludedHue - an optional string defining a hue to be excluded. Either 'red', 'green', 'blue', or 'dim'
      Returns:
      the maximum contrast colors as hex color strings
    • getDistinctColorsHex

      public static String[] getDistinctColorsHex(int nColors)
      Returns distinct colors based on Kenneth Kelly's 22 colors of maximum contrast (black and white excluded) as Hex values. More details on this SO discussion
      Parameters:
      nColors - the number of colors to be retrieved
      Returns:
      the maximum contrast colors as hex color strings
    • getDistinctColorsAWT

      public static Color[] getDistinctColorsAWT(int nColors)