/*
 * FSMorphGradient.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;

/**
 * FSMorphGradient defines the control points that is used to specify how a
 * gradient fill is displayed at the start and end of the shape morphing
 * process.
 * 
 * <p>
 * Two or more control points are used to define how the colour changes across
 * the gradient square.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>startRatio</td>
 * <td>The location of the control point across the gradient square at the
 * start of the morphing process.</td>
 * </tr>
 * 
 * <tr>
 * <td>endRatio</td>
 * <td>The location of the control point across the gradient square at the end
 * of the morphing process.</td>
 * </tr>
 * 
 * <tr>
 * <td>startColor</td>
 * <td>The colour at the control point at the start of the morphing process.</td>
 * </tr>
 * 
 * <tr>
 * <td>startColor</td>
 * <td>The colour at the control point at the end of the morphing process.</td>
 * </tr>
 * </table>
 * 
 * <p>
 * The location of the control point is defined by a ratio - a number between 0
 * and 255 that specifies the relative location in the square. For Linear
 * Gradient Fills a ratio of zero is mapped to the left side of the gradient
 * square and 255 is mapped to the right side of the square. For Radial Gradient
 * Fills a ratio of zero is mapped to the centre of the gradient square and 255
 * is mapped to the edge of the largest circle that fits inside the gradient
 * square.
 * </p>
 * 
 * <p>
 * FSMorphGradients are only used in FSDefineMorphShape objects which support
 * transparent colours so values for all the colour channels must be specified
 * in the FSColor objects.
 * </p>
 * 
 * <p>
 * When the Flash Player displays the gradient control points they are sorted by
 * the ratio with the FSGradient object with the smallest ratio value displayed
 * first.
 * </p>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSMorphGradient class represents the MorphGradientRecord structure from
 * the Macromedia Flash (SWF) File Format Specification. Shape morphing was
 * introduced in Flash 3.
 * </p>
 */
public class FSMorphGradient extends FSTransformObject
{
	private int startRatio = 0;
	private int endRatio = 0;
	private FSColor startColor = null;
	private FSColor endColor = null;

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

	/**
	 * Constructs an FSMorphGradient object specifying the starting and ending
	 * ratios and colours.
	 * 
	 * @param aStartRatio
	 *            the ratio along the gradient square at the start of the
	 *            morphing process.
	 * @param anEndRatio
	 *            the ratio along the gradient square at the end of the morphing
	 *            process.
	 * @param aStartColor
	 *            the colour at the starting control point.
	 * @param anEndColor
	 *            the colour at the ending control point.
	 */
	public FSMorphGradient(int aStartRatio, int anEndRatio,
							FSColor aStartColor, FSColor anEndColor)
	{
		setStartRatio(aStartRatio);
		setEndRatio(anEndRatio);
		setStartColor(aStartColor);
		setEndColor(anEndColor);
	}

	/**
	 * Constructs an FSMorphGradient object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSMorphGradient object.
	 */
	public FSMorphGradient(FSMorphGradient obj)
	{
		startRatio = obj.startRatio;
		endRatio = obj.endRatio;
		startColor = new FSColor(obj.startColor);
		endColor = new FSColor(obj.endColor);
	}

	/**
	 * Gets the ratio at the start of the morphing process.
	 * 
	 * @return the start ratio.
	 */
	public int getStartRatio()
	{
		return startRatio;
	}

	/**
	 * Gets the ratio at the end of the morphing process.
	 * 
	 * @return the end ratio.
	 */
	public int getEndRatio()
	{
		return endRatio;
	}

	/**
	 * Gets the colour at the start of the morphing process.
	 * 
	 * @return the start colour.
	 */
	public FSColor getStartColor()
	{
		return startColor;
	}

	/**
	 * Gets the colour at the end of the morphing process.
	 * 
	 * @return the end colour.
	 */
	public FSColor getEndColor()
	{
		return endColor;
	}

	/**
	 * Sets the ratio along the gradient square at the start of the morphing
	 * process.
	 * 
	 * @param aNumber
	 *            the starting ratio.
	 */
	public void setStartRatio(int aNumber)
	{
		startRatio = aNumber;
	}

	/**
	 * Sets the ratio along the gradient square at the end of the morphing
	 * process.
	 * 
	 * @param aNumber
	 *            the ending ratio.
	 */
	public void setEndRatio(int aNumber)
	{
		endRatio = aNumber;
	}

	/**
	 * Sets the colour at the start of the morphing process.
	 * 
	 * @param aColor
	 *            the start colour.
	 */
	public void setStartColor(FSColor aColor)
	{
		startColor = aColor;
	}

	/**
	 * Sets the colour at the end of the morphing process.
	 * 
	 * @param aColor
	 *            the end colour.
	 */
	public void setEndColor(FSColor aColor)
	{
		endColor = aColor;
	}

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

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

		return anObject;
	}

	/**
	 * Returns true if anObject is equal to this one. Objects are considered
	 * equal if they would generate identical binary data when they are encoded
	 * to a Flash file.
	 * 
	 * @return true if this object would be identical to anObject when encoded.
	 */
	public boolean equals(Object anObject)
	{
		boolean result = false;

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

			result = startRatio == typedObject.startRatio;
			result = result && endRatio == typedObject.endRatio;

			if (startColor != null)
				result = result && startColor.equals(typedObject.startColor);
			else
				result = result && startColor == typedObject.startColor;

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

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

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "startRatio", startRatio);
			Transform.append(buffer, "endRatio", endRatio);
			Transform.append(buffer, "startColor", startColor, depth);
			Transform.append(buffer, "endColor", endColor, depth);
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		int length = 2;

		length += startColor.length(coder);
		length += endColor.length(coder);

		return length;
	}

	public void encode(FSCoder coder)
	{
		coder.writeWord(startRatio, 1);
		startColor.encode(coder);
		coder.writeWord(endRatio, 1);
		endColor.encode(coder);
	}

	public void decode(FSCoder coder)
	{
		startRatio = coder.readWord(1, false);
		startColor = new FSColor(coder);
		endRatio = coder.readWord(1, false);
		endColor = new FSColor(coder);
	}
}
