pixelart 0.1.0 → 0.1.1

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: 61a062dde7cebbcee3dfcf1f39e4ff384961bd773736f54c4959efc8d7c0a768
4
- data.tar.gz: 0d533bfef5d9b4cc13c3323a64acba128a39ba0d97d5c87748033c7c2f2ceba6
3
+ metadata.gz: 68fa84133edc2cb7f9742514af4ef188e1e358b3ae408f6ffd282c4c815e1ae5
4
+ data.tar.gz: 2048f7c4e4a29cf6aff876b273c25a58bb3c2ef1c9836756847c13e59f381d18
5
5
  SHA512:
6
- metadata.gz: 3dccdefa4552f08db7b118f63e6d33d9c0925ccd05bb0b4b6b92e5f419400ad313837e0c38afa9d74a7e8dd9a39bd8919ad4069563ff35901f8f1d18e877c035
7
- data.tar.gz: fc33580f828311ffc4f73b329f2e22d93d387dc7d3b7146aa15d0045d048289860d4639469479fad057f6c3a1ca49124db474311540984a8f7ddef7ed00a172e
6
+ metadata.gz: 188f2845bf2f64102829f1840eaf56ded7ba7f2f2f931e07aa951d3b29e2e82b7570cd1564a40e8afb54ecd8f040874a9e1e589a5284025836dd81adf73549d3
7
+ data.tar.gz: c12fa48855d076acd7516fa02d97a5da56467ce8ce78e7dc41e305681be1e1251fdb82ce638d6921726d5d6455e2849a75806730f314b93efe2146a70eb89d7c
data/README.md CHANGED
@@ -58,21 +58,22 @@ Note: The color 0 (transparent) is auto-magically added / defined.
58
58
  And let's mint a mooncat image:
59
59
 
60
60
  ``` ruby
61
- img = Pixelart::Image.new( pixels, colors: colors )
61
+ img = Pixelart::Image.parse( pixels, colors: colors )
62
62
  img.save( './i/mooncat_white.png' )
63
63
  ```
64
64
 
65
65
  And let's try a 3x zoom factor:
66
66
 
67
67
  ``` ruby
68
- img = Pixelart::Image.new( pixels, colors: colors, zoom: 3 )
69
- img.save( './i/mooncat_white-3x.png' )
68
+ img3x = img.zoom( 3 )
69
+ img3x.save( './i/mooncat_white-3x.png' )
70
70
  ```
71
71
 
72
72
  Voila!
73
73
 
74
- ![](i/mooncat_white.png)
75
- ![](i/mooncat_white-3x.png)
74
+ ![](https://github.com/cryptocopycats/mooncats/raw/master/pixelarti/mooncat_white.png)
75
+ ![](https://github.com/cryptocopycats/mooncats/raw/master/pixelarti/mooncat_white-3x.png)
76
+
76
77
 
77
78
 
78
79
  Let's change the colors to use the genesis black color scheme:
@@ -90,17 +91,17 @@ colors = [
90
91
  And let's start minting:
91
92
 
92
93
  ``` ruby
93
- img = Pixelart::Image.new( pixels, colors: colors )
94
+ img = Pixelart::Image.parse( pixels, colors: colors )
94
95
  img.save( './i/mooncat_black.png' )
95
96
 
96
- img = Pixelart::Image.new( pixels, colors: colors, zoom: 3 )
97
- img.save( './i/mooncat_black-3x.png' )
97
+ img3x = img.zoom( 3 )
98
+ img3x.save( './i/mooncat_black-3x.png' )
98
99
  ```
99
100
 
100
101
  Voila! Black is the new White!
101
102
 
102
- ![](i/mooncat_black.png)
103
- ![](i/mooncat_black-3x.png)
103
+ ![](https://github.com/cryptocopycats/mooncats/raw/master/pixelarti/mooncat_black.png)
104
+ ![](https://github.com/cryptocopycats/mooncats/raw/master/pixelarti/mooncat_black-3x.png)
104
105
 
105
106
 
106
107
 
@@ -213,21 +214,21 @@ colors = {
213
214
  And let's mint an imperial master image:
214
215
 
215
216
  ``` ruby
216
- img = Pixelart::Image.new( pixels, colors: colors )
217
+ img = Pixelart::Image.parse( pixels, colors: colors )
217
218
  img.save( './i/vader.png' )
218
219
  ```
219
220
 
220
221
  And let's try a 5x zoom factor:
221
222
 
222
223
  ``` ruby
223
- img = Pixelart::Image.new( pixels, colors: colors, zoom: 5 )
224
- img.save( './i/vader5x.png' )
224
+ img5x = img.zoom( 5 )
225
+ img5x.save( './i/vader5x.png' )
225
226
  ```
226
227
 
227
228
  Voila!
228
229
 
229
- ![](i/vader.png)
230
- ![](i/vader5x.png)
230
+ ![](https://github.com/cryptocopycats/mooncats/raw/master/pixelarti/vader.png)
231
+ ![](https://github.com/cryptocopycats/mooncats/raw/master/pixelarti/vader5x.png)
231
232
 
232
233
 
233
234
 
data/lib/pixelart.rb CHANGED
@@ -3,6 +3,10 @@ require 'chunky_png'
3
3
 
4
4
  ## stdlib
5
5
  require 'pp'
6
+ require 'time'
7
+ require 'date'
8
+ require 'fileutils'
9
+
6
10
 
7
11
  ## our own code
8
12
  require 'pixelart/version' # note: let version always go first
@@ -12,68 +16,125 @@ require 'pixelart/version' # note: let version always go first
12
16
  module Pixelart
13
17
  class Image
14
18
 
15
- def initialize( pixels, colors:, zoom: 1 )
16
- @colors = parse_colors( colors )
17
- @pixels = parse_pixels( pixels )
18
19
 
19
- max_width = @pixels.reduce(1) {|max_width,row| row.size > max_width ? row.size : max_width }
20
- max_height = @pixels.size
21
20
 
22
- @img = ChunkyPNG::Image.new( max_width*zoom,
23
- max_height*zoom,
24
- ChunkyPNG::Color::TRANSPARENT )
21
+ def self.read( path ) ## convenience helper
22
+ img_inner = ChunkyPNG::Image.from_file( path )
23
+ img = new( img_inner.width, img_inner.height, img_inner )
24
+ img
25
+ end
26
+
27
+
28
+ def self.parse( pixels, colors: )
29
+ colors = parse_colors( colors )
30
+ pixels = parse_pixels( pixels )
31
+
32
+ width = pixels.reduce(1) {|width,row| row.size > width ? row.size : width }
33
+ height = pixels.size
25
34
 
26
- @pixels.each_with_index do |row,y|
35
+ img = new( width, height )
36
+
37
+ pixels.each_with_index do |row,y|
27
38
  row.each_with_index do |color,x|
28
- pixel = @colors[color]
29
- zoom.times do |n|
30
- zoom.times do |m|
31
- @img[n+zoom*x,m+zoom*y] = pixel
32
- end
33
- end
39
+ pixel = colors[color]
40
+ img[x,y] = pixel
34
41
  end # each row
35
42
  end # each data
43
+
44
+ img
36
45
  end
37
46
 
38
47
 
39
- def parse_pixels( pixels )
40
- data = []
41
- pixels.each_line do |line|
42
- line = line.strip
43
- if line.empty?
44
- puts "!! WARN: skipping empty line in pixel art source"
45
- next
46
- end
47
48
 
48
- ## note: allow multiple spaces or tabs to separate pixel codes
49
- ## e.g. o o o o o o o o o o o o dg lg w w lg w lg lg dg dg w w lg dg o o o o o o o o o o o
50
- ## or
51
- data << line.split( /[ \t]+/)
49
+ def initialize( width, height, initial=ChunkyPNG::Color::TRANSPARENT )
50
+
51
+ if initial.is_a?( ChunkyPNG::Image )
52
+ @img = initial
53
+ else
54
+ ## todo/check - initial - use parse_color here too e.g. allow "#fff" too etc.
55
+ @img = ChunkyPNG::Image.new( width, height, initial )
52
56
  end
53
- data
54
57
  end
55
58
 
56
59
 
60
+
61
+ def zoom( zoom=2 )
62
+ ## create a new zoom factor x image (2x, 3x, etc.)
63
+
64
+ img = Image.new( @img.width*zoom,
65
+ @img.height*zoom )
66
+
67
+ @img.height.times do |y|
68
+ @img.width.times do |x|
69
+ pixel = @img[x,y]
70
+ zoom.times do |n|
71
+ zoom.times do |m|
72
+ img[n+zoom*x,m+zoom*y] = pixel
73
+ end
74
+ end
75
+ end # each x
76
+ end # each y
77
+
78
+ img
79
+ end
80
+ alias_method :scale, :zoom
81
+
82
+
83
+
84
+
57
85
  #####
58
86
  # (image) delegates
59
87
  ## todo/check: add some more??
60
88
  def save( path, constraints = {} )
89
+ # step 1: make sure outdir exits
90
+ outdir = File.dirname( path )
91
+ FileUtils.mkdir_p( outdir ) unless Dir.exist?( outdir )
92
+
93
+ # step 2: save
61
94
  @img.save( path, constraints )
62
95
  end
96
+ alias_method :write, :save
97
+
98
+
99
+ def compose!( other, x=0, y=0 )
100
+ @img.compose!( other.image, x, y ) ## note: "unwrap" inner image ref
101
+ end
102
+ alias_method :paste!, :compose!
103
+
63
104
 
64
105
  def width() @img.width; end
65
106
  def height() @img.height; end
66
107
 
108
+ def []( x, y ) @img[x,y]; end
109
+ def []=( x, y, value ) @img[x,y]=value; end
110
+
67
111
  ## return image ref - use a different name - why? why not?
68
112
  def image() @img; end
69
113
 
70
- def colors() @colors; end ## todo/check - return color map (hash table) or just color values (array) - why? why not?
71
114
 
72
115
 
73
116
 
74
117
  ######
75
118
  # helpers
76
- def parse_colors( colors )
119
+ def self.parse_pixels( pixels )
120
+ data = []
121
+ pixels.each_line do |line|
122
+ line = line.strip
123
+ if line.empty?
124
+ puts "!! WARN: skipping empty line in pixel art source"
125
+ next
126
+ end
127
+
128
+ ## note: allow multiple spaces or tabs to separate pixel codes
129
+ ## e.g. o o o o o o o o o o o o dg lg w w lg w lg lg dg dg w w lg dg o o o o o o o o o o o
130
+ ## or
131
+ data << line.split( /[ \t]+/)
132
+ end
133
+ data
134
+ end
135
+
136
+
137
+ def self.parse_colors( colors )
77
138
  if colors.is_a?( Array ) ## convenience shortcut
78
139
  ## note: always auto-add color 0 as pre-defined transparent - why? why not?
79
140
  h = { '0' => ChunkyPNG::Color::TRANSPARENT }
@@ -90,7 +151,7 @@ def parse_colors( colors )
90
151
  end
91
152
  end
92
153
 
93
- def parse_color( color )
154
+ def self.parse_color( color )
94
155
  if color.is_a?( Integer ) ## e.g. Assumess ChunkyPNG::Color.rgb() or such
95
156
  color ## pass through as is 1:1
96
157
  elsif color.is_a?(String)
@@ -3,7 +3,7 @@ module Pixelart
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- PATCH = 0
6
+ PATCH = 1
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.0
4
+ version: 0.1.1
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-08 00:00:00.000000000 Z
11
+ date: 2021-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chunky_png