PageRenderTime 23ms CodeModel.GetById 11ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/src/gosu/jruby/ColorProxy.java

http://jgosu.googlecode.com/
Java | 183 lines | 134 code | 49 blank | 0 comment | 9 complexity | 2c4024c3e40d093e1f4fb4d5b3781d3c MD5 | raw file
  1import gosu.Color;
  2
  3import org.jruby.*;
  4import org.jruby.runtime.*;
  5import org.jruby.runtime.builtin.IRubyObject;
  6import org.jruby.anno.JRubyMethod;
  7import static org.jruby.RubyNumeric.*;
  8import static org.jruby.RubyFloat.*;
  9
 10public class ColorProxy extends RubyObject {
 11  private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
 12    public IRubyObject allocate(Ruby runtime, RubyClass klass) {
 13      return new ColorProxy(runtime, klass);
 14    }
 15  };
 16
 17  private Color _color;
 18
 19  private final Ruby _runtime;
 20
 21  public static void createColorClass(Ruby runtime) {
 22    RubyModule mGosu = runtime.getModule("Gosu");
 23    RubyClass cColor = mGosu.defineClassUnder("Color", runtime.getObject(), ALLOCATOR);
 24
 25    cColor.defineAnnotatedMethods(ColorProxy.class);
 26  }
 27
 28
 29  public ColorProxy(Ruby runtime, RubyClass type) {
 30    super(runtime, type);
 31    _runtime = runtime;
 32  }
 33
 34
 35  public void setColor(Color color) { _color = color; }
 36
 37
 38  public Color getColor() { return _color; }
 39
 40
 41  @JRubyMethod(name = "initialize",
 42               required = 1,
 43               optional = 3,
 44               frame = true,
 45               visibility = Visibility.PRIVATE)
 46  public IRubyObject initialize(IRubyObject[] args) {
 47    int numArgs = Arity.checkArgumentCount(_runtime, args, 1, 4);
 48    if (numArgs == 1) _color = new Color((int) fix2long(args[0]));
 49    else if (numArgs == 2) Arity.raiseArgumentError(_runtime, numArgs, 3, 4);
 50    else if (numArgs == 3) _color = new Color(fix2int(args[0]), fix2int(args[1]), fix2int(args[2]));
 51    else _color = new Color(fix2int(args[0]), fix2int(args[1]), fix2int(args[2]), fix2int(args[3]));
 52
 53    return this;
 54  }
 55
 56  
 57  @JRubyMethod(name = "alpha")
 58  public IRubyObject alpha() {
 59    return _runtime.newFixnum(_color.alpha());
 60  }
 61
 62  
 63  @JRubyMethod(name = "alpha=",
 64               required = 1)
 65  public IRubyObject setAlpha(IRubyObject r_alpha) {
 66    _color.setAlpha(fix2int(r_alpha));
 67    return _runtime.getNil();
 68  }
 69
 70
 71  @JRubyMethod(name = "red")
 72  public IRubyObject red() {
 73    return _runtime.newFixnum(_color.red());
 74  }
 75
 76  
 77  @JRubyMethod(name = "red=",
 78               required = 1)
 79  public IRubyObject setRed(IRubyObject r_red) {
 80    _color.setRed(fix2int(r_red));
 81    return _runtime.getNil();
 82  }
 83
 84
 85  @JRubyMethod(name = "green")
 86  public IRubyObject green() {
 87    return _runtime.newFixnum(_color.green());
 88  }
 89
 90  
 91  @JRubyMethod(name = "green=",
 92               required = 1)
 93  public IRubyObject setGreen(IRubyObject r_green) {
 94    _color.setGreen(fix2int(r_green));
 95    return _runtime.getNil();
 96  }
 97
 98
 99  @JRubyMethod(name = "blue")
100  public IRubyObject blue() {
101    return _runtime.newFixnum(_color.blue());
102  }
103
104  
105  @JRubyMethod(name = "blue=",
106               required = 1)
107  public IRubyObject setBlue(IRubyObject r_blue) {
108    _color.setBlue(fix2int(r_blue));
109    return _runtime.getNil();
110  }
111
112
113  @JRubyMethod(name = "hue")
114  public IRubyObject hue() {
115    return newFloat(_runtime, _color.hue());
116  }
117
118  
119  @JRubyMethod(name = "hue=",
120               required = 1)
121  public IRubyObject setHue(IRubyObject r_hue) {
122    _color.setHue((float) num2dbl(r_hue));
123    return _runtime.getNil();
124  }
125
126
127  @JRubyMethod(name = "saturation")
128  public IRubyObject saturation() {
129    return newFloat(_runtime, _color.saturation());
130  }
131
132  
133  @JRubyMethod(name = "saturation=",
134               required = 1)
135  public IRubyObject setSaturation(IRubyObject r_saturation) {
136    _color.setSaturation((float) num2dbl(r_saturation));
137    return _runtime.getNil();
138  }
139
140
141  @JRubyMethod(name = "value")
142  public IRubyObject value() {
143    return newFloat(_runtime, _color.value());
144  }
145
146  
147  @JRubyMethod(name = "value=",
148               required = 1)
149  public IRubyObject setValue(IRubyObject r_value) {
150    _color.setValue((float) num2dbl(r_value));
151    return _runtime.getNil();
152  }
153
154
155  @JRubyMethod(name = "from_hsv",
156               required = 3,
157               meta = true)
158  public static IRubyObject fromHSV(IRubyObject klass, IRubyObject r_h, IRubyObject r_s, IRubyObject r_v) {
159    float h = (float) num2dbl(r_h);
160    float s = (float) num2dbl(r_s);
161    float v = (float) num2dbl(r_v);
162
163    Color color = Color.fromHSV(h, s, v);
164    ColorProxy proxy = (ColorProxy) ((RubyClass) klass).allocate();
165    proxy.setColor(color);
166    return proxy;
167  }
168
169
170  @JRubyMethod(name = "from_ahsv",
171               required = 4,
172               meta = true)
173  public static IRubyObject fromAHSV(IRubyObject klass, IRubyObject[] args) {
174    float h = (float) num2dbl(args[1]);
175    float s = (float) num2dbl(args[2]);
176    float v = (float) num2dbl(args[3]);
177
178    Color color = Color.fromAHSV(fix2int(args[0]), h, s, v);
179    ColorProxy proxy = (ColorProxy) ((RubyClass) klass).allocate();
180    proxy.setColor(color);
181    return proxy;
182  }
183}