Method: Pixelart::Pixelator#pixels

Defined in:
lib/pixelart/pixelator.rb

#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