rubysketch 0.2.0 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c2d5e6b27dba3d165c946db3fc7cbe09630be01a5dd86238433e6e3b7126138
4
- data.tar.gz: b0565c883e6821f133d64428507fc7d39eaf06f5a11fb634a1a205b25856d77b
3
+ metadata.gz: aeebec01d05ee86260f24da80e85d4ac04bb41029145d8d47e7acc800919e00d
4
+ data.tar.gz: d227536ed72e88ef159c347c8ace10ff20532d52cbeb4588956f0c6e794283d7
5
5
  SHA512:
6
- metadata.gz: 63553c43e5fd2433e7b395aae2b57415b9cc7091a8cbe07934fddede1569383161e73822f6614daa479dde3ef1606d33c545b067ceefbd66ce0c7133149f0ac1
7
- data.tar.gz: be2980a7909244174e52f619ec1e80a77a85483501837e59a5ddea84652792b3718402d1ca9a1201058847770dca9a34e315fbdf58ec2033794bd6438100a12e
6
+ metadata.gz: 9e144994fd11251d589853d0c6439b2d9924a4e76751b6c6147ad1bc4ebb6081d720f1bca5a77b9c1e69228fb4ab87b4807d463db3ca565243a1261f098caed1
7
+ data.tar.gz: fd03275e8730519d1095d60f1dbbca5a33ddc93f2e412ff32548f22c678cbe614bdd42fd9166de7c8dac9370d698a95482c2f71575d8c6bdac581d57bd5722fb
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.5
@@ -0,0 +1,13 @@
1
+ %w[xot rays reflex rubysketch]
2
+ .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
+
5
+ require 'rubysketch-processing'
6
+
7
+
8
+ icon = loadImage 'https://xord.org/rubysketch/rubysketch.png'
9
+
10
+ draw do
11
+ background 0, 10
12
+ image icon, mouseX, mouseY, icon.width / 10, icon.height / 10
13
+ end
@@ -1,3 +1,8 @@
1
+ require 'digest/sha1'
2
+ require 'pathname'
3
+ require 'tmpdir'
4
+ require 'open-uri'
5
+
1
6
  require 'reflex'
2
7
  require 'rubysketch/module'
3
8
  require 'rubysketch/starter'
@@ -294,7 +294,7 @@ module RubySketch
294
294
  end
295
295
 
296
296
  # @private
297
- def setupDrawBlock__ ()
297
+ private def setupDrawBlock__ ()
298
298
  @window__.draw = proc do |e, painter|
299
299
  @painter__ = painter
300
300
  @matrixStack__.clear
@@ -323,7 +323,7 @@ module RubySketch
323
323
  end
324
324
 
325
325
  # @private
326
- def setupMousePressedBlock__ ()
326
+ private def setupMousePressedBlock__ ()
327
327
  @window__.pointer_down = proc do |e|
328
328
  updateMouseState__ e.x, e.y, true
329
329
  @mousePressedBlock__.call e if @mousePressedBlock__
@@ -336,7 +336,7 @@ module RubySketch
336
336
  end
337
337
 
338
338
  # @private
339
- def setupMouseReleasedBlock__ ()
339
+ private def setupMouseReleasedBlock__ ()
340
340
  @window__.pointer_up = proc do |e|
341
341
  updateMouseState__ e.x, e.y, false
342
342
  @mouseReleasedBlock__.call e if @mouseReleasedBlock__
@@ -349,7 +349,7 @@ module RubySketch
349
349
  end
350
350
 
351
351
  # @private
352
- def setupMouseMovedBlock__ ()
352
+ private def setupMouseMovedBlock__ ()
353
353
  @window__.pointer_move = proc do |e|
354
354
  updateMouseState__ e.x, e.y
355
355
  @mouseMovedBlock__.call e if @mouseMovedBlock__
@@ -362,7 +362,7 @@ module RubySketch
362
362
  end
363
363
 
364
364
  # @private
365
- def setupMouseDraggedBlock__ ()
365
+ private def setupMouseDraggedBlock__ ()
366
366
  @window__.pointer_drag = proc do |e|
367
367
  updateMouseState__ e.x, e.y
368
368
  @mouseDraggedBlock__.call e if @mouseDraggedBlock__
@@ -375,20 +375,20 @@ module RubySketch
375
375
  end
376
376
 
377
377
  # @private
378
- def updateMouseState__ (x, y, pressed = nil)
378
+ private def updateMouseState__ (x, y, pressed = nil)
379
379
  @mouseX__ = x
380
380
  @mouseY__ = y
381
381
  @mousePressed__ = pressed if pressed != nil
382
382
  end
383
383
 
384
384
  # @private
385
- def updateMousePrevPos__ ()
385
+ private def updateMousePrevPos__ ()
386
386
  @mousePrevX__ = @mouseX__
387
387
  @mousePrevY__ = @mouseY__
388
388
  end
389
389
 
390
390
  # @private
391
- def size__ (width, height)
391
+ private def size__ (width, height)
392
392
  raise 'size() must be called on startup or setup block' if @started__
393
393
 
394
394
  @painter__.send :end_paint
@@ -487,7 +487,7 @@ module RubySketch
487
487
  # @return [nil] nil
488
488
  #
489
489
  def colorMode (mode, *maxes)
490
- raise ArgumentError, "Invalid color mode: #{mode}" unless [RGB, HSB].include?(mode)
490
+ raise ArgumentError, "invalid color mode: #{mode}" unless [RGB, HSB].include?(mode)
491
491
  raise ArgumentError unless [0, 1, 3, 4].include?(maxes.size)
492
492
 
493
493
  @hsbColor__ = mode.upcase == HSB
@@ -499,9 +499,9 @@ module RubySketch
499
499
  end
500
500
 
501
501
  # @private
502
- def to_rgba__ (*args)
502
+ private def toRGBA__ (*args)
503
503
  a, b, c, d = args
504
- return parse_color__(a, b || alphaMax__) if a.kind_of?(String)
504
+ return parseColor__(a, b || alphaMax__) if a.kind_of?(String)
505
505
 
506
506
  rgba = case args.size
507
507
  when 1, 2 then [a, a, a, b || alphaMax__]
@@ -514,16 +514,16 @@ module RubySketch
514
514
  end
515
515
 
516
516
  # @private
517
- def parse_color__ (str, alpha)
517
+ private def parseColor__ (str, alpha)
518
518
  result = str.match /^\s*##{'([0-9a-f]{2})' * 3}\s*$/i
519
- raise ArgumentError, "Invalid color code: '#{str}'" unless result
519
+ raise ArgumentError, "invalid color code: '#{str}'" unless result
520
520
 
521
521
  rgb = result[1..3].map.with_index {|hex, i| hex.to_i(16) / 255.0}
522
522
  return *rgb, (alpha / alphaMax__)
523
523
  end
524
524
 
525
525
  # @private
526
- def alphaMax__ ()
526
+ private def alphaMax__ ()
527
527
  @colorMaxes__[3]
528
528
  end
529
529
 
@@ -537,13 +537,13 @@ module RubySketch
537
537
  @angleScale__ = case mode
538
538
  when RADIANS then RAD2DEG__
539
539
  when DEGREES then 1.0
540
- else raise ArgumentError, "Invalid angle mode: #{mode}"
540
+ else raise ArgumentError, "invalid angle mode: #{mode}"
541
541
  end
542
542
  nil
543
543
  end
544
544
 
545
545
  # @private
546
- def to_angle__ (angle)
546
+ private def toAngle__ (angle)
547
547
  angle * @angleScale__
548
548
  end
549
549
 
@@ -578,7 +578,7 @@ module RubySketch
578
578
  end
579
579
 
580
580
  # @private
581
- def to_xywh__ (mode, a, b, c, d)
581
+ private def toXYWH__ (mode, a, b, c, d)
582
582
  case mode
583
583
  when CORNER then [a, b, c, d]
584
584
  when CORNERS then [a, b, c - a, d - b]
@@ -607,7 +607,7 @@ module RubySketch
607
607
  # @return [nil] nil
608
608
  #
609
609
  def background (*args)
610
- rgba = to_rgba__ *args
610
+ rgba = toRGBA__ *args
611
611
  if rgba[3] == 1
612
612
  @painter__.background *rgba
613
613
  else
@@ -637,7 +637,7 @@ module RubySketch
637
637
  # @return [nil] nil
638
638
  #
639
639
  def fill (*args)
640
- @painter__.fill(*to_rgba__(*args))
640
+ @painter__.fill(*toRGBA__(*args))
641
641
  nil
642
642
  end
643
643
 
@@ -660,7 +660,7 @@ module RubySketch
660
660
  # @return [nil] nil
661
661
  #
662
662
  def stroke (*args)
663
- @painter__.stroke(*to_rgba__(*args))
663
+ @painter__.stroke(*toRGBA__(*args))
664
664
  nil
665
665
  end
666
666
 
@@ -763,7 +763,7 @@ module RubySketch
763
763
  # @return [nil] nil
764
764
  #
765
765
  def rect (a, b, c, d, *args)
766
- x, y, w, h = to_xywh__ @rectMode__, a, b, c, d
766
+ x, y, w, h = toXYWH__ @rectMode__, a, b, c, d
767
767
  case args.size
768
768
  when 0 then @painter__.rect x, y, w, h
769
769
  when 1 then @painter__.rect x, y, w, h, round: args[0]
@@ -783,7 +783,7 @@ module RubySketch
783
783
  # @return [nil] nil
784
784
  #
785
785
  def ellipse (a, b, c, d)
786
- x, y, w, h = to_xywh__ @ellipseMode__, a, b, c, d
786
+ x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
787
787
  @painter__.ellipse x, y, w, h
788
788
  nil
789
789
  end
@@ -812,9 +812,9 @@ module RubySketch
812
812
  # @return [nil] nil
813
813
  #
814
814
  def arc (a, b, c, d, start, stop)
815
- x, y, w, h = to_xywh__ @ellipseMode__, a, b, c, d
816
- start = to_angle__ start
817
- stop = to_angle__ stop
815
+ x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
816
+ start = toAngle__ start
817
+ stop = toAngle__ stop
818
818
  @painter__.ellipse x, y, w, h, from: start, to: stop
819
819
  nil
820
820
  end
@@ -881,6 +881,26 @@ module RubySketch
881
881
  nil
882
882
  end
883
883
 
884
+ # Draws an image.
885
+ #
886
+ # @overload image(img, x, y)
887
+ # @overload image(img, x, y, w, h)
888
+ #
889
+ # @param img [Image] image to draw
890
+ # @param x [Numeric] horizontal position of the image
891
+ # @param y [Numeric] vertical position of the image
892
+ # @param w [Numeric] width of the image
893
+ # @param h [Numeric] height of the image
894
+ #
895
+ # @return [nil] nil
896
+ #
897
+ def image (img, x, y, w = nil, h = nil)
898
+ w ||= img.width
899
+ h ||= img.height
900
+ @painter__.image img.internal, x, y, w, h
901
+ nil
902
+ end
903
+
884
904
  # Applies translation matrix to current transformation matrix.
885
905
  #
886
906
  # @param x [Numeric] horizontal transformation
@@ -916,7 +936,7 @@ module RubySketch
916
936
  # @return [nil] nil
917
937
  #
918
938
  def rotate (angle)
919
- @painter__.rotate to_angle__ angle
939
+ @painter__.rotate toAngle__ angle
920
940
  nil
921
941
  end
922
942
 
@@ -934,7 +954,7 @@ module RubySketch
934
954
  # @return [nil] nil
935
955
  #
936
956
  def popMatrix ()
937
- raise "Matrix stack underflow" if @matrixStack__.empty?
957
+ raise "matrix stack underflow" if @matrixStack__.empty?
938
958
  @painter__.matrix = @matrixStack__.pop
939
959
  nil
940
960
  end
@@ -972,7 +992,7 @@ module RubySketch
972
992
  # @return [nil] nil
973
993
  #
974
994
  def popStyle ()
975
- raise "Style stack underflow" if @styleStack__.empty?
995
+ raise "style stack underflow" if @styleStack__.empty?
976
996
  @painter__.fill,
977
997
  @painter__.stroke,
978
998
  @painter__.stroke_width,
@@ -1007,19 +1027,97 @@ module RubySketch
1007
1027
  #
1008
1028
  # @overload noise(x)
1009
1029
  # @overload noise(x, y)
1030
+ # @overload noise(x, y, z)
1010
1031
  #
1011
1032
  # @param x [Numeric] horizontal point in noise space
1012
1033
  # @param y [Numeric] vertical point in noise space
1034
+ # @param z [Numeric] depth point in noise space
1013
1035
  #
1014
1036
  # @return [Numeric] noise value (0.0..1.0)
1015
1037
  #
1016
- def noise (x, y = 0)
1017
- Rays.perlin(x, y) / 2.0 + 1.0
1038
+ def noise (x, y = 0, z = 0)
1039
+ Rays.perlin(x, y, z) / 2.0 + 0.5
1040
+ end
1041
+
1042
+ # Loads image.
1043
+ #
1044
+ # @param filename [String] file name to load image
1045
+ # @param extension [String] type of image to load (ex. 'png')
1046
+ #
1047
+ # @return [Image] loaded image object
1048
+ #
1049
+ def loadImage (filename, extension = nil)
1050
+ filename = getImage__ filename, extension if filename =~ %r|^https?://|
1051
+ Image.new Rays::Image.load filename
1052
+ end
1053
+
1054
+ # @private
1055
+ private def getImage__ (uri, ext)
1056
+ ext ||= File.extname uri
1057
+ raise "unsupported image type -- #{ext}" unless ext =~ /^\.?(png)$/i
1058
+
1059
+ tmpdir = Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
1060
+ path = tmpdir + Digest::SHA1.hexdigest(uri)
1061
+ path = path.sub_ext ext
1062
+
1063
+ unless path.file?
1064
+ URI.open uri do |input|
1065
+ tmpdir.mkdir unless tmpdir.directory?
1066
+ path.open('w') do |output|
1067
+ while buf = input.read(2 ** 16)
1068
+ output.write buf
1069
+ end
1070
+ end
1071
+ end
1072
+ end
1073
+ path.to_s
1018
1074
  end
1019
1075
 
1020
1076
  end# Processing
1021
1077
 
1022
1078
 
1079
+ # Image object.
1080
+ #
1081
+ class Processing::Image
1082
+
1083
+ # Initialize image.
1084
+ #
1085
+ def initialize (image)
1086
+ @image = image
1087
+ end
1088
+
1089
+ # Gets width of image.
1090
+ #
1091
+ # @return [Numeric] width of image
1092
+ #
1093
+ def width ()
1094
+ @image.width
1095
+ end
1096
+
1097
+ # Gets height of image.
1098
+ #
1099
+ # @return [Numeric] height of image
1100
+ #
1101
+ def height ()
1102
+ @image.height
1103
+ end
1104
+
1105
+ # Saves image to file.
1106
+ #
1107
+ # @param filename [String] file name to save image
1108
+ #
1109
+ def save (filename)
1110
+ @image.save filename
1111
+ end
1112
+
1113
+ # @private
1114
+ def internal ()
1115
+ @image
1116
+ end
1117
+
1118
+ end# Processing::Image
1119
+
1120
+
1023
1121
  # Font object.
1024
1122
  #
1025
1123
  class Processing::Font
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysketch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-05 00:00:00.000000000 Z
11
+ date: 2020-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -110,6 +110,7 @@ files:
110
110
  - examples/clock.rb
111
111
  - examples/glsl.rb
112
112
  - examples/hello.rb
113
+ - examples/image.rb
113
114
  - examples/shapes.rb
114
115
  - lib/rubysketch-processing.rb
115
116
  - lib/rubysketch.rb