001    package org.shiftone.jrat.provider.tree.ui.trace;
002    
003    
004    import org.shiftone.jrat.ui.util.DotIcon;
005    import org.shiftone.jrat.util.log.Logger;
006    
007    import javax.swing.*;
008    import java.awt.*;
009    
010    
011    public class PercentColorLookup {
012    
013        private static final Logger LOG = Logger.getLogger(PercentColorLookup.class);
014        private Color[] colors = new Color[100];
015        private Icon[] icons = new Icon[100];
016        private double magic = (double) 0xff / (double) (colors.length / 2);
017    
018        public PercentColorLookup() {
019    
020            LOG.info("magic " + magic);
021    
022            for (int i = 0; i < colors.length; i++) {
023                int r = (int) Math.min(i * magic, 0xff);
024                int g = (int) Math.min((100 - i) * magic, 0xff);
025                int b = 0;
026    
027                colors[i] = new Color(r, g, b);
028                icons[i] = new DotIcon(10, colors[i]);
029    
030                // LOG.info(i + " = " + Integer.toHexString(r) + " " +
031                // Integer.toHexString(g));
032            }
033        }
034    
035    
036        public static void main(String[] args) {
037            new PercentColorLookup();
038        }
039    
040    
041        private int getIndex(double pct) {
042            return Math.min((int) Math.round(pct), colors.length - 1);
043        }
044    
045    
046        public Color getColor(double pct) {
047            return colors[getIndex(pct)];
048        }
049    
050    
051        public Icon getIcon(double pct) {
052            return icons[getIndex(pct)];
053        }
054    }