Class ConsistentColor


  • public final class ConsistentColor
    extends Object
    A consistent color which is generated from an input string such as a nickname or a bare JID. Consistent means, the resulting color is always the same for same input strings.

    Generating a Color

    To generate a color pass either a nickname or the bare JID of the conversation as input to the generate(CharSequence) method. Generally, nick names should be preferred over bare JIDs.

    
     ConsistentColor color = ConsistentColor.generate(input);
     float red = color.getRed();
     float green = color.getGreen();
     float blue = color.getBlue();
     

    Corrections for Color Vision Deficiencies

    In order to ensure accessibility to users with color vision deficiencies, you can generate corrected colors.

    
     ConsistentColor color = ConsistentColor.generate(input, ConsistentColor.ColorVisionDeficiency.RED_GREEN_BLINDNESS);
     }

    Adapting the Color for specific Background Colors

    To increase readability on colored background, you may want to adapt the color for background colors.

    
     ConsistentColor color = ConsistentColor.generate(input);
     // Adaption for black background
     ConsistentColor adapted = color.adaptForBackground(0, 0, 0);
     

    Integration with JavaFX and AWT

    
     ConsistentColor c = ConsistentColor.generate(input);
     javafx.scene.paint.Color color = javafx.scene.paint.Color.color(c.getRed(), c.getGreen(), c.getBlue());
     java.awt.Color color = new java.awt.Color(c.getRed(), c.getGreen(), c.getBlue());
     

    The default Y value in the YCbCr color space is 0.732.

    Note that this class overrides equals(Object) and hashCode(): Two instances of this classes are equal, if their RGB values are equal.

    This class is immutable.

    See Also:
    XEP-0392: Consistent Color Generation
    • Method Detail

      • generate

        public static ConsistentColor generate​(CharSequence input,
                                               ConsistentColor.ColorVisionDeficiency colorVisionDeficiency,
                                               float y)
        Generates a color for an input string and for a color vision deficiency. This method also allow to pass other values than 0.732 for the Y value in the YCbCr color space.
        Parameters:
        input - The input string.
        colorVisionDeficiency - The color vision deficiency.
        y - The Y value in the YCbCr color space.
        Returns:
        The color.
      • adaptForBackground

        public final ConsistentColor adaptForBackground​(float red,
                                                        float green,
                                                        float blue)
        Adapts the color for a background color.
        Parameters:
        red - The red component of the background color in the range 0.0-1.0.
        green - The green component of the background color in the range 0.0-1.0.
        blue - The blue component of the background color in the range 0.0-1.0.
        Returns:
        The adapted color.
        Throws:
        IllegalArgumentException - If the parameters are out of range.
      • getRed

        public final float getRed()
        Gets the red component of the color, in the range 0.0-1.0.
        Returns:
        The red component.
      • getGreen

        public final float getGreen()
        Gets the green component of the color, in the range 0.0-1.0.
        Returns:
        The green component.
      • getBlue

        public final float getBlue()
        Gets the blue component of the color, in the range 0.0-1.0.
        Returns:
        The blue component.
      • hashCode

        public final int hashCode()
        Overrides:
        hashCode in class Object