Class: Pixelart::ImagePalette8bit

Inherits:
Image
  • Object
show all
Defined in:
lib/pixelart/misc.rb

Overview

or use Palette256 alias?

Constant Summary

Constants inherited from Image

Pixelart::Image::CHARS, Pixelart::Image::PALETTE8BIT

Instance Method Summary collapse

Methods inherited from Image

#[], #[]=, #_change_colors!, #_parse_color_map, #_parse_colors, #change_colors, #change_palette8bit, #compose!, #grayscale, #height, #image, #led, parse, parse_colors, parse_pixels, #pixels, read, #save, #width, #zoom

Constructor Details

#initialize(colors, size: 1, spacing: nil) ⇒ ImagePalette8bit

Returns a new instance of ImagePalette8bit.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pixelart/misc.rb', line 5

def initialize( colors, size: 1, spacing: nil )
  ## todo/check: change size arg to pixel or such? better name/less confusing - why? why not?

  ## todo/check: assert colors MUST have 256 colors!!!!

  ## use a "smart" default if no spacing set
  ##   0 for for (pixel) size == 1
  ##   1 for the rest
  spacing = size == 1 ? 0 : 1  if spacing.nil?

  img = ChunkyPNG::Image.new( 32*size+(32-1)*spacing,
                               8*size+(8-1)*spacing )


  colors =colors.map {|color| Color.parse( color ) }

  colors.each_with_index do |color,i|
    y,x = i.divmod( 32 )
    if size > 1
      size.times do |n|
        size.times do |m|
          img[ x*size+n+spacing*x,
               y*size+m+spacing*y] = color
        end
      end
    else
      img[x,y] = color
    end
  end

  super( img.width, img.height, img )
end