pixelflow_canvas 0.7.2 → 0.7.4

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: 419e160a5f8978c34f3fa8fbb79c399747cb628267e32dacdcba7ac9276681c5
4
- data.tar.gz: 4017536c4d4c091b861248367964a04df890904517135194d971df0d14703283
3
+ metadata.gz: '0039ad3dacd3960e15c81934677749effbbc94bd7c45ccb5ec35c33d9e5eec62'
4
+ data.tar.gz: 58deb104264ee55c51db4b0e3c6a4bfc696920db5b9e85e02ee8436cf5d3959c
5
5
  SHA512:
6
- metadata.gz: 682317f125eef34904ce479f8b95ce4bf9cfc18db7f674d20dbee9504dbb3602d103adad43b0c35ec5f1630b4c0e580c6e99c6f6359ec3db200314c0819b4c2b
7
- data.tar.gz: 2fcd08dedd0dfeb7ba71eb8608b72403c6794761541578cb1bcfb49188db055aa16898587d5853331b4847552ebe3c52df73489746260c311d4c033b73221a3b
6
+ metadata.gz: 46555159d1dc7472c30de1016d1c4873be9aac23c2ddb271902a876d8b11dfc2f271fd2dbac396b0464464719bac41e1ced25052e38c93feaca0a64c439cdd2d
7
+ data.tar.gz: 3a252f688b843513390e2ccdcd4fbaa6b197df91d622ec43d0763515f2f41b83a5333be7c194f4d7ac5eeb7466fa282c394095d79c25e361e056145e2a0655d2
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PixelflowCanvas
4
- VERSION = "0.7.2"
4
+ VERSION = "0.7.4"
5
5
  end
@@ -39,6 +39,7 @@ module Pixelflow
39
39
  @r = color_mode == :palette ? 15 : 255
40
40
  @g = color_mode == :palette ? 0 : 255
41
41
  @b = color_mode == :palette ? 0 : 255
42
+ @mask = nil
42
43
  @color_mode = :rgb
43
44
  @advance_mode = :right
44
45
  @draw_mode = :direct
@@ -53,7 +54,7 @@ module Pixelflow
53
54
  end
54
55
  end
55
56
 
56
- attr_reader :width, :height
57
+ attr_reader :width, :height, :color_mode
57
58
 
58
59
  def run(&block)
59
60
  instance_eval(&block)
@@ -147,9 +148,9 @@ module Pixelflow
147
148
  (0...@height).each do |y|
148
149
  (0...@width).each do |x|
149
150
  i = @screen[source]
150
- buffer[offset + 0] = @palette[i + 0]
151
- buffer[offset + 1] = @palette[i + 1]
152
- buffer[offset + 2] = @palette[i + 2]
151
+ buffer[offset + 0] = @palette[i * 3+ 0]
152
+ buffer[offset + 1] = @palette[i * 3 + 1]
153
+ buffer[offset + 2] = @palette[i * 3 + 2]
153
154
  source += 1
154
155
  offset += 3
155
156
  end
@@ -159,7 +160,46 @@ module Pixelflow
159
160
  end
160
161
  end
161
162
 
163
+ class MaskGenerator
164
+ def initialize(canvas)
165
+ @canvas = canvas
166
+ @mask = [false] * canvas.width * canvas.height
167
+ end
168
+
169
+ attr_reader :mask
170
+
171
+ def add_color(r = nil, g = nil, b = nil)
172
+ offset = 0
173
+ (0...@canvas.height).each do |y|
174
+ (0...@canvas.width).each do |x|
175
+ if @canvas.color_mode == :rgb
176
+ if @canvas.get_pixel(x, y) == [r, g, b]
177
+ @mask[offset] = true
178
+ end
179
+ else
180
+ if @canvas.get_pixel(x, y) == r
181
+ @mask[offset] = true
182
+ end
183
+ end
184
+ offset += 1
185
+ end
186
+ end
187
+ end
188
+ end
189
+
190
+ def set_mask(&block)
191
+ @mask = MaskGenerator.new(self)
192
+ @mask.instance_eval(&block)
193
+ end
194
+
195
+ def remove_mask()
196
+ @mask = nil
197
+ end
198
+
162
199
  def set_color(r, g = 0, b = 0)
200
+ r = r.to_i.clamp(0, 255)
201
+ g = g.to_i.clamp(0, 255)
202
+ b = b.to_i.clamp(0, 255)
163
203
  @r = r
164
204
  @g = g
165
205
  @b = b
@@ -218,14 +258,15 @@ module Pixelflow
218
258
  r ||= @r
219
259
  g ||= @g
220
260
  b ||= @b
221
- x0 = x0.to_i
222
- y0 = y0.to_i
223
- x1 = x1.to_i
224
- y1 = y1.to_i
261
+ x = x.to_i
262
+ y = y.to_i
225
263
  return if x < 0 || x >= @width || y < 0 || y >= @height
226
264
  unless x == @x && y == @y
227
265
  move_to(x, y)
228
266
  end
267
+ if @mask
268
+ return unless @mask.mask[y * @width + x]
269
+ end
229
270
  if @color_mode == :rgb
230
271
  offset = (@y * @width + @x) * 3
231
272
  if @compose_mode == :add
@@ -267,10 +308,8 @@ module Pixelflow
267
308
  end
268
309
 
269
310
  def get_pixel(x, y)
270
- x0 = x0.to_i
271
- y0 = y0.to_i
272
- x1 = x1.to_i
273
- y1 = y1.to_i
311
+ x = x.to_i
312
+ y = y.to_i
274
313
  return 0 if x < 0 || x >= @width || y < 0 || y >= @height
275
314
  if @color_mode == :rgb
276
315
  return @screen[(y * @width + x) * 3, 3]
@@ -480,6 +519,54 @@ module Pixelflow
480
519
  end
481
520
  end
482
521
 
522
+ def draw_arc(x, y, r, a0, a1, close_path = false)
523
+ # x = x.to_i
524
+ # y = y.to_i
525
+ # r = r.to_i
526
+ a0 = a0.to_f
527
+ a1 = a1.to_f
528
+ a0 = a0 * Math::PI / 180.0
529
+ a1 = a1 * Math::PI / 180.0
530
+ da = 1.0 / r
531
+ a = a0
532
+ while a < a1
533
+ x0 = x + r * Math.cos(a)
534
+ y0 = y + r * Math.sin(a)
535
+ a += da
536
+ x1 = x + r * Math.cos(a)
537
+ y1 = y + r * Math.sin(a)
538
+ draw_line(x0, y0, x1, y1)
539
+ end
540
+ if close_path
541
+ x0 = x + r * Math.cos(a0)
542
+ y0 = y + r * Math.sin(a0)
543
+ draw_line(x, y, x0, y0)
544
+ x0 = x + r * Math.cos(a1)
545
+ y0 = y + r * Math.sin(a1)
546
+ draw_line(x, y, x0, y0)
547
+ end
548
+ end
549
+
550
+ def fill_arc(x, y, r, a0, a1)
551
+ # x = x.to_i
552
+ # y = y.to_i
553
+ # r = r.to_i
554
+ a0 = a0.to_f
555
+ a1 = a1.to_f
556
+ a0 = a0 * Math::PI / 180.0
557
+ a1 = a1 * Math::PI / 180.0
558
+ da = 1.0 / r
559
+ a = a0
560
+ while a < a1
561
+ x0 = x + r * Math.cos(a)
562
+ y0 = y + r * Math.sin(a)
563
+ a += da
564
+ x1 = x + r * Math.cos(a)
565
+ y1 = y + r * Math.sin(a)
566
+ fill_triangle(x, y, x0, y0, x1, y1)
567
+ end
568
+ end
569
+
483
570
  def draw_quadratic_bezier(x0, y0, x1, y1, x2, y2, steps = 100)
484
571
  steps = steps.to_i
485
572
  xp = nil
@@ -518,6 +605,45 @@ module Pixelflow
518
605
  draw_line(x2, y2, x0, y0)
519
606
  end
520
607
 
608
+ def fill_triangle(x0, y0, x1, y1, x2, y2)
609
+ x0 = x0.to_i
610
+ y0 = y0.to_i
611
+ x1 = x1.to_i
612
+ y1 = y1.to_i
613
+ x2 = x2.to_i
614
+ y2 = y2.to_i
615
+ if y0 > y1
616
+ x0, x1 = x1, x0
617
+ y0, y1 = y1, y0
618
+ end
619
+ if y1 > y2
620
+ x1, x2 = x2, x1
621
+ y1, y2 = y2, y1
622
+ end
623
+ if y0 > y1
624
+ x0, x1 = x1, x0
625
+ y0, y1 = y1, y0
626
+ end
627
+ total_height = y2 - y0
628
+ (0...total_height).each do |i|
629
+ second_half = i > y1 - y0 || y1 == y0
630
+ segment_height = second_half ? y2 - y1 : y1 - y0
631
+ alpha = i.to_f / total_height
632
+ beta = (i - (second_half ? y1 - y0 : 0)).to_f / segment_height
633
+ ax = x0 + (x2 - x0) * alpha
634
+ ay = y0 + (y2 - y0) * alpha
635
+ bx = second_half ? x1 + (x2 - x1) * beta : x0 + (x1 - x0) * beta
636
+ by = second_half ? y1 + (y2 - y1) * beta : y0 + (y1 - y0) * beta
637
+ if ax > bx
638
+ ax, bx = bx, ax
639
+ ay, by = by, ay
640
+ end
641
+ (ax.to_i..bx.to_i).each do |j|
642
+ set_pixel(j, y0 + i)
643
+ end
644
+ end
645
+ end
646
+
521
647
  def flood_fill(x, y, r = nil, g = nil, b = nil)
522
648
  x = x.to_i
523
649
  y = y.to_i
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixelflow_canvas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Specht