View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package nl.tudelft.goal.ut2004.visualizer.map;
7   
8   import java.awt.Color;
9   
10  /**
11   * Generator of various colors that are suitable for representation of agents in
12   * the map.
13   * 
14   * @author Honza
15   */
16  public class TeamShade {
17  
18  	private Color personalBase;
19  	private Color personalRed;
20  	private Color personalBlue;
21  
22  	private Color redTeam;
23  	private Color blueTeam;
24  
25  	private static final double TEAM_COLOR_STRENGHT = 0.25;
26  
27  	public TeamShade(){
28  		this(randomColor());
29  	}
30  	
31  	public TeamShade(Color personalColor) {
32  		this(personalColor, Color.RED, Color.BLUE);
33  	}
34  
35  	public TeamShade(Color redteam, Color blueTeam) {
36  		this(randomColor(), redteam, blueTeam);
37  	}
38  
39  	public TeamShade(Color personalColor, Color redTeam, Color blueTeam) {
40  		this.personalBase = personalColor;
41  		this.redTeam = redTeam;
42  		this.blueTeam = blueTeam;
43  
44  		personalRed = mix(redTeam, personalBase, TEAM_COLOR_STRENGHT);
45  		personalBlue = mix(blueTeam, personalBase, TEAM_COLOR_STRENGHT);
46  	}
47  
48  	public Color getColor(int team) {
49  		switch (team) {
50  		case 0:
51  			return personalRed;
52  		case 1:
53  			return personalBlue;
54  		default:
55  			return personalBase;
56  		}
57  	}
58  
59  	/**
60  	 * Create a mixed color from passed color and this color in following
61  	 * fashion:
62  	 * 
63  	 * <pre>
64  	 * this * (1 - portion) + mixing * portion
65  	 * </pre>
66  	 * 
67  	 * @param mixing
68  	 *            Color that will be mixed with this one.
69  	 * @param portion
70  	 *            how much of mixing color will be used
71  	 * @return Mixed color, not that this color won't be changed
72  	 */
73  	private Color mix(Color a, Color b, double portion) {
74  		double thisPortion = 1 - portion;
75  		return new Color(
76  		// Red
77  				(int) (thisPortion * a.getRed()) + (int) (portion * b.getRed()),
78  				// Green
79  				(int) (thisPortion * a.getGreen()) + (int) (portion * b.getGreen()),
80  				// Blue
81  				(int) (thisPortion * a.getBlue()) + (int) (portion * b.getBlue()),
82  				// Alpha
83  				(int) (thisPortion * a.getAlpha()) + (int) (portion * b.getAlpha()));
84  	}
85  
86  	private static Color randomColor() {
87  		return new Color((float) Math.random(), (float) Math.random(), (float) Math.random());
88  	}
89  
90  	public Color getRedTeam() {
91  		return redTeam;
92  	}
93  
94  	public void setRedTeam(Color redTeam) {
95  		this.redTeam = redTeam;
96  		personalRed = mix(redTeam, personalBase, TEAM_COLOR_STRENGHT);
97  	}
98  
99  	public Color getBlueTeam() {
100 		return blueTeam;
101 	}
102 
103 	public void setBlueTeam(Color blueTeam) {
104 		this.blueTeam = blueTeam;
105 		personalBlue = mix(blueTeam, personalBase, TEAM_COLOR_STRENGHT);
106 	}
107 
108 }