Class BlendComposite

  • All Implemented Interfaces:
    java.awt.Composite

    public final class BlendComposite
    extends java.lang.Object
    implements java.awt.Composite
    A Composite implementing the common "blend modes" of image editors (multiply, burn, hard light, difference, divide), which the standard AlphaComposite does not provide.

    The source alpha (including the coverage produced by antialiasing) controls how strongly the blended color replaces the destination, following the "source over" rule of the W3C compositing model, so that a translucent destination is handled correctly as well. It works on any color model, but is implemented per pixel and thus only meant for drawing thin shapes such as grid lines. It must be used on BufferedImage backed graphics, since hardware accelerated pipelines do not support custom composites.

    Since:
    19640
    • Method Detail

      • createContext

        public java.awt.CompositeContext createContext​(java.awt.image.ColorModel srcColorModel,
                                                       java.awt.image.ColorModel dstColorModel,
                                                       java.awt.RenderingHints hints)
        Specified by:
        createContext in interface java.awt.Composite
      • blend

        static int blend​(BlendComposite.Mode mode,
                         int s,
                         int d)
        Blends one color channel.
        Parameters:
        mode - blend mode
        s - source channel value (0-255)
        d - destination channel value (0-255)
        Returns:
        blended value (0-255)
      • composePixel

        static int composePixel​(BlendComposite.Mode mode,
                                int s,
                                int d)
        Composes one pixel: blends the source color with the destination and combines the two with the "source over" rule, so that both the source alpha (including the coverage produced by antialiasing) and a translucent destination are handled correctly.
        Parameters:
        mode - blend mode
        s - source color (ARGB, not premultiplied)
        d - destination color (ARGB, not premultiplied)
        Returns:
        the resulting color (ARGB, not premultiplied)
      • composeChannel

        private static int composeChannel​(BlendComposite.Mode mode,
                                          int cs,
                                          int cb,
                                          int sa,
                                          int da,
                                          int a)
        Composes one color channel of a translucent destination, see the W3C compositing model: co = as*(1-ab)*Cs + as*ab*B(Cb,Cs) + (1-as)*ab*Cb and Co = co/ao.
        Parameters:
        mode - blend mode
        cs - source channel value (0-255)
        cb - destination (backdrop) channel value (0-255)
        sa - source alpha (0-255)
        da - destination alpha (0-255)
        a - the resulting alpha (0-255), must not be 0
        Returns:
        the resulting channel value (0-255)
      • mix

        private static int mix​(int s,
                               int d,
                               int alpha)
        linear interpolation between d (alpha 0) and s (alpha 255)