11# frozen_string_literal: false
2+
23# processing module wrapper
34require_relative 'helpers/numeric'
45module Processing
@@ -52,6 +53,7 @@ def hsb_color(hue, sat, brightness)
5253
5354 def color ( *args )
5455 return super ( *args ) unless args . length == 1
56+
5557 super ( hex_color ( args [ 0 ] ) )
5658 end
5759
@@ -66,11 +68,8 @@ def int_to_ruby_colors(hex)
6668 # Overrides Processing convenience function thread, which takes a String
6769 # arg (for a function) to more rubylike version, takes a block...
6870 def thread ( &block )
69- if block_given?
70- Thread . new ( &block )
71- else
72- raise ArgumentError , 'thread must be called with a block' , caller
73- end
71+ warn 'you must provide a block' unless block_given?
72+ Java ::JavaLang ::Thread . new ( &block ) . start
7473 end
7574
7675 # explicitly provide 'processing.org' min instance method
@@ -94,9 +93,9 @@ def max(*args)
9493 def dist ( *args )
9594 case args . length
9695 when 4
97- return dist2d ( *args )
96+ dist2d ( *args )
9897 when 6
99- return dist3d ( *args )
98+ dist3d ( *args )
10099 else
101100 raise ArgumentError , 'takes 4 or 6 parameters'
102101 end
@@ -117,7 +116,7 @@ def find_method(method_name)
117116 # Proxy over a list of Java declared fields that have the same name as
118117 # some methods. Add to this list as needed.
119118 def proxy_java_fields
120- fields = %w( key frameRate mousePressed keyPressed )
119+ fields = %w[ key frameRate mousePressed keyPressed ]
121120 methods = fields . map { |field | java_class . declared_field ( field ) }
122121 @declared_fields = Hash [ fields . zip ( methods ) ]
123122 end
@@ -163,6 +162,7 @@ def save_strings(filename, strings)
163162 # frame_rate needs to support reading and writing
164163 def frame_rate ( fps = nil )
165164 return @declared_fields [ 'frameRate' ] . value ( java_self ) unless fps
165+
166166 super ( fps )
167167 end
168168
@@ -178,19 +178,17 @@ def key_pressed?
178178
179179 private
180180
181- INTEGER_COL = -> ( x ) { x . is_a? ( Integer ) }
182- STRING_COL = -> ( x ) { x . is_a? ( String ) }
183- FLOAT_COL = -> ( x ) { x . is_a? ( Float ) }
184181 # parse single argument color int/double/String
185- def hex_color ( a )
186- case a
187- when INTEGER_COL
188- Java ::Monkstone ::ColorUtil . colorLong ( a )
189- when STRING_COL
190- return Java ::Monkstone ::ColorUtil . colorString ( a ) if a =~ /#\h +/
191- raise StandardError , 'Dodgy Hexstring'
192- when FLOAT_COL
193- Java ::Monkstone ::ColorUtil . colorDouble ( a )
182+ def hex_color ( arg )
183+ case arg
184+ when Integer
185+ Java ::Monkstone ::ColorUtil . colorLong ( arg )
186+ when String
187+ raise StandardError , 'Dodgy Hexstring' unless arg . match ( /#\h {6}$/ )
188+
189+ Java ::Monkstone ::ColorUtil . colorString ( arg )
190+ when Float
191+ Java ::Monkstone ::ColorUtil . colorDouble ( arg )
194192 else
195193 raise StandardError , 'Dodgy Color Conversion'
196194 end
@@ -200,6 +198,7 @@ def dist2d(*args)
200198 dx = args [ 0 ] - args [ 2 ]
201199 dy = args [ 1 ] - args [ 3 ]
202200 return 0 if dx . abs < EPSILON && dy . abs < EPSILON
201+
203202 Math . hypot ( dx , dy )
204203 end
205204
@@ -208,6 +207,7 @@ def dist3d(*args)
208207 dy = args [ 1 ] - args [ 4 ]
209208 dz = args [ 2 ] - args [ 5 ]
210209 return 0 if dx . abs < EPSILON && dy . abs < EPSILON && dz . abs < EPSILON
210+
211211 Math . sqrt ( dx * dx + dy * dy + dz * dz )
212212 end
213213 end
0 commit comments