View Javadoc

1   /* Open Log Viewer
2    *
3    * Copyright 2011
4    *
5    * This file is part of the OpenLogViewer project.
6    *
7    * OpenLogViewer software is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * OpenLogViewer software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with any OpenLogViewer software.  If not, see http://www.gnu.org/licenses/
19   *
20   * I ask that if you make any changes to this file you fork the code on github.com!
21   *
22   */
23  package org.diyefi.openlogviewer.coloring;
24  
25  import java.awt.Color;
26  import java.util.List;
27  import java.util.LinkedList;
28  import java.util.ListIterator;
29  
30  /**
31   * InitialLineColoring is used to provide the coloring for the GenericDataElements.
32   * The colors provided should be the most contrasting colors possible.
33   * @author Ben Fenner
34   */
35  public enum InitialLineColoring {
36  
37  	INSTANCE;
38  
39  	private final List<MarkedColor> colorList;
40  
41  	private InitialLineColoring() {
42  		colorList = new LinkedList<MarkedColor>();
43  
44  		colorList.add(new MarkedColor(Color.getHSBColor(1.0F, 1.0F, 1.0F), true, 0.0));
45  		colorList.add(new MarkedColor(Color.getHSBColor(0.25F, 1.0F, 1.0F), true, 0.0));
46  		colorList.add(new MarkedColor(Color.getHSBColor(0.5F, 1.0F, 1.0F), true, 0.0));
47  		colorList.add(new MarkedColor(Color.getHSBColor(0.75F, 1.0F, 1.0F), true, 0.0));
48  
49  		for (long i = 8; i < 128; i *= 2) {
50  			this.addColors(i);
51  		}
52  	}
53  
54  	private void addColors(final long hueOffsetDinominator) {
55  		final long numColors = hueOffsetDinominator / 2;
56  		final double hueOffset = 1.0 / hueOffsetDinominator;
57  
58  		for (long i = 0; i < (numColors - 1); i++) {
59  			double hue = 0.0;
60  			hue += hueOffset; // Always skip pure red and go to the next hue.
61  
62  			for (int j = 0; j < 4; j++) {
63  				final MarkedColor newColor = new MarkedColor(Color.getHSBColor((float) hue, 1.0F, 1.0F), true, hue);
64  				if (!colorList.contains(newColor)) {
65  					colorList.add(newColor);
66  				}
67  				hue += 0.25;
68  			}
69  		}
70  	}
71  
72  	public Color getBestAvailableColor() {
73  		Color nextColor = Color.gray;
74  		final ListIterator<MarkedColor> i = colorList.listIterator();
75  
76  		for (boolean found = false; i.hasNext() && !found;) {
77  			final MarkedColor c = i.next();
78  
79  			if (c.isAvailable()) {
80  				c.setAvailability(false);
81  				nextColor = c.getColor();
82  				found = true;
83  			}
84  		}
85  		return nextColor;
86  	}
87  
88  	public void giveBackColor(final Color c) {
89  		if (colorList.contains(c)) {
90  			colorList.get(colorList.indexOf(c)).setAvailability(true);
91  		}
92  	}
93  }