Class: Pixelart::ImageComposite
- Defined in:
- lib/pixelart/composite.rb
Overview
check: (re)name to Collage, Sheet, Sprites, or such?
Constant Summary collapse
- TILE_WIDTH =
default tile width / height in pixel – check: (re)name to sprite or such? why? why not?
24
- TILE_HEIGHT =
24
Constants inherited from Image
Pixelart::Image::CHARS, Pixelart::Image::PALETTE8BIT, Pixelart::Image::RAINBOW_BLUE, Pixelart::Image::RAINBOW_GREEN, Pixelart::Image::RAINBOW_ORANGE, Pixelart::Image::RAINBOW_RED, Pixelart::Image::RAINBOW_VIOLET, Pixelart::Image::RAINBOW_YELLOW, Pixelart::Image::UKRAINE_BLUE, Pixelart::Image::UKRAINE_YELLOW
Class Method Summary collapse
-
.read(path, width: TILE_WIDTH, height: TILE_WIDTH) ⇒ Object
convenience helper.
Instance Method Summary collapse
-
#[](*args) ⇒ Object
overload - why? why not?.
-
#_add(image) ⇒ Object
set / add tile.
-
#add(image_or_images) ⇒ Object
(also: #<<)
note: allow adding of image OR array of images.
- #count ⇒ Object (also: #size, #tile_count)
-
#each(&block) ⇒ Object
convenience helpers to loop over composite.
- #each_with_index(&block) ⇒ Object
-
#initialize(*args, **kwargs) ⇒ ImageComposite
constructor
A new instance of ImageComposite.
-
#tile(index) ⇒ Object
get tile.
- #tile_height ⇒ Object
- #tile_width ⇒ Object
Methods inherited from Image
#[]=, #_change_colors!, #_parse_color_map, #_parse_colors, blob, #blur, calc_sample_steps, calc_stripes, #change_colors, #change_palette8bit, #circle, #compose!, convert, #crop, #flip_horizontally, #grayscale, #height, #image, inherited, #invert, #led, #left, #mirror, parse, parse_base64, parse_colors, parse_pixels, parse_pixels_strict, #pixels, #rainbow, #rotate_clockwise, #rotate_counter_clockwise, #sample, #sample_debug, #save, #silhouette, #sketch, #spots, #spots_hidef, #stripes_horizontal, subclasses, #to_blob, #transparent, #ukraine, #width, #zoom
Constructor Details
#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 |
Class Method Details
.read(path, width: TILE_WIDTH, height: TILE_WIDTH) ⇒ Object
convenience helper
10 11 12 13 14 |
# File 'lib/pixelart/composite.rb', line 10 def self.read( path, width: TILE_WIDTH, height: TILE_WIDTH ) ## convenience helper img = ChunkyPNG::Image.from_file( path ) new( img, width: width, height: height ) end |
Instance Method Details
#[](*args) ⇒ Object
overload - why? why not?
129 130 131 132 133 134 135 136 |
# File 'lib/pixelart/composite.rb', line 129 def []( *args ) ## overload - why? why not? if args.size == 1 index = args[0] tile( index ) else super ## e.g [x,y] --- get pixel end end |
#_add(image) ⇒ Object
set / add tile
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/pixelart/composite.rb', line 85 def _add( image ) y, x = @tile_count.divmod( @tile_cols ) puts " [#{@tile_count}] @ (#{x}/#{y}) #{image.width}x#{image.height} (height x width)" ## note: only used if more than one background color specified ## needs to cycle through if @background_colors && @background_colors.size > 1 i = x + y*@tile_cols ## note: cycle through background color for now background_color = @background_colors[i % @background_colors.size] background = Image.new( @tile_width, @tile_height, background_color ) ## todo/chekc: use "raw" ChunkyPNG:Image here - why? why not? background.compose!( image ) image = background ## switch - make image with background new image end ## note: image.image - "unwrap" the "raw" ChunkyPNG::Image @img.compose!( image.image, x*@tile_width, y*@tile_height ) @tile_count += 1 end |
#add(image_or_images) ⇒ Object Also known as: <<
note: allow adding of image OR array of images
107 108 109 110 111 112 113 114 115 |
# File 'lib/pixelart/composite.rb', line 107 def add( image_or_images ) ## note: allow adding of image OR array of images if image_or_images.is_a?( Array ) images = image_or_images images.each { |image| _add( image ) } else image = image_or_images _add( image ) end end |
#count ⇒ Object Also known as: size, tile_count
74 |
# File 'lib/pixelart/composite.rb', line 74 def count() @tile_count; end |
#each(&block) ⇒ Object
convenience helpers to loop over composite
140 141 142 143 144 |
# File 'lib/pixelart/composite.rb', line 140 def each( &block ) count.times do |i| block.call( tile( i ) ) end end |
#each_with_index(&block) ⇒ Object
146 147 148 149 150 |
# File 'lib/pixelart/composite.rb', line 146 def each_with_index( &block ) count.times do |i| block.call( tile( i ), i ) end end |
#tile(index) ⇒ Object
get tile
123 124 125 126 127 |
# File 'lib/pixelart/composite.rb', line 123 def tile( index ) y, x = index.divmod( @tile_cols ) img = @img.crop( x*@tile_width, y*@tile_height, @tile_width, @tile_height ) Image.new( img.width, img.height, img ) ## wrap in pixelart image end |
#tile_height ⇒ Object
79 |
# File 'lib/pixelart/composite.rb', line 79 def tile_height() @tile_height; end |
#tile_width ⇒ Object
78 |
# File 'lib/pixelart/composite.rb', line 78 def tile_width() @tile_width; end |