View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.timeline.map;
2   
3   import java.awt.Color;
4   
5   /**
6    * AWT provided Color class is not very suitable for OpenGl color manipulation so
7    * this class is supposed to remedy it.
8    *
9    * @author Honza
10   */
11  public class GlColor {
12  
13      public double r;
14      public double g;
15      public double b;
16      /**
17       * Alpha, 1 is fully opaque, 0 is fully transparent
18       */
19      public double a;
20  
21      /**
22       * Create new GlColor
23       * @param r In range <0-1>
24       * @param g In range <0-1>
25       * @param b In range <0-1>
26       * @param a In range <0-1> 0 transparent, 1 opaque
27       */
28      public GlColor(double r, double g, double b, double a) {
29          this.r = r;
30          this.g = g;
31          this.b = b;
32          this.a = a;
33      }
34  
35      /**
36       * Create fully opaque GlColor.
37       * @param r In range <0-1>
38       * @param g In range <0-1>
39       * @param b In range <0-1>
40       */
41      public GlColor(double r, double g, double b) {
42          this(r, g, b, 1.0);
43      }
44  
45      /**
46       * Create GlColor based on passed Color and alpha
47       * @param color Basic color
48       * @param a In range <0-1> 0 transparent, 1 opaque
49       */
50      public GlColor(Color color, double alpha) {
51          this(color.getRed() / 255.0, color.getGreen() / 255.0, color.getBlue() / 255.0, alpha);
52      }
53  
54      /**
55       * Create opaque color based on passed color.
56       * @param color
57       */
58      public GlColor(Color color) {
59          this(color.getRed() / 255.0, color.getGreen() / 255.0, color.getBlue() / 255.0);
60      }
61  
62      public GlColor(GlColor color) {
63          this(color.r, color.g, color.b, color.a);
64      }
65  
66      /**
67       * Create a mixed color from passed color and this color in following fashion:
68       * <pre>this * (1-portion) + mixing * portion</pre>
69       * @param mixing Color that will be mixed with this one.
70       * @param portion how much of mixing color will be used
71       * @return Mixed color, not that this color won't be changed
72       */
73      public GlColor getMixedWith(GlColor mixing, double portion) {
74          double thisPortion = 1 - portion;
75          return new GlColor(
76                  thisPortion * r + portion * mixing.r,
77                  thisPortion * g + portion * mixing.g,
78                  thisPortion * b + portion * mixing.b,
79                  thisPortion * a + portion * mixing.a);
80      }
81  
82      @Override
83      public String toString() {
84          return "[r:" + r + "; g:" + g + "; b:" + b + "; a: " + a + "]";
85      }
86  }