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.image;
018
019import java.awt.*;
020import java.awt.image.*;
021
022/**
023 * Sets the opacity (alpha) of every pixel in an image to a constant value.
024 */
025public class OpacityFilter extends PointFilter {
026        
027        private int opacity;
028        private int opacity24;
029
030        /**
031         * Construct an OpacityFilter with 50% opacity.
032         */
033        public OpacityFilter() {
034                this(0x88);
035        }
036
037        /**
038         * Construct an OpacityFilter with the given opacity (alpha).
039         * @param opacity the opacity (alpha) in the range 0..255
040         */
041        public OpacityFilter(int opacity) {
042                setOpacity(opacity);
043        }
044
045        /**
046         * Set the opacity.
047         * @param opacity the opacity (alpha) in the range 0..255
048     * @see #getOpacity
049         */
050        public void setOpacity(int opacity) {
051                this.opacity = opacity;
052                opacity24 = opacity << 24;
053        }
054        
055        /**
056         * Get the opacity setting.
057         * @return the opacity
058     * @see #setOpacity
059         */
060        public int getOpacity() {
061                return opacity;
062        }
063        
064        public int filterRGB(int x, int y, int rgb) {
065                if ((rgb & 0xff000000) != 0)
066                        return (rgb & 0xffffff) | opacity24;
067                return rgb;
068        }
069
070        public String toString() {
071                return "Colors/Transparency...";
072        }
073
074}
075