stitchify 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/stitchify.rb +279 -238
  3. metadata +15 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c1ae236a6ab9fa858d781b1a3dd11be1346f7b38
4
- data.tar.gz: 7030f2b85807ff84c8bbf7439f2eb8de6e25d58a
3
+ metadata.gz: 1c73c9993fb08707c070876e610733903a80fc6e
4
+ data.tar.gz: b09b9fcca355a156c0ba60425767faac7da06489
5
5
  SHA512:
6
- metadata.gz: 9f2c9384433e03f5269f76a5a03f765e19bf3ba47d39f2847e1c61e32822d11d42e62cf14fc014e3e47ade599c9cfb7a8792da393e883700baec71ab3274c2fc
7
- data.tar.gz: 987943c1456f54ac0c1753fdde31e4af09bd6558bed9ef940bce7aadef33596a0c8806a61f0ef503a1632a261cbd5dc94e7cdaf30a8dbd9eb2fcfcc66720ba81
6
+ metadata.gz: 8d810c0bec150c1525dddc6c140026adfa04ad2c6851d6fecf8356f8e11f2d61ad5fe2056b80aa28b0846f938e10fa2ba052a44c118f89153ba6fac9e94f12dd
7
+ data.tar.gz: 05056621ccac0f50e750ff4b473a8aed14bec07f7acef7ea41715751c4fa8260cbb622b24b09ecbb223b5f527753a610c74592c0cedc551be314eae812d3c81f
data/lib/stitchify.rb CHANGED
@@ -1,238 +1,279 @@
1
- class Stitchifier
2
- require 'nokogiri'
3
- require 'open-uri'
4
- require 'pry'
5
- require 'rasem'
6
- require 'asciiart'
7
-
8
- attr_accessor :px, :pos_x, :pos_y, :width, :height, :ascii_width
9
-
10
- def initialize(px = 10, ascii_width = 45)
11
- self.px = px
12
- self.pos_x = 0
13
- self.pos_y = 0
14
- self.width = 4 * px
15
- self.height = 4 * px
16
- self.ascii_width = ascii_width
17
- end
18
-
19
- def stitch(img, file = 'stitchify.svg')
20
- ascii = img_processor(img)
21
- arrs = paragraph_builder(ascii)
22
- arrs = arrs + grid
23
- rasem = arrs_to_rasem(arrs)
24
- write(rasem, file)
25
- clear_vars
26
- end
27
-
28
- def clear_vars
29
- pos_x = 0
30
- pos_y = 0
31
- width = 0
32
- height = 0
33
- end
34
-
35
- def img_processor(img)
36
- AsciiArt.new(img).to_ascii_art(width: ascii_width)
37
- end
38
-
39
- def arrs_to_rasem(arrs)
40
- Rasem::SVGImage.new(width: self.width, height: self.height) do
41
- for line_data in arrs
42
- line line_data[0], line_data[1], line_data[2], line_data[3]
43
- end
44
- end
45
- end
46
-
47
- def write(rasem, file)
48
- File.open(file, "w") do |f|
49
- rasem.write(f)
50
- end
51
- end
52
-
53
- def grid
54
- n = 10
55
- output = []
56
- (width / n).times do |i|
57
- output << [i * n, 0, i * n, height]
58
- end
59
- (height / n).times do |i|
60
- output << [0, i * n, width, i * n]
61
- end
62
- output
63
- end
64
-
65
- def paragraph_builder(str)
66
- self.height = 0
67
- arr = str.split("\n")
68
- output = []
69
- arr.each do |line|
70
- output = output + line_builder(line)
71
- self.pos_y = self.pos_y + (4 * px)
72
- self.pos_x = 0
73
- self.height = self.height + (4 * px)
74
- end
75
- output
76
- end
77
-
78
- def line_builder(line)
79
- self.width = 0
80
- line_output = []
81
- line.split('').each do |char|
82
- line_output = line_output + char_builder(char)
83
- self.pos_x = self.pos_x + (4 * px)
84
- self.width = self.width + (4 * px)
85
- end
86
- line_output
87
- end
88
-
89
- def char_builder(char)
90
- output = []
91
- case char
92
- when '.'
93
- output << pos_slope_one(1.5 * px, 3.5 * px, px)
94
- output << neg_slope_one(1.5 * px, 2.5 * px, px)
95
- when '~'
96
- output << pos_slope_one(px / 2, 2.5 * px, px)
97
- output << neg_slope_one(1.5 * px, 1.5 * px, px)
98
- output << pos_slope_one(2.5 * px, 2.5 * px, px)
99
- when ':'
100
- output << pos_slope_one(1.5 * px, 3.5 * px, px)
101
- output << neg_slope_one(1.5 * px, 2.5 * px, px)
102
- output << pos_slope_one(1.5 * px, 1.5 * px, px)
103
- output << neg_slope_one(1.5 * px, px / 2, px)
104
- when '+'
105
- output << vertical_line(2.5 * px, px / 2, 3 * px)
106
- output << horizontal_line(px / 2, 1.5 * px, 3 * px)
107
- when '='
108
- output << horizontal_line(px / 2, 1.5 * px, 3 * px)
109
- output << horizontal_line(px / 2, 2.5 * px, 3 * px)
110
- when 'o'
111
- output << pos_slope_one( px / 2, 1.5 * px, px)
112
- output << horizontal_line(1.5 * px, px / 2, px)
113
- output << neg_slope_one( 2.5 * px, px / 2, px)
114
- output << vertical_line( 3.5 * px, 1.5 * px, px)
115
- output << pos_slope_one( 2.5 * px, 3.5 * px, px)
116
- output << horizontal_line(1.5 * px, 3.5 * px, px)
117
- output << neg_slope_one( px / 2, 2.5 * px, px)
118
- output << vertical_line( px / 2, 1.5 * px, px)
119
- when '*'
120
- output << pos_slope_one(px / 2, 3.5 * px , 3 * px)
121
- output << neg_slope_one(px / 2, px / 2, 3 * px)
122
- output << vertical_line(2.5 * px, px / 2, 3 * px)
123
- when 'x'
124
- output << pos_slope_one(px / 2, 3.5 * px, 3 * px)
125
- output << neg_slope_one(px / 2, px / 2, 3 * px)
126
- when '^'
127
- output << pos_slope_one(1.5 * px, 1.5 * px, px)
128
- output << neg_slope_one(2.5 * px, px / 2, px)
129
- when '%'
130
- output << pos_slope_one(px / 2, 1.5 * px, px)
131
- output << neg_slope_one(px / 2, px / 2, px)
132
- output << pos_slope_one(px / 2, 3.5 * px, 3 * px)
133
- output << pos_slope_one(2.5 * px, 3.5 * px, px)
134
- output << neg_slope_one(2.5 * px, 2.5 * px, px)
135
- when '#'
136
- output << vertical_line(1.5 * px, px / 2, 3 * px)
137
- output << vertical_line(2.5 * px, px / 2, 3 * px)
138
- output << horizontal_line(px / 2, 1.5 * px, 3 * px)
139
- output << horizontal_line(px / 2, 2.5 * px, 3 * px)
140
- when '@'
141
- output << pos_slope_one(2.5 * px, 2.5 * px, px)
142
- output << neg_slope_one(2.5 * px, 1.5 * px, px)
143
- output << vertical_line(3.5 * px, px / 2, 2 * px)
144
- output << horizontal_line(1.5 * px, px / 2, 2 * px)
145
- output << pos_slope_two(px / 2, 2.5 * px, px)
146
- output << neg_slope_one(px / 2, 2.5 * px, px)
147
- output << horizontal_line(1.5 * px, 3.5 * px, 2 * px)
148
- when '$'
149
- output << pos_slope_one(1.5 * px, 1.5 * px, px)
150
- output << neg_slope_one(2.5 * px, px / 2, px)
151
- output << neg_slope_half(1.5 * px, 1.5 * px, px)
152
- output << pos_slope_one(2.5 * px, 3.5 * px, px)
153
- output << neg_slope_one(1.5 * px, 2.5 * px, px)
154
- output << vertical_line(2.5 * px, px / 2, 3 * px)
155
- when 'M'
156
- output << vertical_line(1.5 * px, px / 2, 3 * px)
157
- output << neg_slope_two(1.5 * px, px / 2, px)
158
- output << pos_slope_two(2.5 * px, 2.5 * px, px)
159
- output << vertical_line(3.5 * px, px / 2, 3 * px)
160
- when 'W'
161
- output << vertical_line(1.5 * px, px / 2, 3 * px)
162
- output << pos_slope_two(1.5 * px, 3.5 * px, px)
163
- output << neg_slope_two(2.5 * px, 1.5 * px, px)
164
- output << vertical_line(3.5 * px, px / 2, 3 * px)
165
- when '|'
166
- output << vertical_line(2.5 * px, px / 2, 3 * px)
167
- when '-'
168
- output << horizontal_line(px / 2, 2.5 * px, 3 * px)
169
- end
170
- output
171
- end
172
-
173
- def pos_slope_one(startX, startY, length)
174
- [
175
- startX + pos_x,
176
- startY + pos_y,
177
- startX + pos_x + length,
178
- startY + pos_y - length
179
- ]
180
- end
181
-
182
- def neg_slope_one(startX, startY, length)
183
- [
184
- startX + pos_x,
185
- startY + pos_y,
186
- startX + pos_x + length,
187
- startY + pos_y + length
188
- ]
189
- end
190
-
191
- def vertical_line(startX, startY, length)
192
- [
193
- startX + pos_x,
194
- startY + pos_y,
195
- startX + pos_x,
196
- startY + pos_y + length
197
- ]
198
- end
199
-
200
- def horizontal_line(startX, startY, length)
201
- [
202
- startX + pos_x,
203
- startY + pos_y,
204
- startX + pos_x + length,
205
- startY + pos_y
206
- ]
207
- end
208
-
209
- def pos_slope_two(startX, startY, width)
210
- [
211
- startX + pos_x,
212
- startY + pos_y,
213
- startX + pos_x + width,
214
- startY + pos_y - (2 * width)
215
- ]
216
- end
217
-
218
- def neg_slope_half(startX, startY, height)
219
- [
220
- startX + pos_x,
221
- startY + pos_y,
222
- startX + pos_x + (2 * height),
223
- startY + pos_y + height
224
- ]
225
- end
226
-
227
- def neg_slope_two(startX, startY, width)
228
- [
229
- startX + pos_x,
230
- startY + pos_y,
231
- startX + pos_x + width,
232
- startY + pos_y + (2 * width)
233
- ]
234
- end
235
- end
236
-
237
- s = Stitchifier.new
238
- s.stitch('../../../Downloads/MYFACE.jpg')
1
+ class Stitchifier
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+ require 'pry'
5
+ require 'rasem'
6
+ require 'asciiart'
7
+ require 'hex256'
8
+
9
+ OPEN_BRACKET = "\e[38;5;"
10
+ CLOSE_BRACKET = "\e[0m"
11
+
12
+ attr_accessor :px, :pos_x, :pos_y, :width, :height, :ascii_width
13
+
14
+ def initialize(px = 10, ascii_width = nil)
15
+ self.px = px
16
+ self.pos_x = 0
17
+ self.pos_y = 0
18
+ self.width = 4 * px
19
+ self.height = 4 * px
20
+ self.ascii_width = ascii_width
21
+ end
22
+
23
+ def stitch(img, file = 'stitchify.svg')
24
+ ascii = img_processor(img)
25
+ arrs = paragraph_builder(ascii)
26
+ rasem = arrs_to_rasem(arrs, grid)
27
+ write(rasem, file)
28
+ clear_vars
29
+ end
30
+
31
+ # ".~:+=o*x^%#@$MW\n .~:+=o*x^%#@$M\nW.~:+=o*x^% #@$MW"
32
+
33
+ def stitch_string(str, file = 'stitchify.svg')
34
+ arrs = paragraph_builder(str)
35
+ rasem = arrs_to_rasem(arrs, grid)
36
+ write(rasem, file)
37
+ clear_vars
38
+ end
39
+
40
+ def clear_vars
41
+ self.pos_x = 0
42
+ self.pos_y = 0
43
+ self.width = 0
44
+ self.height = 0
45
+ end
46
+
47
+ def ascii_conditions
48
+ ret = {}
49
+ ret[:color] = false
50
+ ret[:width] = self.ascii_width unless self.ascii_width.nil?
51
+ ret
52
+ end
53
+
54
+ def img_processor(img)
55
+ AsciiArt.new(img).to_ascii_art(ascii_conditions)
56
+ end
57
+
58
+ def arrs_to_rasem(arrs, grid)
59
+ Rasem::SVGImage.new(width: self.width, height: self.height) do
60
+ for line_data in arrs
61
+ line line_data[0], line_data[1], line_data[2], line_data[3], :stroke_width=>2, :fill=>line_data[4], :stroke=>line_data[4]
62
+ end
63
+ for line_data in grid
64
+ line line_data[0], line_data[1], line_data[2], line_data[3], :stroke_width=>line_data[5]
65
+ end
66
+ end
67
+ end
68
+
69
+ def write(rasem, file)
70
+ File.open(file, "w") do |f|
71
+ rasem.write(f)
72
+ end
73
+ end
74
+
75
+ def grid
76
+ n = 10
77
+ output = []
78
+ (width / n).times do |i|
79
+ x = 1
80
+ x = 2 if (i % 10 == 0)
81
+ output << [i * n, 0, i * n, height, 'black', x]
82
+ end
83
+ (height / n).times do |i|
84
+ x = 1
85
+ x = 2 if i % 10 == 0
86
+ output << [0, i * n, width, i * n, 'black', x]
87
+ end
88
+ output
89
+ end
90
+
91
+ def paragraph_builder(str)
92
+ self.height = 0
93
+ arr = str.split("\n")
94
+ output = []
95
+ arr.each do |line|
96
+ output = output + line_builder(line)
97
+ self.pos_y = self.pos_y + (3 * px)
98
+ self.pos_x = 0
99
+ self.height = self.height + (3 * px)
100
+ end
101
+ output
102
+ end
103
+
104
+ def line_builder(line)
105
+ self.width = 0
106
+ line_output = []
107
+
108
+ l = line.split(OPEN_BRACKET)
109
+
110
+ l.each do |segment|
111
+
112
+ char_data = segment.split("m")
113
+
114
+ # if the midpoint doesn't exist
115
+ if char_data.length == 1
116
+ char_data[0].chars.each do |char|
117
+ line_output = line_output + char_builder(char, '#000000')
118
+ self.pos_x = self.pos_x + (2 * px)
119
+ self.width = self.width + (2 * px)
120
+ end
121
+ else
122
+ char = char_data[1].delete(CLOSE_BRACKET)
123
+ line_output = line_output + char_builder(char, HexConverter.ansi_to_hex(char_data[0]))
124
+ self.pos_x = self.pos_x + (2 * px)
125
+ self.width = self.width + (2 * px)
126
+ end
127
+ end
128
+
129
+ line_output
130
+ end
131
+
132
+ def char_builder(char, hex_str)
133
+ output = []
134
+ case char
135
+ when '.'
136
+ output << pos_slope_one(1.5 * px, 2.5 * px, px, hex_str)
137
+ output << neg_slope_one(1.5 * px, 1.5 * px, px, hex_str)
138
+ when '~'
139
+ output << pos_slope_one((0 - px / 2), 2.5 * px, px, hex_str)
140
+ output << neg_slope_one(px / 2, 1.5 * px, px, hex_str)
141
+ output << pos_slope_one(1.5 * px, 2.5 * px, px, hex_str)
142
+ when ':'
143
+ output << pos_slope_one(1.5 * px, 2.5 * px, px, hex_str)
144
+ output << neg_slope_one(1.5 * px, 1.5 * px, px, hex_str)
145
+ output << pos_slope_one(1.5 * px, 1.5 * px, px, hex_str)
146
+ output << neg_slope_one(1.5 * px, px / 2, px, hex_str)
147
+ when '+'
148
+ output << vertical_line(1.5 * px, px / 2, 2 * px, hex_str)
149
+ output << horizontal_line(px / 2, 1.5 * px, 2 * px, hex_str)
150
+ when '='
151
+ output << horizontal_line(px / 2, 1.5 * px, 2 * px, hex_str)
152
+ output << horizontal_line(px / 2, 2.5 * px, 2 * px, hex_str)
153
+ when 'o'
154
+ output << pos_slope_one(px / 2, 1.5 * px, px, hex_str)
155
+ output << neg_slope_one(1.5 * px, px / 2, px, hex_str)
156
+ output << pos_slope_one(1.5 * px, 2.5 * px, px, hex_str)
157
+ output << neg_slope_one(px / 2, 1.5 * px, px, hex_str)
158
+ when '*'
159
+ output << pos_slope_one(px / 2, 2.5 * px , 2 * px, hex_str)
160
+ output << neg_slope_one(px / 2, px / 2, 2 * px, hex_str)
161
+ output << vertical_line(1.5 * px, px / 2, 2 * px, hex_str)
162
+ when 'x'
163
+ output << pos_slope_one(px / 2, 2.5 * px , 2 * px, hex_str)
164
+ output << neg_slope_one(px / 2, px / 2, 2 * px, hex_str)
165
+ when '^'
166
+ output << pos_slope_one(px / 2, 1.5 * px, px, hex_str)
167
+ output << neg_slope_one(1.5 * px, px / 2, px, hex_str)
168
+ when '%'
169
+ output << pos_slope_one(px / 2, 1.5 * px, px, hex_str)
170
+ output << neg_slope_one(px / 2, px / 2, px, hex_str)
171
+ output << pos_slope_one(px / 2, 2.5 * px, 2 * px, hex_str)
172
+ output << pos_slope_one(1.5 * px, 2.5 * px, px, hex_str)
173
+ output << neg_slope_one(1.5 * px, 1.5 * px, px, hex_str)
174
+ when '#'
175
+ output << pos_slope_two(px / 2, 2.5 * px, px, hex_str)
176
+ output << pos_slope_two(1.5 * px, 2.5 * px, px, hex_str)
177
+ output << horizontal_line(px / 2, 1.5 * px, 2 * px, hex_str)
178
+ output << horizontal_line(px / 2, 2.5 * px, 2 * px, hex_str)
179
+ when '@'
180
+ output << pos_slope_one(1.5 * px, 2.5 * px, px, hex_str)
181
+ output << neg_slope_one(1.5 * px, 1.5 * px, px, hex_str)
182
+ output << vertical_line(2.5 * px, px / 2, 2 * px, hex_str)
183
+ output << horizontal_line(1.5 * px, px / 2, px, hex_str)
184
+ output << pos_slope_one(px / 2, 1.5 * px, px, hex_str)
185
+ output << neg_slope_one(px / 2, 1.5 * px, px, hex_str)
186
+ output << horizontal_line(1.5 * px, 2.5 * px, px, hex_str)
187
+ when '$'
188
+ output << horizontal_line(px / 2, px / 2, 2 * px, hex_str)
189
+ output << neg_slope_one(px / 2, px / 2, 2 * px, hex_str)
190
+ output << horizontal_line(px / 2, 2.5 * px, 2 * px, hex_str)
191
+ output << vertical_line(1.5 * px, px / 2, 2 * px, hex_str)
192
+ when 'M'
193
+ output << vertical_line(px / 2, px / 2, 2 * px, hex_str)
194
+ output << neg_slope_one(px / 2, px / 2, px, hex_str)
195
+ output << pos_slope_one(1.5 * px, 1.5 * px, px, hex_str)
196
+ output << vertical_line(2.5 * px, px / 2, 2 * px, hex_str)
197
+ when 'W'
198
+ output << vertical_line(px / 2, px / 2, 2 * px, hex_str)
199
+ output << pos_slope_one(px / 2, 2.5 * px, px, hex_str)
200
+ output << neg_slope_one(1.5 * px, 1.5 * px, px, hex_str)
201
+ output << vertical_line(2.5 * px, px / 2, 2 * px, hex_str)
202
+ when '|'
203
+ output << vertical_line(2.5 * px, px / 2, 2 * px, hex_str)
204
+ when '-'
205
+ output << horizontal_line(px / 2, 2.5 * px, 2 * px, hex_str)
206
+ end
207
+ output
208
+ end
209
+
210
+ def pos_slope_one(startX, startY, length, hex_str)
211
+ [
212
+ startX + pos_x,
213
+ startY + pos_y,
214
+ startX + pos_x + length,
215
+ startY + pos_y - length,
216
+ hex_str
217
+ ]
218
+ end
219
+
220
+ def neg_slope_one(startX, startY, length, hex_str)
221
+ [
222
+ startX + pos_x,
223
+ startY + pos_y,
224
+ startX + pos_x + length,
225
+ startY + pos_y + length,
226
+ hex_str
227
+ ]
228
+ end
229
+
230
+ def vertical_line(startX, startY, length, hex_str)
231
+ [
232
+ startX + pos_x,
233
+ startY + pos_y,
234
+ startX + pos_x,
235
+ startY + pos_y + length,
236
+ hex_str
237
+ ]
238
+ end
239
+
240
+ def horizontal_line(startX, startY, length, hex_str)
241
+ [
242
+ startX + pos_x,
243
+ startY + pos_y,
244
+ startX + pos_x + length,
245
+ startY + pos_y,
246
+ hex_str
247
+ ]
248
+ end
249
+
250
+ def pos_slope_two(startX, startY, width, hex_str)
251
+ [
252
+ startX + pos_x,
253
+ startY + pos_y,
254
+ startX + pos_x + width,
255
+ startY + pos_y - (2 * width),
256
+ hex_str
257
+ ]
258
+ end
259
+
260
+ def neg_slope_half(startX, startY, height, hex_str)
261
+ [
262
+ startX + pos_x,
263
+ startY + pos_y,
264
+ startX + pos_x + (2 * height),
265
+ startY + pos_y + height,
266
+ hex_str
267
+ ]
268
+ end
269
+
270
+ def neg_slope_two(startX, startY, width, hex_str)
271
+ [
272
+ startX + pos_x,
273
+ startY + pos_y,
274
+ startX + pos_x + width,
275
+ startY + pos_y + (2 * width),
276
+ hex_str
277
+ ]
278
+ end
279
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stitchify
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
  - Ellen Wondra
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - '='
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.0.9
55
+ - !ruby/object:Gem::Dependency
56
+ name: hex256
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.0.1
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rake
57
71
  requirement: !ruby/object:Gem::Requirement