Class: Pixelart::Pixelator

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

Overview

or use Minifier or such - rename - why? why not?

Instance Method Summary collapse

Constructor Details

#initialize(img, width = 24, height = 24) ⇒ Pixelator

Returns a new instance of Pixelator.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/pixelart/pixelator.rb', line 6

def initialize( img, width=24, height=24 )
  @img    = img.is_a?( Image ) ? img.image : img  ## "unwrap" if Pixelart::Image
  @width  = width
  @height = height

  ## calculate pixel size / density / resolution
  ##   how many pixels per pixel?
  @xsize, @xoverflow = img.width.divmod( width )
  @ysize, @yoverflow = img.height.divmod( height )

  puts "minify image size from (#{@img.width}x#{@img.height}) to (#{width}x#{height})"
  puts "  pixel size (#{@xsize}x#{@ysize}) - #{@xsize*@ysize} pixel(s) per pixel"
  puts "    overflow x: #{@xoverflow}, y: #{@yoverflow} pixel(s)"    if @xoverflow > 0 || @yoverflow > 0
end

Instance Method Details

#can_pixelate?Boolean Also known as: pixelate?

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pixelart/pixelator.rb', line 79

def can_pixelate?
  # check if any pixel has NOT a color with a 50% majority?
  count = 0
  @width.times do |x|
    @height.times do |y|
      pixel = pixel( x, y )
      sum         = pixel.values.sum
      color_count = pixel.values[0]
      if color_count < (sum/2)
        count += 1
        ## todo/check: stor warn in a public errors or warns array - why? why not?
        puts "!! WARN #{count} - pixel (#{x}/#{y}) - no majority (50%) color:"
        pp pixel
      end
    end
  end

  count == 0    ## return true if not warnings found
end

#grid(spacing: 10) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pixelart/pixelator.rb', line 22

def grid( spacing: 10 )
  width  = @img.width  + (@width-1)*spacing
  height = @img.height + (@height-1)*spacing

  img = ChunkyPNG::Image.new( width, height, ChunkyPNG::Color::WHITE )

  @img.width.times do |x|
    xpixel = x/@xsize
    @img.height.times do |y|
      ypixel = y/@ysize

      ## clip overflow pixels
      xpixel = @width-1   if xpixel >= @width
      ypixel = @height-1  if ypixel >= @height

      color = @img[x,y]
      img[x + spacing*xpixel,
          y + spacing*ypixel] = color
    end
  end

  Image.new( img.width, img.height, img )  ## wrap in Pixelart::Image - why? why not?
end

#pixel(x, y) ⇒ Object Also known as: []



75
# File 'lib/pixelart/pixelator.rb', line 75

def pixel(x,y)  pixels[x+y*@width]; end

#pixelateObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pixelart/pixelator.rb', line 101

def pixelate
  img = ChunkyPNG::Image.new( @width, @height )

  @width.times do |x|
    @height.times do |y|
      pixel = pixel( x, y )
      color = pixel.keys[0]
      img[x,y] = color
    end
  end

  Image.new( img.width, img.height, img )  ## wrap in Pixelart::Image - why? why not?
end

#pixelsObject

pixels by coordinates (x/y) with color statistics / usage



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pixelart/pixelator.rb', line 48

def pixels
  @pixels ||= begin
                pixels = []
                @img.width.times do |x|
                  xpixel = x/@xsize
                  @img.height.times do |y|
                    ypixel = y/@ysize

                    ## skip/cut off overflow pixels
                    next if xpixel >= @width || ypixel >= @height

                    color = @img[x,y]
                    colors = pixels[xpixel+ypixel*@width] ||= Hash.new(0)
                    colors[ color ] += 1
                  end
                end

                ## sort pixel colors by usage / count (highest first)
                pixels = pixels.map do |pixel|
                                       pixel.sort do |l,r|
                                                    r[1] <=> l[1]
                                                  end.to_h
                                    end
                pixels
              end
end