001/*
002 * $Id: BaseColor.java 4847 2011-05-05 19:46:13Z redlab_b $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text;
045
046import com.itextpdf.text.error_messages.MessageLocalization;
047
048/**
049 *
050 * @author psoares
051 */
052public class BaseColor {
053    public static final BaseColor WHITE = new BaseColor(255, 255, 255);
054    public static final BaseColor LIGHT_GRAY = new BaseColor(192, 192, 192);
055    public static final BaseColor GRAY = new BaseColor(128, 128, 128);
056    public static final BaseColor DARK_GRAY = new BaseColor(64, 64, 64);
057    public static final BaseColor BLACK = new BaseColor(0, 0, 0);
058    public static final BaseColor RED = new BaseColor(255, 0, 0);
059    public static final BaseColor PINK = new BaseColor(255, 175, 175);
060    public static final BaseColor ORANGE = new BaseColor(255, 200, 0);
061    public static final BaseColor YELLOW = new BaseColor(255, 255, 0);
062    public static final BaseColor GREEN = new BaseColor(0, 255, 0);
063    public static final BaseColor MAGENTA = new BaseColor(255, 0, 255);
064    public static final BaseColor CYAN = new BaseColor(0, 255, 255);
065    public static final BaseColor BLUE = new BaseColor(0, 0, 255);
066    private static final double FACTOR = 0.7;
067    private final int value;
068
069    /**
070     * Construct a new BaseColor.
071     * @param red the value for the red gamma
072     * @param green the value for the green gamma
073     * @param blue the value for the blue gamma
074     * @param alpha the value for the alpha gamma
075     */
076    public BaseColor(final int red, final int green, final int blue, final int alpha) {
077        validate(red);
078        validate(green);
079        validate(blue);
080        validate(alpha);
081        value = ((alpha & 0xFF) << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0);
082    }
083
084    /**
085     * @param red
086     * @param green
087     * @param blue
088     */
089    public BaseColor(final int red, final int green, final int blue) {
090        this(red, green, blue, 255);
091    }
092
093    /**
094     * Construct a BaseColor with float values.
095     * @param red
096     * @param green
097     * @param blue
098     * @param alpha
099     */
100    public BaseColor(final float red, final float green, final float blue, final float alpha) {
101        this((int)(red * 255 + .5), (int)(green * 255 + .5), (int)(blue * 255 + .5), (int)(alpha * 255 + .5));
102    }
103
104    /**
105     * Construct a BaseColor with float values.
106     * @param red
107     * @param green
108     * @param blue
109     */
110    public BaseColor(final float red, final float green, final float blue) {
111        this(red, green, blue, 1f);
112    }
113    /**
114     * Construct a BaseColor by setting the combined value.
115     * @param argb
116     */
117    public BaseColor(final int argb) {
118        value = argb;
119    }
120
121    /**
122     * Construct a new BaseColor from a java.awt.Color
123     * @param color
124     */
125    public BaseColor(final java.awt.Color color) {
126        value = color.getRGB();
127    }
128
129    /**
130     * @return the combined color value
131     */
132    public int getRGB() {
133        return value;
134    }
135    /**
136     *
137     * @return the value for red
138     */
139    public int getRed() {
140        return (getRGB() >> 16) & 0xFF;
141    }
142    /**
143     *
144     * @return the value for green
145     */
146    public int getGreen() {
147        return (getRGB() >> 8) & 0xFF;
148    }
149    /**
150     *
151     * @return the value for blue
152     */
153    public int getBlue() {
154        return (getRGB() >> 0) & 0xFF;
155    }
156    /**
157     *
158     * @return the value for the alpha channel
159     */
160    public int getAlpha() {
161        return (getRGB() >> 24) & 0xff;
162    }
163
164    /**
165     * Make this BaseColor brighter. Factor used is 0.7.
166     * @return the new BaseColor
167     */
168    public BaseColor brighter() {
169        int r = getRed();
170        int g = getGreen();
171        int b = getBlue();
172
173        int i = (int) (1.0 / (1.0 - FACTOR));
174        if (r == 0 && g == 0 && b == 0) {
175            return new BaseColor(i, i, i);
176        }
177        if (r > 0 && r < i)
178            r = i;
179        if (g > 0 && g < i)
180            g = i;
181        if (b > 0 && b < i)
182            b = i;
183
184        return new BaseColor(Math.min((int) (r / FACTOR), 255),
185                Math.min((int) (g / FACTOR), 255),
186                Math.min((int) (b / FACTOR), 255));
187    }
188
189    /**
190     * Make this color darker. Factor used is 0.7
191     * @return the new BaseColor
192     */
193    public BaseColor darker() {
194        return new BaseColor(Math.max((int) (getRed() * FACTOR), 0),
195                Math.max((int) (getGreen() * FACTOR), 0),
196                Math.max((int) (getBlue() * FACTOR), 0));
197    }
198
199    @Override
200    public boolean equals(final Object obj) {
201        return obj instanceof BaseColor && ((BaseColor) obj).value == this.value;
202    }
203
204    @Override
205    public int hashCode() {
206        return value;
207    }
208
209
210    private static void validate(final int value) {
211        if (value < 0 || value > 255)
212            throw new IllegalArgumentException(MessageLocalization.getComposedMessage("color.value.outside.range.0.255"));
213    }
214
215    /* (non-Javadoc)
216     * @see java.lang.Object#toString()
217     */
218    @Override
219    public String toString() {
220        return "Color value["+value+"]";
221    }
222}