/*
 * FSSolidFill.java
 * Transform
 * 
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.flagstone.transform;

/**
 * FSSolidFill defines a solid colour that is used to fill an enclosed area in a
 * shape.
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the data structure when it is encoded.</td>
 * </tr>
 * 
 * <tr>
 * <td>color</td>
 * <td>The colour used to fill the area of the shape.</td>
 * </tr>
 * 
 * </table>
 * 
 * <p>
 * The parent class, FSFillStyle allows the type to be set. This should only be
 * used for manipulating bitmap and gradient fill styles.
 * </p>
 * 
 * <p>
 * Fill styles are selected using the FSShapeStyle class and are referenced by
 * their ordinal position in the array of available styles. The ordinal position
 * in the array is used rather than the index (which starts at 0). An index of 0
 * is used to specify that a fill style is not selected.
 * </p>
 * 
 * <h1 class="datasheet">Examples</h1>
 * 
 * <p>
 * An FSSolidFill object is created by specifying the colour to fill the shape:
 * </p>
 * 
 * <pre>
 * FSColor red = new FSColor(255, 0, 0);
 * 
 * FSSolidFill fill = new FSSolidFill(red);
 * </pre>
 * 
 * <p>
 * Shapes can be filled with transparent colours - rather than specifying that
 * the entire shape is transparent when adding it to the display list using the
 * FSPlaceObject2 class:
 * 
 * <pre>
 * FSColor transparentRed = new FSColor(255, 0, 0, 128);
 * 
 * FSSolidFill transparentFill = new FSSolidFill(transparentRed);
 * </pre>
 * 
 * <p>
 * Note however that the object used to create the shape definition determines
 * whether the alpha channel is encoded. Simply specifying the level of
 * transparency in the FSColor object is not sufficient. Transparent colours are
 * only supports in Flash 3 onwards.
 * </p>
 * 
 * <p>
 * To reference different colours to fill a shape use the ordinal position of
 * the FSSolidFill object in the array of fill styles defined in the shape:
 * </p>
 * 
 * <pre>
 *  ArrayList fillStyles = new ArrayList();
 *  fillStyles.add(new FSSolidFill(new FSColor(255, 0, 0))); // red 
 *  fillStyles.add(new FSSolidFill(new FSColor(0, 255, 0))); // green
 * 
 *  FSShape shape = new FSShape(); \n&quot;; 
 * 
 *  // The fill styles are referenced by their ordinal position in the 
 *  // array of styles.
 *  int redStyle = 1; 
 *  int greenStyle = 2; 
 * 
 *  // fill the enclosed area with red.
 *  shape.add(new FSShapeStyle(0, redStyle, 0));
 *  ...
 *  // now fill the remaining area with green.
 *  shape.add(new FSShapeStyle(0, greenStyle, 0));
 * 
 * <h1 class="datasheet">
 * History
 * </h1>
 * <p>
 * The FSSolidFill class represents the FillStyle structure from the 
 * Macromedia Flash (SWF) File Format Specification. It was introduced in 
 * Flash 1.
 * </p>
 */
public class FSSolidFill extends FSFillStyle
{
	private FSColor color = null;

	/**
	 * Construct an FSSolidFill object, initialising it with values decoded from
	 * an encoded object.
	 * 
	 * @param coder
	 *            an FSCoder containing the binary data.
	 */
	public FSSolidFill(FSCoder coder)
	{
		decode(coder);
	}

	/**
	 * Constructs an FSSolidFill object of the specified colour.
	 * 
	 * @param aColor
	 *            an FSColor object that defines the colour that the area will
	 *            be filled with.
	 */
	public FSSolidFill(FSColor aColor)
	{
		super(FSFillStyle.Solid);
		setColor(aColor);
	}

	/**
	 * Constructs an FSSolidFill object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSSolidFill object.
	 */
	public FSSolidFill(FSSolidFill obj)
	{
		super(obj);
		color = new FSColor(obj.color);
	}

	/**
	 * Gets the colour of the fill style.
	 * 
	 * @return the colour defined for the solid fill.
	 */
	public FSColor getColor()
	{
		return color;
	}

	/**
	 * Sets the colour of the fill style.
	 * 
	 * @param aColor
	 *            an FSColor object that defines the colour that the area will
	 *            be filled with.
	 */
	public void setColor(FSColor aColor)
	{
		color = aColor;
	}

	public Object clone()
	{
		FSSolidFill anObject = (FSSolidFill) super.clone();

		anObject.color = (color != null) ? (FSColor) color.clone() : null;

		return anObject;
	}

	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
		{
			FSSolidFill typedObject = (FSSolidFill) anObject;

			if (color != null)
				result = color.equals(typedObject.color);
			else
				result = color == typedObject.color;
		}
		return result;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		buffer.append(name());

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "color", color, depth);
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		int length = super.length(coder);

		length += color.length(coder);

		return length;
	}

	public void encode(FSCoder coder)
	{
		super.encode(coder);
		color.encode(coder);
	}

	public void decode(FSCoder coder)
	{
		super.decode(coder);
		color = new FSColor(coder);
	}
}
