001/*
002Copyright 2006 Jerry Huxtable
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008   http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015*/
016
017package com.jhlabs.composite;
018
019import java.awt.*;
020import java.awt.image.*;
021
022public final class MiscComposite implements Composite {
023
024        public final static int BLEND = 0;
025        public final static int ADD = 1;
026        public final static int SUBTRACT = 2;
027        public final static int DIFFERENCE = 3;
028
029        public final static int MULTIPLY = 4;
030        public final static int DARKEN = 5;
031        public final static int BURN = 6;
032        public final static int COLOR_BURN = 7;
033
034        public final static int SCREEN = 8;
035        public final static int LIGHTEN = 9;
036        public final static int DODGE = 10;
037        public final static int COLOR_DODGE = 11;
038
039        public final static int HUE = 12;
040        public final static int SATURATION = 13;
041        public final static int VALUE = 14;
042        public final static int COLOR = 15;
043
044        public final static int OVERLAY = 16;
045        public final static int SOFT_LIGHT = 17;
046        public final static int HARD_LIGHT = 18;
047        public final static int PIN_LIGHT = 19;
048
049        public final static int EXCLUSION = 20;
050        public final static int NEGATION = 21;
051        public final static int AVERAGE = 22;
052
053        public final static int STENCIL = 23;
054        public final static int SILHOUETTE = 24;
055
056        private static final int MIN_RULE = BLEND;
057        private static final int MAX_RULE = SILHOUETTE;
058
059        public static String[] RULE_NAMES = {
060                "Normal",
061                "Add",
062                "Subtract",
063                "Difference",
064
065                "Multiply",
066                "Darken",
067                "Burn",
068                "Color Burn",
069                
070                "Screen",
071                "Lighten",
072                "Dodge",
073                "Color Dodge",
074
075                "Hue",
076                "Saturation",
077                "Brightness",
078                "Color",
079                
080                "Overlay",
081                "Soft Light",
082                "Hard Light",
083                "Pin Light",
084
085                "Exclusion",
086                "Negation",
087                "Average",
088
089                "Stencil",
090                "Silhouette",
091        };
092
093        protected float extraAlpha;
094        protected int rule;
095
096        private MiscComposite(int rule) {
097                this(rule, 1.0f);
098        }
099
100        private MiscComposite(int rule, float alpha) {
101                if (alpha < 0.0f || alpha > 1.0f)
102                        throw new IllegalArgumentException("alpha value out of range");
103                if (rule < MIN_RULE || rule > MAX_RULE)
104                        throw new IllegalArgumentException("unknown composite rule");
105                this.rule = rule;
106                this.extraAlpha = alpha;
107        }
108
109        public static Composite getInstance(int rule, float alpha) {
110                switch ( rule ) {
111                case MiscComposite.BLEND:
112                        return AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha );
113                case MiscComposite.ADD:
114                        return new AddComposite( alpha );
115                case MiscComposite.SUBTRACT:
116                        return new SubtractComposite( alpha );
117                case MiscComposite.DIFFERENCE:
118                        return new DifferenceComposite( alpha );
119                case MiscComposite.MULTIPLY:
120                        return new MultiplyComposite( alpha );
121                case MiscComposite.DARKEN:
122                        return new DarkenComposite( alpha );
123                case MiscComposite.BURN:
124                        return new BurnComposite( alpha );
125                case MiscComposite.COLOR_BURN:
126                        return new ColorBurnComposite( alpha );
127                case MiscComposite.SCREEN:
128                        return new ScreenComposite( alpha );
129                case MiscComposite.LIGHTEN:
130                        return new LightenComposite( alpha );
131                case MiscComposite.DODGE:
132                        return new DodgeComposite( alpha );
133                case MiscComposite.COLOR_DODGE:
134                        return new ColorDodgeComposite( alpha );
135                case MiscComposite.HUE:
136                        return new HueComposite( alpha );
137                case MiscComposite.SATURATION:
138                        return new SaturationComposite( alpha );
139                case MiscComposite.VALUE:
140                        return new ValueComposite( alpha );
141                case MiscComposite.COLOR:
142                        return new ColorComposite( alpha );
143                case MiscComposite.OVERLAY:
144                        return new OverlayComposite( alpha );
145                case MiscComposite.SOFT_LIGHT:
146                        return new SoftLightComposite( alpha );
147                case MiscComposite.HARD_LIGHT:
148                        return new HardLightComposite( alpha );
149                case MiscComposite.PIN_LIGHT:
150                        return new PinLightComposite( alpha );
151                case MiscComposite.EXCLUSION:
152                        return new ExclusionComposite( alpha );
153                case MiscComposite.NEGATION:
154                        return new NegationComposite( alpha );
155                case MiscComposite.AVERAGE:
156                        return new AverageComposite( alpha );
157                case MiscComposite.STENCIL:
158                        return AlphaComposite.getInstance( AlphaComposite.DST_IN, alpha );
159                case MiscComposite.SILHOUETTE:
160                        return AlphaComposite.getInstance( AlphaComposite.DST_OUT, alpha );
161                }
162                return new MiscComposite(rule, alpha);
163        }
164
165        public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints) {
166                return new MiscCompositeContext( rule, extraAlpha, srcColorModel, dstColorModel );
167        }
168
169        public float getAlpha() {
170                return extraAlpha;
171        }
172
173        public int getRule() {
174                return rule;
175        }
176
177        public int hashCode() {
178                return (Float.floatToIntBits(extraAlpha) * 31 + rule);
179        }
180
181        public boolean equals(Object o) {
182                if (!(o instanceof MiscComposite))
183                        return false;
184                MiscComposite c = (MiscComposite)o;
185
186                if (rule != c.rule)
187                        return false;
188                if (extraAlpha != c.extraAlpha)
189                        return false;
190                return true;
191        }
192                        
193}