pixelart 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df39f58ffbca991ae1fbbdc2f4a3b83545074f7872dd96bf9b23ac5be7259a0d
4
- data.tar.gz: 865908e9a481ef8bd74c98572dbe6c17fd2c7dde2bd5f0f8613247716be4065a
3
+ metadata.gz: 0dd959d7ee982c2634f325c6e7f91bfb46d12dc61a79f364d0b7caae529d59f1
4
+ data.tar.gz: 024f66e021b395b4141c54298e2fb0f31b47d28952a5c03d56ecfaf75c68b1be
5
5
  SHA512:
6
- metadata.gz: 65305e097f184eb6e62e6d0b392f2a9e42c2a705abbb198752fdb905681bc35ac9fb263efd663bb77d5ab3cef440127e55eeee2eb33ba841fde35338dff50d63
7
- data.tar.gz: 94df6b1fced1c835ae8132922350b9d50d2fe21c29c020cc2b4595246af2d42fc0e0346bcfd6aacbc1ae45e153d58cfb5592ee90ab9c9da01b4ce07313d2b23e
6
+ metadata.gz: 71093d3b9977140303838bf9628c1de54c47f978a556945321fd849d3ebb26912d50846050c15efe3f1a2cf4d8b0991940c08f4a63511451148f63d2b9e788f5
7
+ data.tar.gz: 41c97279da0b7f81430a56c1c8e88be1ebc2bac61ea6b7a136be1accfe3cd71b7c5e0c3885d3a7dfa5d021852d4550e3f692b151767a18f6fcf7a8c9fd5340d0
data/Manifest.txt CHANGED
@@ -9,4 +9,5 @@ lib/pixelart/gradient.rb
9
9
  lib/pixelart/image.rb
10
10
  lib/pixelart/misc.rb
11
11
  lib/pixelart/palette.rb
12
+ lib/pixelart/pixelator.rb
12
13
  lib/pixelart/version.rb
data/lib/pixelart/base.rb CHANGED
@@ -15,6 +15,8 @@ require 'pixelart/gradient'
15
15
  require 'pixelart/palette'
16
16
  require 'pixelart/image'
17
17
 
18
+ require 'pixelart/pixelator'
19
+
18
20
  require 'pixelart/misc' ## misc helpers
19
21
 
20
22
 
@@ -0,0 +1,117 @@
1
+ module Pixelart
2
+
3
+
4
+ class Pixelator # or use Minifier or such - rename - why? why not?
5
+
6
+ def initialize( img, width=24, height=24 )
7
+ @img = img.is_a?( Image ) ? img.image : img ## "unwrap" if Pixelart::Image
8
+ @width = width
9
+ @height = height
10
+
11
+ ## calculate pixel size / density / resolution
12
+ ## how many pixels per pixel?
13
+ @xsize, @xoverflow = img.width.divmod( width )
14
+ @ysize, @yoverflow = img.height.divmod( height )
15
+
16
+ puts "minify image size from (#{@img.width}x#{@img.height}) to (#{width}x#{height})"
17
+ puts " pixel size (#{@xsize}x#{@ysize}) - #{@xsize*@ysize} pixel(s) per pixel"
18
+ puts " overflow x: #{@xoverflow}, y: #{@yoverflow} pixel(s)" if @xoverflow > 0 || @yoverflow > 0
19
+ end
20
+
21
+
22
+ def grid( spacing: 10 )
23
+ width = @img.width + (@width-1)*spacing
24
+ height = @img.height + (@height-1)*spacing
25
+
26
+ img = ChunkyPNG::Image.new( width, height, ChunkyPNG::Color::WHITE )
27
+
28
+ @img.width.times do |x|
29
+ xpixel = x/@xsize
30
+ @img.height.times do |y|
31
+ ypixel = y/@ysize
32
+
33
+ ## clip overflow pixels
34
+ xpixel = @width-1 if xpixel >= @width
35
+ ypixel = @height-1 if ypixel >= @height
36
+
37
+ color = @img[x,y]
38
+ img[x + spacing*xpixel,
39
+ y + spacing*ypixel] = color
40
+ end
41
+ end
42
+
43
+ Image.new( img.width, img.height, img ) ## wrap in Pixelart::Image - why? why not?
44
+ end
45
+
46
+
47
+ # pixels by coordinates (x/y) with color statistics / usage
48
+ def pixels
49
+ @pixels ||= begin
50
+ pixels = []
51
+ @img.width.times do |x|
52
+ xpixel = x/@xsize
53
+ @img.height.times do |y|
54
+ ypixel = y/@ysize
55
+
56
+ ## skip/cut off overflow pixels
57
+ next if xpixel >= @width || ypixel >= @height
58
+
59
+ color = @img[x,y]
60
+ colors = pixels[xpixel+ypixel*@width] ||= Hash.new(0)
61
+ colors[ color ] += 1
62
+ end
63
+ end
64
+
65
+ ## sort pixel colors by usage / count (highest first)
66
+ pixels = pixels.map do |pixel|
67
+ pixel.sort do |l,r|
68
+ r[1] <=> l[1]
69
+ end.to_h
70
+ end
71
+ pixels
72
+ end
73
+ end
74
+
75
+ def pixel(x,y) pixels[x+y*@width]; end
76
+ alias_method :[], :pixel
77
+
78
+
79
+ def can_pixelate?
80
+ # check if any pixel has NOT a color with a 50% majority?
81
+ count = 0
82
+ @width.times do |x|
83
+ @height.times do |y|
84
+ pixel = pixel( x, y )
85
+ sum = pixel.values.sum
86
+ color_count = pixel.values[0]
87
+ if color_count < (sum/2)
88
+ count += 1
89
+ ## todo/check: stor warn in a public errors or warns array - why? why not?
90
+ puts "!! WARN #{count} - pixel (#{x}/#{y}) - no majority (50%) color:"
91
+ pp pixel
92
+ end
93
+ end
94
+ end
95
+
96
+ count == 0 ## return true if not warnings found
97
+ end
98
+ alias_method :pixelate?, :can_pixelate?
99
+
100
+
101
+ def pixelate
102
+ img = ChunkyPNG::Image.new( @width, @height )
103
+
104
+ @width.times do |x|
105
+ @height.times do |y|
106
+ pixel = pixel( x, y )
107
+ color = pixel.keys[0]
108
+ img[x,y] = color
109
+ end
110
+ end
111
+
112
+ Image.new( img.width, img.height, img ) ## wrap in Pixelart::Image - why? why not?
113
+ end
114
+ end # class Pixelator
115
+ end # module Pixelart
116
+
117
+
@@ -3,7 +3,7 @@ module Pixelart
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- PATCH = 6
6
+ PATCH = 7
7
7
  VERSION = [MAJOR,MINOR,PATCH].join('.')
8
8
 
9
9
  def self.version
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixelart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-20 00:00:00.000000000 Z
11
+ date: 2021-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chunky_png
@@ -79,6 +79,7 @@ files:
79
79
  - lib/pixelart/image.rb
80
80
  - lib/pixelart/misc.rb
81
81
  - lib/pixelart/palette.rb
82
+ - lib/pixelart/pixelator.rb
82
83
  - lib/pixelart/version.rb
83
84
  homepage: https://github.com/rubycoco/pixel
84
85
  licenses: