Method: Pixelart::ImageComposite#initialize

Defined in:
lib/pixelart/composite.rb

#initialize(*args, **kwargs) ⇒ ImageComposite

Returns a new instance of ImageComposite.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pixelart/composite.rb', line 17

def initialize( *args, **kwargs )
  @tile_width  = kwargs[:width]  || kwargs[:tile_width]  || TILE_WIDTH
  @tile_height = kwargs[:height] || kwargs[:tile_height] || TILE_HEIGHT

  ## check for background
  background = kwargs[:background] || kwargs[:tile_background]

  if background
    ## wrap into an array if not already an array
    ##  and convert all colors to true rgba colors as integer numbers
    background = [background]   unless background.is_a?( Array )
    @background_colors = background.map { |color| Color.parse( color ) }
  else
    ## todo/check: use empty array instead of nil - why? why not?
    @background_colors = nil
  end


  ## todo/fix: check type - args[0] is Image!!!
  if args.size == 1   ## assume "copy" c'tor with passed in image
     img = args[0]    ## pass image through as-is

     @tile_cols  = img.width  / @tile_width       ## e.g. 2400/24 = 100
     @tile_rows  = img.height / @tile_height      ## e.g. 2400/24 = 100
     @tile_count = @tile_cols * @tile_rows       ## ## 10000 = 100x100  (2400x2400 pixel)
  elsif args.size == 2 || args.size == 0     ## cols, rows
    ## todo/fix: check type - args[0] & args[1] is Integer!!!!!
     ## todo/check - find a better name for cols/rows - why? why not?
     @tile_cols = args[0] || 3
     @tile_rows = args[1] || 3
     @tile_count = 0   # (track) current index (of added images)

     background_color = if @background_colors && @background_colors.size == 1
                           @background_colors[0]
                        else
                           0  # note: 0 is transparent (0) true color
                        end

      ## todo/check - always auto-fill complete image (even if empty/no tiles)
      ##                with background color if only one background color
      ##                   why? why not??? or always follow the "model"
      ##                                       with more than one background color???
      img = ChunkyPNG::Image.new( @tile_cols * @tile_width,
                                  @tile_rows * @tile_height,
                                  background_color )

  else
     raise ArgumentError, "cols, rows or image arguments expected; got: #{args.inspect}"
  end


  puts "     #{img.height}x#{img.width} (height x width)"

  super( nil, nil, img )
end