Class BlendComposite

java.lang.Object
org.openstreetmap.josm.gui.draw.BlendComposite
All Implemented Interfaces:
Composite

public final class BlendComposite extends Object implements 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
  • Field Details

  • Constructor Details

  • Method Details

    • getInstance

      Returns the composite for the given mode.
      Parameters:
      mode - the blend mode
      Returns:
      the composite
    • getMode

      Returns the blend mode of this composite.
      Returns:
      the blend mode
    • createContext

      public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints)
      Specified by:
      createContext in interface 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)