1 package prefuse.action.animate; 2 3 import prefuse.action.ItemAction; 4 import prefuse.util.ColorLib; 5 import prefuse.util.PrefuseLib; 6 import prefuse.util.collections.CopyOnWriteArrayList; 7 import prefuse.visual.VisualItem; 8 9 10 19 public class ColorAnimator extends ItemAction { 20 21 private static final String [] DEFAULTS = new String [] { 22 VisualItem.STROKECOLOR, VisualItem.FILLCOLOR, 23 VisualItem.TEXTCOLOR }; 24 25 private CopyOnWriteArrayList m_colorFields; 26 27 30 public ColorAnimator() { 31 super(); 32 setColorFields(DEFAULTS); 33 } 34 35 39 public ColorAnimator(String group) { 40 super(group); 41 setColorFields(DEFAULTS); 42 } 43 44 50 public ColorAnimator(String group, String field) { 51 super(group); 52 setColorFields(new String [] {field}); 53 } 54 55 61 public ColorAnimator(String group, String [] fields) { 62 super(group); 63 setColorFields(fields); 64 } 65 66 70 public void setColorFields(String [] fields) { 71 if ( fields == null ) { 72 throw new IllegalArgumentException (); 73 } 74 75 if ( m_colorFields == null ) 76 m_colorFields = new CopyOnWriteArrayList(); 77 else 78 m_colorFields.clear(); 79 80 for ( int i=0; i<fields.length; ++i ) { 81 m_colorFields.add(fields[i]); 82 m_colorFields.add(PrefuseLib.getStartField(fields[i])); 83 m_colorFields.add(PrefuseLib.getEndField(fields[i])); 84 } 85 } 86 87 90 public void process(VisualItem item, double frac) { 91 if ( m_colorFields == null ) return; 92 93 Object [] fields = m_colorFields.getArray(); 94 for ( int i=0; i<fields.length; i += 3 ) { 95 String f = (String )fields[i]; 96 String sf = (String )fields[i+1]; 97 String ef = (String )fields[i+2]; 98 99 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 } | Popular Tags |