001: package prefuse.action.animate;
002:
003: import prefuse.action.ItemAction;
004: import prefuse.util.ColorLib;
005: import prefuse.util.PrefuseLib;
006: import prefuse.util.collections.CopyOnWriteArrayList;
007: import prefuse.visual.VisualItem;
008:
009: /**
010: * Animator that linearly interpolates between starting and ending colors
011: * for VisualItems during an animation. By default, interpolates the three
012: * primary color fields: {@link VisualItem#STROKECOLOR stroke color},
013: * {@link VisualItem#FILLCOLOR fill color}, and
014: * {@link VisualItem#TEXTCOLOR text color}.
015: *
016: * @author <a href="http://jheer.org">jeffrey heer</a>
017: */
018: public class ColorAnimator extends ItemAction {
019:
020: private static final String[] DEFAULTS = new String[] {
021: VisualItem.STROKECOLOR, VisualItem.FILLCOLOR,
022: VisualItem.TEXTCOLOR };
023:
024: private CopyOnWriteArrayList m_colorFields;
025:
026: /**
027: * Create a new ColorAnimator that processes all data groups.
028: */
029: public ColorAnimator() {
030: super ();
031: setColorFields(DEFAULTS);
032: }
033:
034: /**
035: * Create a new ColorAnimator that processes the specified group.
036: * @param group the data group to process
037: */
038: public ColorAnimator(String group) {
039: super (group);
040: setColorFields(DEFAULTS);
041: }
042:
043: /**
044: * Create a new ColorAnimator that processes the specified group and
045: * color field.
046: * @param group the data group to process
047: * @param field the color field to interpolate
048: */
049: public ColorAnimator(String group, String field) {
050: super (group);
051: setColorFields(new String[] { field });
052: }
053:
054: /**
055: * Create a new ColorAnimator that processes the specified group and
056: * color fields.
057: * @param group the data group to process
058: * @param fields the color fields to interpolate
059: */
060: public ColorAnimator(String group, String[] fields) {
061: super (group);
062: setColorFields(fields);
063: }
064:
065: /**
066: * Sets the color fields to interpolate.
067: * @param fields the color fields to interpolate
068: */
069: public void setColorFields(String[] fields) {
070: if (fields == null) {
071: throw new IllegalArgumentException();
072: }
073:
074: if (m_colorFields == null)
075: m_colorFields = new CopyOnWriteArrayList();
076: else
077: m_colorFields.clear();
078:
079: for (int i = 0; i < fields.length; ++i) {
080: m_colorFields.add(fields[i]);
081: m_colorFields.add(PrefuseLib.getStartField(fields[i]));
082: m_colorFields.add(PrefuseLib.getEndField(fields[i]));
083: }
084: }
085:
086: /**
087: * @see prefuse.action.ItemAction#process(prefuse.visual.VisualItem, double)
088: */
089: public void process(VisualItem item, double frac) {
090: if (m_colorFields == null)
091: return;
092:
093: Object[] fields = m_colorFields.getArray();
094: for (int i = 0; i < fields.length; i += 3) {
095: String f = (String) fields[i];
096: String sf = (String) fields[i + 1];
097: String ef = (String) fields[i + 2];
098:
099: int sc = item.getInt(sf), ec = item.getInt(ef);
100: int cc = ColorLib.interp(sc, ec, frac);
101: item.setInt(f, cc);
102: }
103: }
104:
105: } // end of class ColorAnimator
|