Path: blob/master/src/java.desktop/share/classes/java/awt/AlphaComposite.java
41152 views
/*1* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt;2627import java.awt.image.ColorModel;28import java.lang.annotation.Native;29import sun.java2d.SunCompositeContext;3031/**32* The {@code AlphaComposite} class implements basic alpha33* compositing rules for combining source and destination colors34* to achieve blending and transparency effects with graphics and35* images.36* The specific rules implemented by this class are the basic set37* of 12 rules described in38* T. Porter and T. Duff, "Compositing Digital Images", SIGGRAPH 84,39* 253-259.40* The rest of this documentation assumes some familiarity with the41* definitions and concepts outlined in that paper.42*43* <p>44* This class extends the standard equations defined by Porter and45* Duff to include one additional factor.46* An instance of the {@code AlphaComposite} class can contain47* an alpha value that is used to modify the opacity or coverage of48* every source pixel before it is used in the blending equations.49*50* <p>51* It is important to note that the equations defined by the Porter52* and Duff paper are all defined to operate on color components53* that are premultiplied by their corresponding alpha components.54* Since the {@code ColorModel} and {@code Raster} classes55* allow the storage of pixel data in either premultiplied or56* non-premultiplied form, all input data must be normalized into57* premultiplied form before applying the equations and all results58* might need to be adjusted back to the form required by the destination59* before the pixel values are stored.60*61* <p>62* Also note that this class defines only the equations63* for combining color and alpha values in a purely mathematical64* sense. The accurate application of its equations depends65* on the way the data is retrieved from its sources and stored66* in its destinations.67* See <a href="#caveats">Implementation Caveats</a>68* for further information.69*70* <p>71* The following factors are used in the description of the blending72* equation in the Porter and Duff paper:73*74* <table class="striped">75* <caption style="display:none">Factors</caption>76* <thead>77* <tr>78* <th scope="col">Factor79* <th scope="col">Definition80* </thead>81* <tbody>82* <tr>83* <th scope="row"><em>A<sub>s</sub></em>84* <td>the alpha component of the source pixel85* <tr>86* <th scope="row"><em>C<sub>s</sub></em>87* <td>a color component of the source pixel in premultiplied form88* <tr>89* <th scope="row"><em>A<sub>d</sub></em>90* <td>the alpha component of the destination pixel91* <tr>92* <th scope="row"><em>C<sub>d</sub></em>93* <td>a color component of the destination pixel in premultiplied form94* <tr>95* <th scope="row"><em>F<sub>s</sub></em>96* <td>the fraction of the source pixel that contributes to the output97* <tr>98* <th scope="row"><em>F<sub>d</sub></em>99* <td>the fraction of the destination pixel that contributes to the output100* <tr>101* <th scope="row"><em>A<sub>r</sub></em>102* <td>the alpha component of the result103* <tr>104* <th scope="row"><em>C<sub>r</sub></em>105* <td>a color component of the result in premultiplied form106* </tbody>107* </table>108* <p>109* Using these factors, Porter and Duff define 12 ways of choosing110* the blending factors <em>F<sub>s</sub></em> and <em>F<sub>d</sub></em> to111* produce each of 12 desirable visual effects.112* The equations for determining <em>F<sub>s</sub></em> and <em>F<sub>d</sub></em>113* are given in the descriptions of the 12 static fields114* that specify visual effects.115* For example,116* the description for117* <a href="#SRC_OVER">{@code SRC_OVER}</a>118* specifies that <em>F<sub>s</sub></em> = 1 and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>).119* Once a set of equations for determining the blending factors is120* known they can then be applied to each pixel to produce a result121* using the following set of equations:122*123* <pre>124* <em>F<sub>s</sub></em> = <em>f</em>(<em>A<sub>d</sub></em>)125* <em>F<sub>d</sub></em> = <em>f</em>(<em>A<sub>s</sub></em>)126* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*<em>F<sub>s</sub></em> + <em>A<sub>d</sub></em>*<em>F<sub>d</sub></em>127* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*<em>F<sub>s</sub></em> + <em>C<sub>d</sub></em>*<em>F<sub>d</sub></em></pre>128*129* <p>130* The following factors will be used to discuss our extensions to131* the blending equation in the Porter and Duff paper:132*133* <table class="striped">134* <caption style="display:none">Factors</caption>135* <thead>136* <tr>137* <th scope="col">Factor138* <th scope="col">Definition139* </thead>140* <tbody>141* <tr>142* <th scope="row"><em>C<sub>sr</sub></em>143* <td>one of the raw color components of the source pixel144* <tr>145* <th scope="row"><em>C<sub>dr</sub></em>146* <td>one of the raw color components of the destination pixel147* <tr>148* <th scope="row"><em>A<sub>ac</sub></em>149* <td>the "extra" alpha component from the AlphaComposite instance150* <tr>151* <th scope="row"><em>A<sub>sr</sub></em>152* <td>the raw alpha component of the source pixel153* <tr>154* <th scope="row"><em>A<sub>dr</sub></em>155* <td>the raw alpha component of the destination pixel156* <tr>157* <th scope="row"><em>A<sub>df</sub></em>158* <td>the final alpha component stored in the destination159* <tr>160* <th scope="row"><em>C<sub>df</sub></em>161* <td>the final raw color component stored in the destination162* </tbody>163* </table>164*165* <h2>Preparing Inputs</h2>166*167* <p>168* The {@code AlphaComposite} class defines an additional alpha169* value that is applied to the source alpha.170* This value is applied as if an implicit SRC_IN rule were first171* applied to the source pixel against a pixel with the indicated172* alpha by multiplying both the raw source alpha and the raw173* source colors by the alpha in the {@code AlphaComposite}.174* This leads to the following equation for producing the alpha175* used in the Porter and Duff blending equation:176*177* <pre>178* <em>A<sub>s</sub></em> = <em>A<sub>sr</sub></em> * <em>A<sub>ac</sub></em> </pre>179*180* All of the raw source color components need to be multiplied181* by the alpha in the {@code AlphaComposite} instance.182* Additionally, if the source was not in premultiplied form183* then the color components also need to be multiplied by the184* source alpha.185* Thus, the equation for producing the source color components186* for the Porter and Duff equation depends on whether the source187* pixels are premultiplied or not:188*189* <pre>190* <em>C<sub>s</sub></em> = <em>C<sub>sr</sub></em> * <em>A<sub>sr</sub></em> * <em>A<sub>ac</sub></em> (if source is not premultiplied)191* <em>C<sub>s</sub></em> = <em>C<sub>sr</sub></em> * <em>A<sub>ac</sub></em> (if source is premultiplied) </pre>192*193* No adjustment needs to be made to the destination alpha:194*195* <pre>196* <em>A<sub>d</sub></em> = <em>A<sub>dr</sub></em> </pre>197*198* <p>199* The destination color components need to be adjusted only if200* they are not in premultiplied form:201*202* <pre>203* <em>C<sub>d</sub></em> = <em>C<sub>dr</sub></em> * <em>A<sub>d</sub></em> (if destination is not premultiplied)204* <em>C<sub>d</sub></em> = <em>C<sub>dr</sub></em> (if destination is premultiplied) </pre>205*206* <h2>Applying the Blending Equation</h2>207*208* <p>209* The adjusted <em>A<sub>s</sub></em>, <em>A<sub>d</sub></em>,210* <em>C<sub>s</sub></em>, and <em>C<sub>d</sub></em> are used in the standard211* Porter and Duff equations to calculate the blending factors212* <em>F<sub>s</sub></em> and <em>F<sub>d</sub></em> and then the resulting213* premultiplied components <em>A<sub>r</sub></em> and <em>C<sub>r</sub></em>.214*215* <h2>Preparing Results</h2>216*217* <p>218* The results only need to be adjusted if they are to be stored219* back into a destination buffer that holds data that is not220* premultiplied, using the following equations:221*222* <pre>223* <em>A<sub>df</sub></em> = <em>A<sub>r</sub></em>224* <em>C<sub>df</sub></em> = <em>C<sub>r</sub></em> (if dest is premultiplied)225* <em>C<sub>df</sub></em> = <em>C<sub>r</sub></em> / <em>A<sub>r</sub></em> (if dest is not premultiplied) </pre>226*227* Note that since the division is undefined if the resulting alpha228* is zero, the division in that case is omitted to avoid the "divide229* by zero" and the color components are left as230* all zeros.231*232* <h2>Performance Considerations</h2>233*234* <p>235* For performance reasons, it is preferable that236* {@code Raster} objects passed to the {@code compose}237* method of a {@link CompositeContext} object created by the238* {@code AlphaComposite} class have premultiplied data.239* If either the source {@code Raster}240* or the destination {@code Raster}241* is not premultiplied, however,242* appropriate conversions are performed before and after the compositing243* operation.244*245* <h2><a id="caveats">Implementation Caveats</a></h2>246*247* <ul>248* <li>249* Many sources, such as some of the opaque image types listed250* in the {@code BufferedImage} class, do not store alpha values251* for their pixels. Such sources supply an alpha of 1.0 for252* all of their pixels.253*254* <li>255* Many destinations also have no place to store the alpha values256* that result from the blending calculations performed by this class.257* Such destinations thus implicitly discard the resulting258* alpha values that this class produces.259* It is recommended that such destinations should treat their stored260* color values as non-premultiplied and divide the resulting color261* values by the resulting alpha value before storing the color262* values and discarding the alpha value.263*264* <li>265* The accuracy of the results depends on the manner in which pixels266* are stored in the destination.267* An image format that provides at least 8 bits of storage per color268* and alpha component is at least adequate for use as a destination269* for a sequence of a few to a dozen compositing operations.270* An image format with fewer than 8 bits of storage per component271* is of limited use for just one or two compositing operations272* before the rounding errors dominate the results.273* An image format274* that does not separately store275* color components is not a276* good candidate for any type of translucent blending.277* For example, {@code BufferedImage.TYPE_BYTE_INDEXED}278* should not be used as a destination for a blending operation279* because every operation280* can introduce large errors, due to281* the need to choose a pixel from a limited palette to match the282* results of the blending equations.283*284* <li>285* Nearly all formats store pixels as discrete integers rather than286* the floating point values used in the reference equations above.287* The implementation can either scale the integer pixel288* values into floating point values in the range 0.0 to 1.0 or289* use slightly modified versions of the equations290* that operate entirely in the integer domain and yet produce291* analogous results to the reference equations.292*293* <p>294* Typically the integer values are related to the floating point295* values in such a way that the integer 0 is equated296* to the floating point value 0.0 and the integer297* 2^<em>n</em>-1 (where <em>n</em> is the number of bits298* in the representation) is equated to 1.0.299* For 8-bit representations, this means that 0x00300* represents 0.0 and 0xff represents301* 1.0.302*303* <li>304* The internal implementation can approximate some of the equations305* and it can also eliminate some steps to avoid unnecessary operations.306* For example, consider a discrete integer image with non-premultiplied307* alpha values that uses 8 bits per component for storage.308* The stored values for a309* nearly transparent darkened red might be:310*311* <pre>312* (A, R, G, B) = (0x01, 0xb0, 0x00, 0x00)</pre>313*314* <p>315* If integer math were being used and this value were being316* composited in317* <a href="#SRC">{@code SRC}</a>318* mode with no extra alpha, then the math would319* indicate that the results were (in integer format):320*321* <pre>322* (A, R, G, B) = (0x01, 0x01, 0x00, 0x00)</pre>323*324* <p>325* Note that the intermediate values, which are always in premultiplied326* form, would only allow the integer red component to be either 0x00327* or 0x01. When we try to store this result back into a destination328* that is not premultiplied, dividing out the alpha will give us329* very few choices for the non-premultiplied red value.330* In this case an implementation that performs the math in integer331* space without shortcuts is likely to end up with the final pixel332* values of:333*334* <pre>335* (A, R, G, B) = (0x01, 0xff, 0x00, 0x00)</pre>336*337* <p>338* (Note that 0x01 divided by 0x01 gives you 1.0, which is equivalent339* to the value 0xff in an 8-bit storage format.)340*341* <p>342* Alternately, an implementation that uses floating point math343* might produce more accurate results and end up returning to the344* original pixel value with little, if any, round-off error.345* Or, an implementation using integer math might decide that since346* the equations boil down to a virtual NOP on the color values347* if performed in a floating point space, it can transfer the348* pixel untouched to the destination and avoid all the math entirely.349*350* <p>351* These implementations all attempt to honor the352* same equations, but use different tradeoffs of integer and353* floating point math and reduced or full equations.354* To account for such differences, it is probably best to355* expect only that the premultiplied form of the results to356* match between implementations and image formats. In this357* case both answers, expressed in premultiplied form would358* equate to:359*360* <pre>361* (A, R, G, B) = (0x01, 0x01, 0x00, 0x00)</pre>362*363* <p>364* and thus they would all match.365*366* <li>367* Because of the technique of simplifying the equations for368* calculation efficiency, some implementations might perform369* differently when encountering result alpha values of 0.0370* on a non-premultiplied destination.371* Note that the simplification of removing the divide by alpha372* in the case of the SRC rule is technically not valid if the373* denominator (alpha) is 0.374* But, since the results should only be expected to be accurate375* when viewed in premultiplied form, a resulting alpha of 0376* essentially renders the resulting color components irrelevant377* and so exact behavior in this case should not be expected.378* </ul>379* @see Composite380* @see CompositeContext381*/382383public final class AlphaComposite implements Composite {384/**385* Both the color and the alpha of the destination are cleared386* (Porter-Duff Clear rule).387* Neither the source nor the destination is used as input.388*<p>389* <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = 0, thus:390*<pre>391* <em>A<sub>r</sub></em> = 0392* <em>C<sub>r</sub></em> = 0393*</pre>394*/395@Native public static final int CLEAR = 1;396397/**398* The source is copied to the destination399* (Porter-Duff Source rule).400* The destination is not used as input.401*<p>402* <em>F<sub>s</sub></em> = 1 and <em>F<sub>d</sub></em> = 0, thus:403*<pre>404* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>405* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>406*</pre>407*/408@Native public static final int SRC = 2;409410/**411* The destination is left untouched412* (Porter-Duff Destination rule).413*<p>414* <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = 1, thus:415*<pre>416* <em>A<sub>r</sub></em> = <em>A<sub>d</sub></em>417* <em>C<sub>r</sub></em> = <em>C<sub>d</sub></em>418*</pre>419* @since 1.4420*/421@Native public static final int DST = 9;422// Note that DST was added in 1.4 so it is numbered out of order...423424/**425* The source is composited over the destination426* (Porter-Duff Source Over Destination rule).427*<p>428* <em>F<sub>s</sub></em> = 1 and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>), thus:429*<pre>430* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em> + <em>A<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)431* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em> + <em>C<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)432*</pre>433*/434@Native public static final int SRC_OVER = 3;435436/**437* The destination is composited over the source and438* the result replaces the destination439* (Porter-Duff Destination Over Source rule).440*<p>441* <em>F<sub>s</sub></em> = (1-<em>A<sub>d</sub></em>) and <em>F<sub>d</sub></em> = 1, thus:442*<pre>443* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>A<sub>d</sub></em>444* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>C<sub>d</sub></em>445*</pre>446*/447@Native public static final int DST_OVER = 4;448449/**450* The part of the source lying inside of the destination replaces451* the destination452* (Porter-Duff Source In Destination rule).453*<p>454* <em>F<sub>s</sub></em> = <em>A<sub>d</sub></em> and <em>F<sub>d</sub></em> = 0, thus:455*<pre>456* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*<em>A<sub>d</sub></em>457* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*<em>A<sub>d</sub></em>458*</pre>459*/460@Native public static final int SRC_IN = 5;461462/**463* The part of the destination lying inside of the source464* replaces the destination465* (Porter-Duff Destination In Source rule).466*<p>467* <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = <em>A<sub>s</sub></em>, thus:468*<pre>469* <em>A<sub>r</sub></em> = <em>A<sub>d</sub></em>*<em>A<sub>s</sub></em>470* <em>C<sub>r</sub></em> = <em>C<sub>d</sub></em>*<em>A<sub>s</sub></em>471*</pre>472*/473@Native public static final int DST_IN = 6;474475/**476* The part of the source lying outside of the destination477* replaces the destination478* (Porter-Duff Source Held Out By Destination rule).479*<p>480* <em>F<sub>s</sub></em> = (1-<em>A<sub>d</sub></em>) and <em>F<sub>d</sub></em> = 0, thus:481*<pre>482* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>)483* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>)484*</pre>485*/486@Native public static final int SRC_OUT = 7;487488/**489* The part of the destination lying outside of the source490* replaces the destination491* (Porter-Duff Destination Held Out By Source rule).492*<p>493* <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>), thus:494*<pre>495* <em>A<sub>r</sub></em> = <em>A<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)496* <em>C<sub>r</sub></em> = <em>C<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)497*</pre>498*/499@Native public static final int DST_OUT = 8;500501// Rule 9 is DST which is defined above where it fits into the502// list logically, rather than numerically503//504// public static final int DST = 9;505506/**507* The part of the source lying inside of the destination508* is composited onto the destination509* (Porter-Duff Source Atop Destination rule).510*<p>511* <em>F<sub>s</sub></em> = <em>A<sub>d</sub></em> and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>), thus:512*<pre>513* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*<em>A<sub>d</sub></em> + <em>A<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>) = <em>A<sub>d</sub></em>514* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*<em>A<sub>d</sub></em> + <em>C<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)515*</pre>516* @since 1.4517*/518@Native public static final int SRC_ATOP = 10;519520/**521* The part of the destination lying inside of the source522* is composited over the source and replaces the destination523* (Porter-Duff Destination Atop Source rule).524*<p>525* <em>F<sub>s</sub></em> = (1-<em>A<sub>d</sub></em>) and <em>F<sub>d</sub></em> = <em>A<sub>s</sub></em>, thus:526*<pre>527* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>A<sub>d</sub></em>*<em>A<sub>s</sub></em> = <em>A<sub>s</sub></em>528* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>C<sub>d</sub></em>*<em>A<sub>s</sub></em>529*</pre>530* @since 1.4531*/532@Native public static final int DST_ATOP = 11;533534/**535* The part of the source that lies outside of the destination536* is combined with the part of the destination that lies outside537* of the source538* (Porter-Duff Source Xor Destination rule).539*<p>540* <em>F<sub>s</sub></em> = (1-<em>A<sub>d</sub></em>) and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>), thus:541*<pre>542* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>A<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)543* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>C<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)544*</pre>545* @since 1.4546*/547@Native public static final int XOR = 12;548549/**550* {@code AlphaComposite} object that implements the opaque CLEAR rule551* with an alpha of 1.0f.552* @see #CLEAR553*/554public static final AlphaComposite Clear = new AlphaComposite(CLEAR);555556/**557* {@code AlphaComposite} object that implements the opaque SRC rule558* with an alpha of 1.0f.559* @see #SRC560*/561public static final AlphaComposite Src = new AlphaComposite(SRC);562563/**564* {@code AlphaComposite} object that implements the opaque DST rule565* with an alpha of 1.0f.566* @see #DST567* @since 1.4568*/569public static final AlphaComposite Dst = new AlphaComposite(DST);570571/**572* {@code AlphaComposite} object that implements the opaque SRC_OVER rule573* with an alpha of 1.0f.574* @see #SRC_OVER575*/576public static final AlphaComposite SrcOver = new AlphaComposite(SRC_OVER);577578/**579* {@code AlphaComposite} object that implements the opaque DST_OVER rule580* with an alpha of 1.0f.581* @see #DST_OVER582*/583public static final AlphaComposite DstOver = new AlphaComposite(DST_OVER);584585/**586* {@code AlphaComposite} object that implements the opaque SRC_IN rule587* with an alpha of 1.0f.588* @see #SRC_IN589*/590public static final AlphaComposite SrcIn = new AlphaComposite(SRC_IN);591592/**593* {@code AlphaComposite} object that implements the opaque DST_IN rule594* with an alpha of 1.0f.595* @see #DST_IN596*/597public static final AlphaComposite DstIn = new AlphaComposite(DST_IN);598599/**600* {@code AlphaComposite} object that implements the opaque SRC_OUT rule601* with an alpha of 1.0f.602* @see #SRC_OUT603*/604public static final AlphaComposite SrcOut = new AlphaComposite(SRC_OUT);605606/**607* {@code AlphaComposite} object that implements the opaque DST_OUT rule608* with an alpha of 1.0f.609* @see #DST_OUT610*/611public static final AlphaComposite DstOut = new AlphaComposite(DST_OUT);612613/**614* {@code AlphaComposite} object that implements the opaque SRC_ATOP rule615* with an alpha of 1.0f.616* @see #SRC_ATOP617* @since 1.4618*/619public static final AlphaComposite SrcAtop = new AlphaComposite(SRC_ATOP);620621/**622* {@code AlphaComposite} object that implements the opaque DST_ATOP rule623* with an alpha of 1.0f.624* @see #DST_ATOP625* @since 1.4626*/627public static final AlphaComposite DstAtop = new AlphaComposite(DST_ATOP);628629/**630* {@code AlphaComposite} object that implements the opaque XOR rule631* with an alpha of 1.0f.632* @see #XOR633* @since 1.4634*/635public static final AlphaComposite Xor = new AlphaComposite(XOR);636637@Native private static final int MIN_RULE = CLEAR;638@Native private static final int MAX_RULE = XOR;639640float extraAlpha;641int rule;642643private AlphaComposite(int rule) {644this(rule, 1.0f);645}646647private AlphaComposite(int rule, float alpha) {648if (rule < MIN_RULE || rule > MAX_RULE) {649throw new IllegalArgumentException("unknown composite rule");650}651if (alpha >= 0.0f && alpha <= 1.0f) {652this.rule = rule;653this.extraAlpha = alpha;654} else {655throw new IllegalArgumentException("alpha value out of range");656}657}658659/**660* Creates an {@code AlphaComposite} object with the specified rule.661*662* @param rule the compositing rule663* @return the {@code AlphaComposite} object created664* @throws IllegalArgumentException if {@code rule} is not one of665* the following: {@link #CLEAR}, {@link #SRC}, {@link #DST},666* {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN},667* {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT},668* {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}669*/670public static AlphaComposite getInstance(int rule) {671switch (rule) {672case CLEAR:673return Clear;674case SRC:675return Src;676case DST:677return Dst;678case SRC_OVER:679return SrcOver;680case DST_OVER:681return DstOver;682case SRC_IN:683return SrcIn;684case DST_IN:685return DstIn;686case SRC_OUT:687return SrcOut;688case DST_OUT:689return DstOut;690case SRC_ATOP:691return SrcAtop;692case DST_ATOP:693return DstAtop;694case XOR:695return Xor;696default:697throw new IllegalArgumentException("unknown composite rule");698}699}700701/**702* Creates an {@code AlphaComposite} object with the specified rule and703* the constant alpha to multiply with the alpha of the source.704* The source is multiplied with the specified alpha before being composited705* with the destination.706*707* @param rule the compositing rule708* @param alpha the constant alpha to be multiplied with the alpha of709* the source. {@code alpha} must be a floating point number in the710* inclusive range [0.0, 1.0].711* @return the {@code AlphaComposite} object created712* @throws IllegalArgumentException if713* {@code alpha} is less than 0.0 or greater than 1.0, or if714* {@code rule} is not one of715* the following: {@link #CLEAR}, {@link #SRC}, {@link #DST},716* {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN},717* {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT},718* {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}719*/720public static AlphaComposite getInstance(int rule, float alpha) {721if (alpha == 1.0f) {722return getInstance(rule);723}724return new AlphaComposite(rule, alpha);725}726727/**728* Creates a context for the compositing operation.729* The context contains state that is used in performing730* the compositing operation.731* @param srcColorModel the {@link ColorModel} of the source732* @param dstColorModel the {@code ColorModel} of the destination733* @return the {@code CompositeContext} object to be used to perform734* compositing operations.735*/736public CompositeContext createContext(ColorModel srcColorModel,737ColorModel dstColorModel,738RenderingHints hints) {739return new SunCompositeContext(this, srcColorModel, dstColorModel);740}741742/**743* Returns the alpha value of this {@code AlphaComposite}. If this744* {@code AlphaComposite} does not have an alpha value, 1.0 is returned.745* @return the alpha value of this {@code AlphaComposite}.746*/747public float getAlpha() {748return extraAlpha;749}750751/**752* Returns the compositing rule of this {@code AlphaComposite}.753* @return the compositing rule of this {@code AlphaComposite}.754*/755public int getRule() {756return rule;757}758759/**760* Returns a similar {@code AlphaComposite} object that uses761* the specified compositing rule.762* If this object already uses the specified compositing rule,763* this object is returned.764* @return an {@code AlphaComposite} object derived from765* this object that uses the specified compositing rule.766* @param rule the compositing rule767* @throws IllegalArgumentException if768* {@code rule} is not one of769* the following: {@link #CLEAR}, {@link #SRC}, {@link #DST},770* {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN},771* {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT},772* {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}773* @since 1.6774*/775public AlphaComposite derive(int rule) {776return (this.rule == rule)777? this778: getInstance(rule, this.extraAlpha);779}780781/**782* Returns a similar {@code AlphaComposite} object that uses783* the specified alpha value.784* If this object already has the specified alpha value,785* this object is returned.786* @return an {@code AlphaComposite} object derived from787* this object that uses the specified alpha value.788* @param alpha the constant alpha to be multiplied with the alpha of789* the source. {@code alpha} must be a floating point number in the790* inclusive range [0.0, 1.0].791* @throws IllegalArgumentException if792* {@code alpha} is less than 0.0 or greater than 1.0793* @since 1.6794*/795public AlphaComposite derive(float alpha) {796return (this.extraAlpha == alpha)797? this798: getInstance(this.rule, alpha);799}800801/**802* Returns the hashcode for this composite.803* @return a hash code for this composite.804*/805public int hashCode() {806return (Float.floatToIntBits(extraAlpha) * 31 + rule);807}808809/**810* Determines whether the specified object is equal to this811* {@code AlphaComposite}.812* <p>813* The result is {@code true} if and only if814* the argument is not {@code null} and is an815* {@code AlphaComposite} object that has the same816* compositing rule and alpha value as this object.817*818* @param obj the {@code Object} to test for equality819* @return {@code true} if {@code obj} equals this820* {@code AlphaComposite}; {@code false} otherwise.821*/822public boolean equals(Object obj) {823if (!(obj instanceof AlphaComposite)) {824return false;825}826827AlphaComposite ac = (AlphaComposite) obj;828829if (rule != ac.rule) {830return false;831}832833if (extraAlpha != ac.extraAlpha) {834return false;835}836837return true;838}839840}841842843