pixelart 1.1.0 → 1.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: bb520f7fc94b0b7d62336829d92929bbeb27600f0042ab3f68d81b6e5b26d2b3
4
- data.tar.gz: ed4756f69b5f5d5ca696a1105aa348b5b86ab33653ff7c34d1b24f4dc75e244f
3
+ metadata.gz: ba25604fe4925484c00f133e26e2d3d67aec4b23af8daa2cc3306235a2c704a6
4
+ data.tar.gz: 570abba81b211dc9aa1df3939b15e9e6845676884c01197c25d9cb23d3b4ba0f
5
5
  SHA512:
6
- metadata.gz: caa3d9eeb12a4a59e4f6ec3191e432ae11d7cf44465e0b6722c69bda985af6caeea3a4390f773d4d535a38096f483c3d9467b03f63ed6547a168f79361db1991
7
- data.tar.gz: d7f6d039eddc337b62ca22b338712a26423781b2a57703429469d376c02af0fef6aadd9182f4fb2cb755dc83f27c6bcbb97519ee528c01169ba8070534c7c377
6
+ metadata.gz: 1af47c73f13001e103d1ad0a4965d8125763935c2b962d50769f48cba1728ea351645bfb381721425c776b20ccd484acde470a41044b055552bb929e18a8e98d
7
+ data.tar.gz: e60473974839ade310e105c148f98b7a1e5232f6e0491df1108cb0f50028d8ff1b02af8e9c2ba61ede00c6396b4d5bf8833dd3c757692190f101ecdc6e6072f8
data/Manifest.txt CHANGED
@@ -15,4 +15,5 @@ lib/pixelart/palette.rb
15
15
  lib/pixelart/pixelator.rb
16
16
  lib/pixelart/sketch.rb
17
17
  lib/pixelart/spots.rb
18
+ lib/pixelart/vector.rb
18
19
  lib/pixelart/version.rb
data/lib/pixelart/base.rb CHANGED
@@ -28,6 +28,7 @@ require 'pixelart/palette'
28
28
  require 'pixelart/image'
29
29
  require 'pixelart/composite'
30
30
 
31
+
31
32
  require 'pixelart/pixelator'
32
33
 
33
34
  require 'pixelart/misc' ## misc helpers
@@ -47,6 +48,9 @@ module Pixelart
47
48
  MAGICK_OUTPUT = './tmp/magick_output.png'
48
49
  end
49
50
 
51
+ require 'pixelart/vector' ## vector graphics helpers
52
+
53
+
50
54
  require 'pixelart/spots'
51
55
  require 'pixelart/blur'
52
56
 
@@ -101,6 +101,14 @@ def mirror
101
101
  end
102
102
  alias_method :flip_vertically, :mirror
103
103
 
104
+ def flip
105
+ img = @img.flip
106
+ Image.new( img.width, img.height, img )
107
+ end
108
+ alias_method :flip_horizontally, :flip
109
+
110
+
111
+
104
112
 
105
113
  ## add replace_colors alias too? - why? why not?
106
114
  def change_colors( color_map )
@@ -3,41 +3,67 @@ module Pixelart
3
3
  class Image
4
4
 
5
5
 
6
- def spots( spot=10,
7
- spacing: 0,
8
- center: nil,
9
- radius: nil,
10
- background: nil,
11
- lightness: nil,
12
- odd: false )
6
+ def spots_hidef( spot=10,
7
+ spacing: 0,
8
+ center: nil,
9
+ radius: nil,
10
+ background: nil,
11
+ lightness: nil,
12
+ odd: false )
13
+
14
+ width = @img.width*spot+(@img.width-1)*spacing
15
+ height = @img.height*spot+(@img.height-1)*spacing
13
16
 
14
- width = @img.width
15
- height = @img.height
16
17
  ## puts " #{width}x#{height}"
17
18
 
19
+ ## settings in a hash for "pretty printing" in comments
20
+ settings = { spot: spot
21
+ }
22
+
23
+ settings[ :spacing ] = spacing if spacing
24
+ settings[ :center ] = center if center
25
+ settings[ :radius ] = radius if radius
26
+ settings[ :background ] = background if background
27
+ settings[ :lightness ] = lightness if lightness
28
+ settings[ :odd ] = odd if odd
29
+
30
+
31
+ v = Vector.new( width, height, header: <<TXT )
32
+ generated by pixelart/v#{VERSION} on #{Time.now.utc}
33
+
34
+ spots_hidef with settings:
35
+ #{settings.to_json}
36
+ TXT
37
+
38
+
18
39
  min_center, max_center = center ? center : [0,0]
19
40
  min_radius, max_radius = radius ? radius : [0,0]
20
41
 
21
- background_color = background ? Color.parse( background ) : 0
42
+ ## note: allow passing in array of colors (will get randomally picked)
43
+ background_colors = if background
44
+ ## check for array; otherwise assume single color passed in
45
+ background_ary = background.is_a?( Array) ? background : [background]
46
+ background_ary.map { |background| Color.parse( background ) }
47
+ else
48
+ [0] # transparent (default - no background)
49
+ end
22
50
 
23
- min_lightness, max_lightness = lightness ? lightness : [0.0,0.0]
24
51
 
25
- ## note: magick command line might get way too long, thus,
26
- ## save commands to a script
27
- script = String.new('')
28
- script << "#{MAGICK_INPUT} \\\n"
52
+ min_lightness, max_lightness = lightness ? lightness : [0.0,0.0]
29
53
 
30
54
 
31
- width.times do |x|
32
- height.times do |y|
55
+ @img.width.times do |x|
56
+ @img.height.times do |y|
33
57
  color = @img[ x, y ]
34
58
 
35
- if color == 0 ## transparent
36
- if background ## change transparent to background
37
- color = background_color
38
- else
39
- next ## skip transparent
40
- end
59
+ if color == 0 ## transparent
60
+ next if background.nil?
61
+
62
+ color = if background_colors.size == 1
63
+ background_colors[0]
64
+ else ## pick random background color
65
+ background_colors[ rand( background_colors.size ) ]
66
+ end
41
67
  end
42
68
 
43
69
 
@@ -65,7 +91,6 @@ def spots( spot=10,
65
91
  # e.g. 0 becomes #00000000
66
92
  # and so on
67
93
  color_hex = Color.to_hex( color, include_alpha: true )
68
- script << "-fill '#{color_hex}' "
69
94
 
70
95
  cx_offset,
71
96
  cy_offset = if center ## randomize (offset off center +/-)
@@ -79,48 +104,40 @@ def spots( spot=10,
79
104
  cx = x*spot + x*spacing + cx_offset
80
105
  cy = y*spot + y*spacing + cx_offset
81
106
 
82
- px_offset = if radius ## randomize (radius +/-)
107
+ r = if radius ## randomize (radius +/-)
83
108
  min_radius + rand( max_radius-min_radius )
84
109
  else
85
110
  spot/2
86
111
  end
87
- px = cx + px_offset
88
- py = cy
89
112
 
113
+ cx += spot/2 if odd && (y % 2 == 1) ## add odd offset
90
114
 
91
- if odd && (y % 2 == 1) ## add odd offset
92
- cx += spot/2
93
- px += spot/2
94
- end
95
115
 
96
- ## circle
97
- ## give the center and any point on the perimeter (boundary)
98
- script << "-draw 'circle #{cx},#{cy},#{px},#{py}' \\\n"
116
+ v.circle( cx: cx, cy: cy, r: r, fill: color_hex)
99
117
  end
100
118
  end
119
+ v
120
+ end ## method spots_hidef
121
+ alias_method :spots_hd, :spots_hidef
101
122
 
102
- script << "-write #{MAGICK_OUTPUT}\n"
103
-
104
-
105
- ## use an empty image (canvas) with transparent background
106
- ## as magick input image
107
- canvas = Image.new( width*spot+(width-1)*spacing,
108
- height*spot+(height-1)*spacing )
109
- canvas.save( MAGICK_INPUT )
110
-
111
- ## note: save magick input first (see above) before save script
112
- ## will (auto-)create missing directories in path (if missing)
113
-
114
- File.open( MAGICK_SCRIPT, 'w:utf-8' ) do |f|
115
- f.write( script )
116
- end
117
123
 
124
+ def spots( spot=10,
125
+ spacing: 0,
126
+ center: nil,
127
+ radius: nil,
128
+ background: nil,
129
+ lightness: nil,
130
+ odd: false )
118
131
 
119
- MiniMagick::Tool::Magick.new do |magick|
120
- magick.script( MAGICK_SCRIPT )
121
- end
132
+ v = spots_hidef( spot,
133
+ spacing: spacing,
134
+ center: center,
135
+ radius: radius,
136
+ background: background,
137
+ lightness: lightness,
138
+ odd: odd )
122
139
 
123
- Image.read( MAGICK_OUTPUT )
140
+ v.to_image
124
141
  end
125
142
 
126
143
 
@@ -0,0 +1,108 @@
1
+ module Pixelart
2
+
3
+
4
+ class Vector # holds a vector graphics image (in source)
5
+
6
+ class Shape; end
7
+ class Circle < Shape
8
+ def initialize( cx, cy, r, fill: )
9
+ @cx = cx
10
+ @cy = cy
11
+ @r = r
12
+ @fill = fill
13
+ end
14
+
15
+ def to_svg
16
+ %Q{<circle cx="#{@cx}" cy="#{@cy}" r="#{@r}" fill="#{@fill}" />}
17
+ end
18
+
19
+ def to_magick
20
+ ## circle
21
+ ## give the center and any point on the perimeter (boundary)
22
+ px = @cx+@r
23
+ py = @cy
24
+ "-fill '#{@fill}' -draw 'circle #{@cx},#{@cy},#{px},#{py}'"
25
+ end
26
+ end # class Circle
27
+
28
+
29
+
30
+ def initialize( width, height, header: nil )
31
+ @width = width
32
+ @height = height
33
+
34
+ @header = header
35
+ @shapes = []
36
+ end
37
+
38
+ def circle( cx:, cy:, r:, fill: )
39
+ @shapes << Circle.new( cx, cy, r, fill: fill )
40
+ end
41
+
42
+
43
+
44
+ def to_image
45
+ ## use an empty image (canvas) with transparent background
46
+ ## as magick input image
47
+ canvas = Image.new( @width, @height )
48
+ canvas.save( MAGICK_INPUT )
49
+
50
+ ## note: magick command line might get way too long, thus,
51
+ ## save commands to a script
52
+ ## note: save magick input first (see above) before save script
53
+ ## will (auto-)create missing directories in path (if missing)
54
+
55
+ File.open( MAGICK_SCRIPT, 'w:utf-8' ) do |f|
56
+ f.write( "#{MAGICK_INPUT} \\\n" )
57
+ @shapes.each do |shape|
58
+ f.write( "#{shape.to_magick} \\\n" )
59
+ end
60
+ f.write( "-write #{MAGICK_OUTPUT}\n" )
61
+ end
62
+
63
+ MiniMagick::Tool::Magick.new do |magick|
64
+ magick.script( MAGICK_SCRIPT )
65
+ end
66
+
67
+ Image.read( MAGICK_OUTPUT )
68
+ end
69
+
70
+
71
+ def to_svg
72
+ buf = String.new('')
73
+
74
+ if @header
75
+ buf << "<!--\n"
76
+ ## auto-indent lines by five (5) spaces for now
77
+ @header.each_line do |line|
78
+ buf << " #{line}"
79
+ end
80
+ buf << "\n-->\n\n"
81
+ end
82
+
83
+ buf << %Q{<svg version="1.1" width="#{@width}" height="#{@height}" xmlns="http://www.w3.org/2000/svg">\n}
84
+ @shapes.each do |shape|
85
+ buf << " #{shape.to_svg}\n"
86
+ end
87
+ buf << "</svg>"
88
+ buf
89
+ end
90
+
91
+
92
+ def save( path, format: nil )
93
+ if format && format.downcase == 'png' ## support png with image magick
94
+ img = to_image
95
+ img.save( path )
96
+ else
97
+ # make sure outdir exits
98
+ outdir = File.dirname( path )
99
+ FileUtils.mkdir_p( outdir ) unless Dir.exist?( outdir )
100
+ File.open( path, 'w:utf-8' ) do |f|
101
+ f.write( to_svg )
102
+ end
103
+ end
104
+ end
105
+ alias_method :write, :save
106
+
107
+ end # class Vector
108
+ end # module Pixelart
@@ -3,7 +3,7 @@ module Pixelart
3
3
 
4
4
  MAJOR = 1
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: 1.1.0
4
+ version: 1.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-09-24 00:00:00.000000000 Z
11
+ date: 2021-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chunky_png
@@ -99,6 +99,7 @@ files:
99
99
  - lib/pixelart/pixelator.rb
100
100
  - lib/pixelart/sketch.rb
101
101
  - lib/pixelart/spots.rb
102
+ - lib/pixelart/vector.rb
102
103
  - lib/pixelart/version.rb
103
104
  homepage: https://github.com/rubycoco/pixel
104
105
  licenses: