/**
* eAdventure (formerly <e-Adventure> and <e-Game>) is a research project of the
* <e-UCM> research group.
*
* Copyright 2005-2010 <e-UCM> research group.
*
* You can access a list of all the contributors to eAdventure at:
* http://e-adventure.e-ucm.es/contributors
*
* <e-UCM> is a research group of the Department of Software Engineering
* and Artificial Intelligence at the Complutense University of Madrid
* (School of Computer Science).
*
* C Profesor Jose Garcia Santesmases sn,
* 28040 Madrid (Madrid), Spain.
*
* For more info please visit: <http://e-adventure.e-ucm.es> or
* <http://www.e-ucm.es>
*
* ****************************************************************************
*
* This file is part of eAdventure, version 2.0
*
* eAdventure is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* eAdventure is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with eAdventure. If not, see <http://www.gnu.org/licenses/>.
*/
package es.eucm.ead.model.params.fills;
import es.eucm.ead.model.params.AbstractParam;
import es.eucm.ead.model.params.paint.EAdFill;
/**
* <p>
* This class represents a basic color in the eAdventure model.
* </p>
*
*/
public final class ColorFill extends AbstractParam implements EAdFill {
/**
* White EAdColor
*/
public static final ColorFill WHITE = new ColorFill(255, 255, 255);
/**
* Black EAdColor
*/
public static final ColorFill BLACK = new ColorFill(0, 0, 0);
/**
* Transparent EAdColor
*/
public static final ColorFill TRANSPARENT = new ColorFill(0, 0, 0, 0);
/**
* Red EAdColor
*/
public static final ColorFill RED = new ColorFill(255, 0, 0);
/**
* Blue EAdColor
*/
public static final ColorFill BLUE = new ColorFill(0, 0, 255);
/**
* Green EAdColor
*/
public static final ColorFill GREEN = new ColorFill(0, 255, 0);
/**
* Cyan EAdColor
*/
public static final ColorFill CYAN = new ColorFill(0, 255, 255);
/**
* Yellow EAdColor
*/
public static final ColorFill YELLOW = new ColorFill(255, 255, 0);
/**
* Orange EAdColor
*/
public static final ColorFill ORANGE = new ColorFill(255, 125, 0);
/**
* Magenta EAdColor
*/
public static final ColorFill MAGENTA = new ColorFill(255, 0, 255);
/**
* Gray EAdColor
*/
public static final ColorFill GRAY = new ColorFill(125, 125, 125);
/**
* Dark gray EAdColor
*/
public static final ColorFill DARK_GRAY = new ColorFill(62, 62, 62);
/**
* Light gray EAdColor
*/
public static final ColorFill LIGHT_GRAY = new ColorFill(200, 200, 200);
/**
* Brown EAdColor
*/
public static final ColorFill BROWN = new ColorFill(200, 50, 0);
/**
* Dark brown EAdColor
*/
public static final ColorFill DARK_BROWN = new ColorFill(100, 50, 0);
/**
* Light brown EAdColor
*/
public static final ColorFill LIGHT_BROWN = new ColorFill(150, 75, 0);
/**
* The red value of the color
*/
private int red;
/**
* The green value of the color
*/
private int green;
/**
* The blue value of the color
*/
private int blue;
/**
* The alpha value of the color
*/
private int alpha;
/**
* Creates a new white color, with the values of white
*/
public ColorFill() {
this(0, 0, 0, 255);
}
/**
* Create a new color with the given RGB values
*
* @param red
* The red value
* @param green
* The green value
* @param blue
* The blue value
*/
public ColorFill(int red, int green, int blue) {
this(red, green, blue, 255);
}
/**
* Create a new color with the given RGBA values
*
* @param red
* The red value
* @param green
* The green value
* @param blue
* The blue value
* @param alpha
* The alpha value
*/
public ColorFill(int red, int green, int blue, int alpha) {
setRed(red);
setGreen(green);
setBlue(blue);
setAlpha(alpha);
}
/**
* Parse the value of a color from a string in the hexadecimal form
* {@code 0xRRGGBBAA}.
*
* @param data String with the color value
*/
public ColorFill(String data) {
parse(data);
}
/**
* Sets the red, green, blue, alpha color components. Values are normalized
* to be set between 0 and 255
*
* @param red
* @param green
* @param blue
* @param alpha
*/
public void setRGBA(int red, int green, int blue, int alpha) {
setRed(red);
setGreen(green);
setBlue(blue);
setAlpha(alpha);
}
/**
* Sets the red, green, blue color components, leaving the alpha as it is.
* Same effect as calling {@link ColorFill#setRGBA(int, int, int, int)} with
* {@link ColorFill#getAlpha()} as fourth parameter
*
* @param red
* @param green
* @param blue
*/
public void setRGB(int red, int green, int blue) {
this.setRGBA(red, green, blue, alpha);
}
/**
* Returns the red value
*
* @return the red value
*/
public int getRed() {
return red;
}
/**
* Sets the red value
*
* @param red
* the red value of the color
*/
public void setRed(int red) {
this.red = normalize(red);
}
/**
* Returns the green value
*
* @return the green value of the color
*/
public int getGreen() {
return green;
}
/**
* Sets the green value
*
* @param green
* the green value of the color
*/
public void setGreen(int green) {
this.green = normalize(green);
}
/**
* Returns the blue value
*
* @return the blue value of the color
*/
public int getBlue() {
return blue;
}
/**
* Sets the blue value
*
* @param blue
* the blue value of the color
*/
public void setBlue(int blue) {
this.blue = normalize(blue);
}
/**
* Returns the alpha value
*
* @return the alpha value of the color
*/
public int getAlpha() {
return alpha;
}
/**
* Sets the alpha value of the color
*
* @param alpha
* the alpha value of the color
*/
public void setAlpha(int alpha) {
this.alpha = normalize(alpha);
}
private int normalize(int value) {
if (value < 0) {
return 0;
} else if (value > 255) {
return 255;
} else {
return value;
}
}
@Override
public String toString() {
return toStringData();
}
private String valueToString(int value) {
return Integer.toHexString(256 + value).substring(1);
}
@Override
public String toStringData() {
return "0x" + valueToString(red) + valueToString(green)
+ valueToString(blue) + valueToString(alpha);
}
@Override
public boolean parse(String data) {
boolean error = false;
if (data != null && data.length() == 10) {
try {
setRed(Integer.parseInt(data.substring(2, 4), 16));
setGreen(Integer.parseInt(data.substring(4, 6), 16));
setBlue(Integer.parseInt(data.substring(6, 8), 16));
setAlpha(Integer.parseInt(data.substring(8, 10), 16));
} catch (NumberFormatException e) {
error = true;
}
} else {
error = true;
}
if (error) {
red = blue = green = 0;
alpha = 255;
}
return !error;
}
@Override
public EAdFill getBorder() {
return null;
}
@Override
public EAdFill getFill() {
return this;
}
@Override
public int getBorderWidth() {
return 0;
}
@Override
public boolean equals(Object object) {
if (object == null || !(object instanceof ColorFill)) {
return false;
}
ColorFill paint = (ColorFill) object;
return paint.alpha == alpha && paint.blue == blue
&& paint.green == green && paint.red == red;
}
@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + this.red;
hash = 31 * hash + this.green;
hash = 31 * hash + this.blue;
hash = 31 * hash + this.alpha;
return hash;
}
}