/src/gosu/jruby/ColorProxy.java

http://jgosu.googlecode.com/ · Java · 183 lines · 134 code · 49 blank · 0 comment · 9 complexity · 2c4024c3e40d093e1f4fb4d5b3781d3c MD5 · raw file

  1. import gosu.Color;
  2. import org.jruby.*;
  3. import org.jruby.runtime.*;
  4. import org.jruby.runtime.builtin.IRubyObject;
  5. import org.jruby.anno.JRubyMethod;
  6. import static org.jruby.RubyNumeric.*;
  7. import static org.jruby.RubyFloat.*;
  8. public class ColorProxy extends RubyObject {
  9. private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
  10. public IRubyObject allocate(Ruby runtime, RubyClass klass) {
  11. return new ColorProxy(runtime, klass);
  12. }
  13. };
  14. private Color _color;
  15. private final Ruby _runtime;
  16. public static void createColorClass(Ruby runtime) {
  17. RubyModule mGosu = runtime.getModule("Gosu");
  18. RubyClass cColor = mGosu.defineClassUnder("Color", runtime.getObject(), ALLOCATOR);
  19. cColor.defineAnnotatedMethods(ColorProxy.class);
  20. }
  21. public ColorProxy(Ruby runtime, RubyClass type) {
  22. super(runtime, type);
  23. _runtime = runtime;
  24. }
  25. public void setColor(Color color) { _color = color; }
  26. public Color getColor() { return _color; }
  27. @JRubyMethod(name = "initialize",
  28. required = 1,
  29. optional = 3,
  30. frame = true,
  31. visibility = Visibility.PRIVATE)
  32. public IRubyObject initialize(IRubyObject[] args) {
  33. int numArgs = Arity.checkArgumentCount(_runtime, args, 1, 4);
  34. if (numArgs == 1) _color = new Color((int) fix2long(args[0]));
  35. else if (numArgs == 2) Arity.raiseArgumentError(_runtime, numArgs, 3, 4);
  36. else if (numArgs == 3) _color = new Color(fix2int(args[0]), fix2int(args[1]), fix2int(args[2]));
  37. else _color = new Color(fix2int(args[0]), fix2int(args[1]), fix2int(args[2]), fix2int(args[3]));
  38. return this;
  39. }
  40. @JRubyMethod(name = "alpha")
  41. public IRubyObject alpha() {
  42. return _runtime.newFixnum(_color.alpha());
  43. }
  44. @JRubyMethod(name = "alpha=",
  45. required = 1)
  46. public IRubyObject setAlpha(IRubyObject r_alpha) {
  47. _color.setAlpha(fix2int(r_alpha));
  48. return _runtime.getNil();
  49. }
  50. @JRubyMethod(name = "red")
  51. public IRubyObject red() {
  52. return _runtime.newFixnum(_color.red());
  53. }
  54. @JRubyMethod(name = "red=",
  55. required = 1)
  56. public IRubyObject setRed(IRubyObject r_red) {
  57. _color.setRed(fix2int(r_red));
  58. return _runtime.getNil();
  59. }
  60. @JRubyMethod(name = "green")
  61. public IRubyObject green() {
  62. return _runtime.newFixnum(_color.green());
  63. }
  64. @JRubyMethod(name = "green=",
  65. required = 1)
  66. public IRubyObject setGreen(IRubyObject r_green) {
  67. _color.setGreen(fix2int(r_green));
  68. return _runtime.getNil();
  69. }
  70. @JRubyMethod(name = "blue")
  71. public IRubyObject blue() {
  72. return _runtime.newFixnum(_color.blue());
  73. }
  74. @JRubyMethod(name = "blue=",
  75. required = 1)
  76. public IRubyObject setBlue(IRubyObject r_blue) {
  77. _color.setBlue(fix2int(r_blue));
  78. return _runtime.getNil();
  79. }
  80. @JRubyMethod(name = "hue")
  81. public IRubyObject hue() {
  82. return newFloat(_runtime, _color.hue());
  83. }
  84. @JRubyMethod(name = "hue=",
  85. required = 1)
  86. public IRubyObject setHue(IRubyObject r_hue) {
  87. _color.setHue((float) num2dbl(r_hue));
  88. return _runtime.getNil();
  89. }
  90. @JRubyMethod(name = "saturation")
  91. public IRubyObject saturation() {
  92. return newFloat(_runtime, _color.saturation());
  93. }
  94. @JRubyMethod(name = "saturation=",
  95. required = 1)
  96. public IRubyObject setSaturation(IRubyObject r_saturation) {
  97. _color.setSaturation((float) num2dbl(r_saturation));
  98. return _runtime.getNil();
  99. }
  100. @JRubyMethod(name = "value")
  101. public IRubyObject value() {
  102. return newFloat(_runtime, _color.value());
  103. }
  104. @JRubyMethod(name = "value=",
  105. required = 1)
  106. public IRubyObject setValue(IRubyObject r_value) {
  107. _color.setValue((float) num2dbl(r_value));
  108. return _runtime.getNil();
  109. }
  110. @JRubyMethod(name = "from_hsv",
  111. required = 3,
  112. meta = true)
  113. public static IRubyObject fromHSV(IRubyObject klass, IRubyObject r_h, IRubyObject r_s, IRubyObject r_v) {
  114. float h = (float) num2dbl(r_h);
  115. float s = (float) num2dbl(r_s);
  116. float v = (float) num2dbl(r_v);
  117. Color color = Color.fromHSV(h, s, v);
  118. ColorProxy proxy = (ColorProxy) ((RubyClass) klass).allocate();
  119. proxy.setColor(color);
  120. return proxy;
  121. }
  122. @JRubyMethod(name = "from_ahsv",
  123. required = 4,
  124. meta = true)
  125. public static IRubyObject fromAHSV(IRubyObject klass, IRubyObject[] args) {
  126. float h = (float) num2dbl(args[1]);
  127. float s = (float) num2dbl(args[2]);
  128. float v = (float) num2dbl(args[3]);
  129. Color color = Color.fromAHSV(fix2int(args[0]), h, s, v);
  130. ColorProxy proxy = (ColorProxy) ((RubyClass) klass).allocate();
  131. proxy.setColor(color);
  132. return proxy;
  133. }
  134. }