PageRenderTime 47ms CodeModel.GetById 26ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/gosu/Color.java

http://jgosu.googlecode.com/
Java | 107 lines | 69 code | 38 blank | 0 comment | 0 complexity | 5e790e445a0df71c1b43e4bb48346a50 MD5 | raw file
  1package gosu;
  2
  3import java.awt.color.*;
  4
  5public class Color {
  6  public static Color none = new Color(0x00000000);
  7  public static Color black = new Color(java.awt.Color.black);
  8  public static Color gray = new Color(java.awt.Color.gray);
  9  public static Color white = new Color(java.awt.Color.white);
 10  public static Color red = new Color(java.awt.Color.red);
 11  public static Color green = new Color(java.awt.Color.green);
 12  public static Color blue = new Color(java.awt.Color.blue);
 13  public static Color yellow = new Color(java.awt.Color.yellow);
 14  public static Color fuschia = new Color(0xffff00ff);
 15  public static Color cyan = new Color(java.awt.Color.cyan);
 16  
 17
 18  private java.awt.Color _color;
 19
 20
 21  public Color(java.awt.Color color) {
 22    _color = color;
 23  }
 24
 25  public Color(int argb) {
 26    _color = new java.awt.Color(argb, true);
 27  }
 28
 29
 30  public Color(int r, int g, int b) {
 31    _color = new java.awt.Color(r, g, b);
 32  }
 33
 34
 35  public Color(int a, int r, int g, int b) {
 36    _color = new java.awt.Color(r, g, b, a);
 37  }
 38
 39
 40  public int alpha() { return _color.getAlpha(); }
 41  public int red() { return _color.getRed(); }
 42  public int green() { return _color.getGreen(); }
 43  public int blue() { return _color.getBlue(); }
 44
 45
 46  public void setAlpha(int alpha) {
 47    _color = new java.awt.Color(_color.getRed(), _color.getGreen(), _color.getBlue(), alpha);
 48  }
 49
 50
 51  public void setRed(int red) {
 52    _color = new java.awt.Color(red, _color.getGreen(), _color.getBlue(), _color.getAlpha());
 53  }
 54
 55
 56  public void setGreen(int green) {
 57    _color = new java.awt.Color(_color.getRed(), green, _color.getBlue(), _color.getAlpha());
 58  }
 59
 60
 61  public void setBlue(int blue) {
 62    _color = new java.awt.Color(_color.getRed(), _color.getGreen(), blue, _color.getAlpha());
 63  }
 64
 65
 66  public double hue() { return toHSV()[0] * 360.0; }
 67  public double saturation() { return toHSV()[1]; }
 68  public double value() { return toHSV()[2]; }
 69
 70
 71  public void setHue(float hue) {
 72    float hsb[] = toHSV();
 73    _color = java.awt.Color.getHSBColor(hue / 360.0f, hsb[1], hsb[2]);
 74  }
 75
 76
 77  public void setSaturation(float saturation) {
 78    float hsb[] = toHSV();
 79    _color = java.awt.Color.getHSBColor(hsb[0], saturation, hsb[2]);
 80  }
 81
 82
 83  public void setValue(float value) {
 84    float hsb[] = toHSV();
 85    _color = java.awt.Color.getHSBColor(hsb[0], hsb[1], value);
 86  }
 87
 88
 89  public java.awt.Color getAWTColor() { return _color; }
 90
 91
 92  public static Color fromHSV(float h, float s, float v) {
 93    return fromAHSV(255, h, s, v);
 94  }
 95
 96
 97  public static Color fromAHSV(int alpha, float h, float s, float v) {
 98    java.awt.Color c = java.awt.Color.getHSBColor(h/360.0f, s, v);
 99    return new Color(alpha, c.getRed(), c.getGreen(), c.getBlue());
100  }
101
102
103  private float[] toHSV() {
104    return java.awt.Color.RGBtoHSB(_color.getRed(), _color.getGreen(), _color.getBlue(), null);
105  }
106}
107