Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Tuesday, 24 December 2013

Experimental handler for overloaded "background" in JRubyArt

Previously for ruby-processing, we allowed jruby to make sensible choices for methods with overloaded signatures (but to encourage more efficient operation, jruby is being very noisy about it) and we told you how you can use specific method signatures using java_alias to shut up jruby (and be more efficient). However background it could be argued (because of its ubiquity in sketches and the unique method signatures) might warrant special treatment. Here is an experiment to add such features via the helper_methods module.
# The vanilla processing background method is highly overloaded, a single
# argument could be an instance of PImage, int or float (multiple arguments are
# usually taken to be floats).
# @param *args arguments for background as an Array

def background *args
  if (args.length == 1)
    if args[0].kind_of? Integer
      self.java_send :background, [Java::int], args[0]
    elsif args[0].kind_of? Float
      self.java_send :background, [Java::float], args[0]
    elsif args[0].kind_of? Java::ProcessingCore::PImage
      self.java_send :background, [Java::ProcessingCore::PImage], args[0]
  elsif (args.length == 3)
    self.java_send :background, [Java::float, Java::float, Java::float], args[0], args[1], args[2]
  elsif (args.length == 2)  # greyscale with transparency
    self.java_send :background, [Java::float, Java::float], args[0], args[1]
  elsif (args.length == 4) # color with transparency
    self.java_send :background, [Java::float, Java::float, Java::float, Java::float], args[0], args[1], args[2], args[3]

JRubyArt is the development branch of ruby-processing.

No comments:

Post a Comment


About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby- and processing-3.2.2