Home | History | Annotate | Download | only in awt
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 /**
     18  * @author Igor V. Stolyarov
     19  * @version $Revision$
     20  */
     21 
     22 package java.awt;
     23 
     24 import java.awt.Composite;
     25 import java.awt.CompositeContext;
     26 import java.awt.RenderingHints;
     27 import java.awt.image.ColorModel;
     28 
     29 import org.apache.harmony.awt.gl.ICompositeContext;
     30 import org.apache.harmony.awt.internal.nls.Messages;
     31 
     32 /**
     33  * The AlphaComposite class defines a basic alpha compositing rules for
     34  * combining source and destination colors to achieve blending and transparency
     35  * effects with graphics and images.
     36  *
     37  * @since Android 1.0
     38  */
     39 public final class AlphaComposite implements Composite {
     40 
     41     /**
     42      * The Constant CLEAR indicates that both the color and the alpha of the
     43      * destination are cleared (Porter-Duff Clear rule).
     44      */
     45     public static final int CLEAR = 1;
     46 
     47     /**
     48      * The Constant SRC indicates that the source is copied to the destination
     49      * (Porter-Duff Source rule).
     50      */
     51     public static final int SRC = 2;
     52 
     53     /**
     54      * The Constant DST indicates that the destination is left untouched
     55      * (Porter-Duff Destination rule).
     56      */
     57     public static final int DST = 9;
     58 
     59     /**
     60      * The Constant SRC_OVER indicates that the source is composited over the
     61      * destination (Porter-Duff Source Over Destination rule).
     62      */
     63     public static final int SRC_OVER = 3;
     64 
     65     /**
     66      * The Constant DST_OVER indicates that The destination is composited over
     67      * the source and the result replaces the destination (Porter-Duff
     68      * Destination Over Source rule).
     69      */
     70     public static final int DST_OVER = 4;
     71 
     72     /**
     73      * The Constant SRC_IN indicates that the part of the source lying inside of
     74      * the destination replaces the destination (Porter-Duff Source In
     75      * Destination rule).
     76      */
     77     public static final int SRC_IN = 5;
     78 
     79     /**
     80      * The Constant DST_IN indicates that the part of the destination lying
     81      * inside of the source replaces the destination (Porter-Duff Destination In
     82      * Source rule).
     83      */
     84     public static final int DST_IN = 6;
     85 
     86     /**
     87      * The Constant SRC_OUT indicates that the part of the source lying outside
     88      * of the destination replaces the destination (Porter-Duff Source Held Out
     89      * By Destination rule).
     90      */
     91     public static final int SRC_OUT = 7;
     92 
     93     /**
     94      * The Constant DST_OUT indicates that the part of the destination lying
     95      * outside of the source replaces the destination (Porter-Duff Destination
     96      * Held Out By Source rule).
     97      */
     98     public static final int DST_OUT = 8;
     99 
    100     /**
    101      * The Constant SRC_ATOP indicates that the part of the source lying inside
    102      * of the destination is composited onto the destination (Porter-Duff Source
    103      * Atop Destination rule).
    104      */
    105     public static final int SRC_ATOP = 10;
    106 
    107     /**
    108      * The Constant DST_ATOP indicates that the part of the destination lying
    109      * inside of the source is composited over the source and replaces the
    110      * destination (Porter-Duff Destination Atop Source rule).
    111      */
    112     public static final int DST_ATOP = 11;
    113 
    114     /**
    115      * The Constant XOR indicates that the part of the source that lies outside
    116      * of the destination is combined with the part of the destination that lies
    117      * outside of the source (Porter-Duff Source Xor Destination rule).
    118      */
    119     public static final int XOR = 12;
    120 
    121     /**
    122      * AlphaComposite object with the opaque CLEAR rule and an alpha of 1.0f.
    123      */
    124     public static final AlphaComposite Clear = new AlphaComposite(CLEAR);
    125 
    126     /**
    127      * AlphaComposite object with the opaque SRC rule and an alpha of 1.0f.
    128      */
    129     public static final AlphaComposite Src = new AlphaComposite(SRC);
    130 
    131     /**
    132      * AlphaComposite object with the opaque DST rule and an alpha of 1.0f.
    133      */
    134     public static final AlphaComposite Dst = new AlphaComposite(DST);
    135 
    136     /**
    137      * AlphaComposite object with the opaque SRC_OVER rule and an alpha of 1.0f.
    138      */
    139     public static final AlphaComposite SrcOver = new AlphaComposite(SRC_OVER);
    140 
    141     /**
    142      * AlphaComposite object with the opaque DST_OVER rule and an alpha of 1.0f.
    143      */
    144     public static final AlphaComposite DstOver = new AlphaComposite(DST_OVER);
    145 
    146     /**
    147      * AlphaComposite object with the opaque SRC_IN rule and an alpha of 1.0f.
    148      */
    149     public static final AlphaComposite SrcIn = new AlphaComposite(SRC_IN);
    150 
    151     /**
    152      * AlphaComposite object with the opaque DST_IN rule and an alpha of 1.0f.
    153      */
    154     public static final AlphaComposite DstIn = new AlphaComposite(DST_IN);
    155 
    156     /**
    157      * AlphaComposite object with the opaque SRC_OUT rule and an alpha of 1.0f.
    158      */
    159     public static final AlphaComposite SrcOut = new AlphaComposite(SRC_OUT);
    160 
    161     /**
    162      * AlphaComposite object with the opaque DST_OUT rule and an alpha of 1.0f.
    163      */
    164     public static final AlphaComposite DstOut = new AlphaComposite(DST_OUT);
    165 
    166     /**
    167      * AlphaComposite object with the opaque SRC_ATOP rule and an alpha of 1.0f.
    168      */
    169     public static final AlphaComposite SrcAtop = new AlphaComposite(SRC_ATOP);
    170 
    171     /**
    172      * AlphaComposite object with the opaque DST_ATOP rule and an alpha of 1.0f.
    173      */
    174     public static final AlphaComposite DstAtop = new AlphaComposite(DST_ATOP);
    175 
    176     /**
    177      * AlphaComposite object with the opaque XOR rule and an alpha of 1.0f.
    178      */
    179     public static final AlphaComposite Xor = new AlphaComposite(XOR);
    180 
    181     /**
    182      * The rule.
    183      */
    184     private int rule;
    185 
    186     /**
    187      * The alpha.
    188      */
    189     private float alpha;
    190 
    191     /**
    192      * Instantiates a new alpha composite. Creates a context for the compositing
    193      * operation. The context contains state that is used in performing the
    194      * compositing operation.
    195      *
    196      * @param rule
    197      *            the rule.
    198      * @param alpha
    199      *            the alpha.
    200      */
    201     private AlphaComposite(int rule, float alpha) {
    202         if (rule < CLEAR || rule > XOR) {
    203             // awt.11D=Unknown rule
    204             throw new IllegalArgumentException(Messages.getString("awt.11D")); //$NON-NLS-1$
    205         }
    206         if (alpha < 0.0f || alpha > 1.0f) {
    207             // awt.11E=Wrong alpha value
    208             throw new IllegalArgumentException(Messages.getString("awt.11E")); //$NON-NLS-1$
    209         }
    210 
    211         this.rule = rule;
    212         this.alpha = alpha;
    213     }
    214 
    215     /**
    216      * Instantiates a new alpha composite.
    217      *
    218      * @param rule
    219      *            the rule.
    220      */
    221     private AlphaComposite(int rule) {
    222         this(rule, 1.0f);
    223     }
    224 
    225     /**
    226      * Creates a CompositeContext object with the specified source ColorModel,
    227      * destination ColorModel and RenderingHints parameters for a composing
    228      * operation.
    229      *
    230      * @param srcColorModel
    231      *            the source's ColorModel.
    232      * @param dstColorModel
    233      *            the destination's ColorModel.
    234      * @param hints
    235      *            the RenderingHints object.
    236      * @return the CompositeContext object.
    237      * @see java.awt.Composite#createContext(java.awt.image.ColorModel,
    238      *      java.awt.image.ColorModel, java.awt.RenderingHints)
    239      */
    240     public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel,
    241             RenderingHints hints) {
    242         return new ICompositeContext(this, srcColorModel, dstColorModel);
    243     }
    244 
    245     /**
    246      * Compares the AlphaComposite object with the specified object.
    247      *
    248      * @param obj
    249      *            the Object to be compared.
    250      * @return true, if the AlphaComposite object is equal to the specified
    251      *         object.
    252      */
    253     @Override
    254     public boolean equals(Object obj) {
    255         if (!(obj instanceof AlphaComposite)) {
    256             return false;
    257         }
    258         AlphaComposite other = (AlphaComposite)obj;
    259         return (this.rule == other.getRule() && this.alpha == other.getAlpha());
    260     }
    261 
    262     /**
    263      * Returns the hash code of the AlphaComposite object.
    264      *
    265      * @return the hash code of the AlphaComposite object.
    266      */
    267     @Override
    268     public int hashCode() {
    269         int hash = Float.floatToIntBits(alpha);
    270         int tmp = hash >>> 24;
    271         hash <<= 8;
    272         hash |= tmp;
    273         hash ^= rule;
    274         return hash;
    275     }
    276 
    277     /**
    278      * Gets the compositing rule of this AlphaComposite object.
    279      *
    280      * @return the compositing rule of this AlphaComposite object.
    281      */
    282     public int getRule() {
    283         return rule;
    284     }
    285 
    286     /**
    287      * Gets the alpha value of this AlphaComposite object; returns 1.0 if this
    288      * AlphaComposite object doesn't have alpha value.
    289      *
    290      * @return the alpha value of this AlphaComposite object or 1.0 if this
    291      *         AlphaComposite object doesn't have alpha value.
    292      */
    293     public float getAlpha() {
    294         return alpha;
    295     }
    296 
    297     /**
    298      * Gets the AlphaComposite instance with the specified rule and alpha value.
    299      *
    300      * @param rule
    301      *            the compositing rule.
    302      * @param alpha
    303      *            the alpha value.
    304      * @return the AlphaComposite instance.
    305      */
    306     public static AlphaComposite getInstance(int rule, float alpha) {
    307         if (alpha == 1.0f) {
    308             return getInstance(rule);
    309         }
    310         return new AlphaComposite(rule, alpha);
    311     }
    312 
    313     /**
    314      * Gets the AlphaComposite instance with the specified rule.
    315      *
    316      * @param rule
    317      *            the compositing rule.
    318      * @return the AlphaComposite instance.
    319      */
    320     public static AlphaComposite getInstance(int rule) {
    321         switch (rule) {
    322             case CLEAR:
    323                 return Clear;
    324             case SRC:
    325                 return Src;
    326             case DST:
    327                 return Dst;
    328             case SRC_OVER:
    329                 return SrcOver;
    330             case DST_OVER:
    331                 return DstOver;
    332             case SRC_IN:
    333                 return SrcIn;
    334             case DST_IN:
    335                 return DstIn;
    336             case SRC_OUT:
    337                 return SrcOut;
    338             case DST_OUT:
    339                 return DstOut;
    340             case SRC_ATOP:
    341                 return SrcAtop;
    342             case DST_ATOP:
    343                 return DstAtop;
    344             case XOR:
    345                 return Xor;
    346             default:
    347                 // awt.11D=Unknown rule
    348                 throw new IllegalArgumentException(Messages.getString("awt.11D")); //$NON-NLS-1$
    349         }
    350     }
    351 
    352 }
    353