Class ColorUtil


  • public class ColorUtil
    extends Object
    Author:
    Kilian
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ColorUtil.ColorPalette  
    • Constructor Summary

      Constructors 
      Constructor Description
      ColorUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int[] argbToComponents​(int argb)
      Convert an argb value to it's individual components in range of 0 - 255
      static javafx.scene.paint.Color argbToFXColor​(int argb)
      Convert an argb value (alpha 24,red 16, green 8, blue 0) into a java fx color.
      static javafx.scene.paint.Color awtToFxColor​(Color awtColor)
      Convert a Java AWT color to it's JavaFX sibling
      static int componentsToARGB​(int alpha, int red, int green, int blue)
      Converts the components to a single int argb representation.
      static double distance​(Color c1, Color c2)
      Compute a distance metric of 2 colors.
      static double distance​(javafx.scene.paint.Color c1, javafx.scene.paint.Color c2)
      Compute a distance metric of 2 colors.
      static Color fxToAwtColor​(javafx.scene.paint.Color fxColor)
      Convert a JavaFX color to it's awt sibling
      static String fxToHex​(javafx.scene.paint.Color color)
      Return the hexcode of a color
      static Color getContrastColor​(Color input)
      Return either white or black depending on the supplied color to guarantee readability.
      static javafx.scene.paint.Color getContrastColor​(javafx.scene.paint.Color input)
      Return either white or black depending on the supplied color to guarantee readability.
      static int getLuma​(Color c)
      Get the Y (luma component) of the YCrCb color model
      static double getLuma​(javafx.scene.paint.Color c)
      Get the Y (luma component) of the YCrCb color model
    • Constructor Detail

      • ColorUtil

        public ColorUtil()
    • Method Detail

      • fxToAwtColor

        public static Color fxToAwtColor​(javafx.scene.paint.Color fxColor)
        Convert a JavaFX color to it's awt sibling
        Parameters:
        fxColor - The fx color to convert
        Returns:
        The awt color
        Since:
        1.0.0 com.github.kilianB
      • awtToFxColor

        public static javafx.scene.paint.Color awtToFxColor​(Color awtColor)
        Convert a Java AWT color to it's JavaFX sibling
        Parameters:
        awtColor - The awt color to convert
        Returns:
        The fx color
        Since:
        1.0.0 com.github.kilianB
      • argbToComponents

        public static int[] argbToComponents​(int argb)
        Convert an argb value to it's individual components in range of 0 - 255
        Parameters:
        argb - values as int
        Returns:
        [0] Alpha, [1] Red, [2] Green, [3] Blue
        Since:
        1.0.0 com.github.kilianB
      • componentsToARGB

        public static int componentsToARGB​(int alpha,
                                           int red,
                                           int green,
                                           int blue)
        Converts the components to a single int argb representation. The individual values are not range checked
        Parameters:
        alpha - in range of 0 - 255
        red - in range of 0 - 255
        green - in range of 0 - 255
        blue - in range of 0 - 255
        Returns:
        a single int representing the argb value
        Since:
        1.0.0 com.github.kilianB
      • argbToFXColor

        public static javafx.scene.paint.Color argbToFXColor​(int argb)
        Convert an argb value (alpha 24,red 16, green 8, blue 0) into a java fx color.
        Parameters:
        argb - the argb color as an int
        Returns:
        The JavaFX Color
        Since:
        1.0.0 com.github.kilianB
      • fxToHex

        public static String fxToHex​(javafx.scene.paint.Color color)
        Return the hexcode of a color
        Parameters:
        color - the color to convert
        Returns:
        a hex representation of the color
        Since:
        1.0.0 com.github.kilianB
      • distance

        public static double distance​(javafx.scene.paint.Color c1,
                                      javafx.scene.paint.Color c2)
        Compute a distance metric of 2 colors. The distance of a color is greater the further away two colors are.

        Identical colors will return a distance of 0.

        Parameters:
        c1 - The first color
        c2 - The second color
        Returns:
        a double value indicating the distance of two colors
        Since:
        1.0.0 com.github.kilianB
      • distance

        public static double distance​(Color c1,
                                      Color c2)
        Compute a distance metric of 2 colors. The distance of a color is greater the further away two colors are.

        Identical colors will return a distance of 0.

        Parameters:
        c1 - The first color
        c2 - The second color
        Returns:
        a double value indicating the distance of two colors
        Since:
        1.0.0 com.github.kilianB
      • getLuma

        public static double getLuma​(javafx.scene.paint.Color c)
        Get the Y (luma component) of the YCrCb color model
        Parameters:
        c - an javaFX color
        Returns:
        the luma component in the tange [0-1]
        Since:
        1.3.2 com.github.kilianB
      • getLuma

        public static int getLuma​(Color c)
        Get the Y (luma component) of the YCrCb color model
        Parameters:
        c - an awt color
        Returns:
        the luma component in the tange [0-255]
        Since:
        1.3.2 com.github.kilianB
      • getContrastColor

        public static javafx.scene.paint.Color getContrastColor​(javafx.scene.paint.Color input)
        Return either white or black depending on the supplied color to guarantee readability. The contrast color is assumed to be used as text overlay on top of the input color.
        Parameters:
        input - the color of the background
        Returns:
        the color (white or black) of the foreground whichever guarantees more readability.
        Since:
        1.0.0 com.github.kilianB
      • getContrastColor

        public static Color getContrastColor​(Color input)
        Return either white or black depending on the supplied color to guarantee readability. The contrast color is assumed to be used as text overlay on top of the input color.
        Parameters:
        input - the color of the background
        Returns:
        the color (white or black) of the foreground whichever guarantees more readability.
        Since:
        1.3.2 com.github.kilianB