grobber 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ff2b9a25b479d799168ccaaa55d1193aa740ad6
4
+ data.tar.gz: 07c5730e59c28186e1af920f7f12dc4ba869397d
5
+ SHA512:
6
+ metadata.gz: 5e86d8772c61d8f4679b011acb81e7169477320b6d563b1ae42fc6745647008a6c45e12c8c9e59b3818be798e94f190a25e5eb15fc28e4de78c96b20ddb741f7
7
+ data.tar.gz: fefe28b6d2db57e1b0fa2481724ccbc12374fbfecd56fea98d8056f10e22ef66f6cbfa7917f7634f6a9ebc17dda152c78bf30fc646f6b71344d579fd342ca69e
@@ -0,0 +1,8 @@
1
+ module Grobber
2
+
3
+ require 'grobber/square_matrix'
4
+ require 'grobber/image'
5
+ require 'digest'
6
+ require 'RMagick'
7
+
8
+ end
@@ -0,0 +1,85 @@
1
+ module Grobber
2
+
3
+ class Image
4
+
5
+ attr_reader :base_color, :second_color, :signature
6
+
7
+ def initialize string_base
8
+ @signature = Digest::SHA1.hexdigest string_base
9
+ end
10
+
11
+ def base_color
12
+ signature[9..14]
13
+ end
14
+
15
+ def second_color
16
+ signature[15..20]
17
+ end
18
+
19
+ def to_rmagick size_px = 480
20
+ new_base_image(size_px).tap do |magick|
21
+ block_width = size_px / 6
22
+ binary_matrix.each_with_coords do |b, x, y|
23
+ next if b.zero?
24
+ add_block magick, color_string(second_color), x, y, block_width
25
+ end
26
+ end
27
+ end
28
+
29
+ def write path, size = 480
30
+ magick = to_rmagick size
31
+ magick.write(path) { self.quality = 95 }
32
+ magick.destroy!
33
+ path
34
+ end
35
+
36
+ def binary_matrix
37
+ @binary_matrix ||= SquareMatrix.new(binary_array).flip_flop
38
+ end
39
+
40
+ def color_matrix
41
+ @color_matrix ||= generate_hexa_array
42
+ end
43
+
44
+ def generate_hexa_array
45
+ binary_matrix.array.map do |b|
46
+ b.zero? ? base_color : second_color
47
+ end
48
+ end
49
+
50
+ def binary_array
51
+ signature[0..8].split(//).map do |c|
52
+ 2 * c.hex / 16
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def color_string hex_value
59
+ "##{ hex_value }"
60
+ end
61
+
62
+ def add_block image, color, x, y, square_px
63
+ origin_point = [x, y].map{ |c| c * square_px }
64
+ end_point = [x, y].map{ |c| (c + 1) * square_px }
65
+ draw_rectangle image, color, origin_point, end_point
66
+ end
67
+
68
+ def draw_rectangle image, color, origin_point, end_point
69
+ p = Magick::Draw.new
70
+ p.fill = color
71
+ p.stroke_width(0)
72
+ p.rectangle(*origin_point, *end_point)
73
+ p.draw(image)
74
+ end
75
+
76
+ def new_base_image size_px
77
+ _base_color = color_string(base_color)
78
+ Magick::Image.new(size_px,size_px) do
79
+ self.background_color = _base_color
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ end
@@ -0,0 +1,97 @@
1
+ class SquareMatrix
2
+
3
+ attr_reader :array
4
+
5
+ def initialize _array
6
+ check_square(_array)
7
+ @array = _array
8
+ end
9
+
10
+ def each_with_coords
11
+ array.each_with_index do |e, i|
12
+ yield e, *coords_for(i)
13
+ end
14
+ end
15
+
16
+ def coords_for index
17
+ [x_coord(index), y_coord(index)]
18
+ end
19
+
20
+ def array_length
21
+ array.length
22
+ end
23
+
24
+ def length
25
+ length_for array
26
+ end
27
+
28
+ def x_coord index
29
+ index % length
30
+ end
31
+
32
+ def y_coord index
33
+ (index / length)
34
+ end
35
+
36
+ def index_for x, y
37
+ length * y + x
38
+ end
39
+
40
+ def flop_array
41
+ array_length.times.map do |i|
42
+ array[index_for(length - 1 - x_coord(i), y_coord(i))]
43
+ end
44
+ end
45
+
46
+ def flop
47
+ self.class.new flop_array
48
+ end
49
+
50
+ def flip_flop
51
+ self.class.new flip_flop_array
52
+ end
53
+
54
+ def flip_flop_array
55
+ right_add_array(flop) + flip.right_add_array(flip.flop)
56
+ end
57
+
58
+ def flip_array
59
+ array_length.times.map do |i|
60
+ array[index_for x_coord(i), length - 1 - y_coord(i)]
61
+ end
62
+ end
63
+
64
+ def flip
65
+ self.class.new flip_array
66
+ end
67
+
68
+ def right_add_array square_matrix
69
+ length.times.flat_map do |i|
70
+ line(i) + square_matrix.line(i)
71
+ end
72
+ end
73
+
74
+ def bottom_add square_matrix
75
+ end
76
+
77
+ def length_for array
78
+ (array.length ** 0.5).to_i
79
+ end
80
+
81
+ def check_square array
82
+ length = length_for array
83
+ raise "Non Square Matrix" unless array.length == length ** 2
84
+ end
85
+
86
+ def line i
87
+ s_index = length * i
88
+ array[s_index..s_index + length - 1]
89
+ end
90
+
91
+ def display
92
+ length.times do |i|
93
+ puts line(i).to_s
94
+ end
95
+ end
96
+
97
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grobber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Theo C
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rmagick
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ description: Generate a picture based on a string value
34
+ email: superkoinkoin@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/grobber.rb
40
+ - lib/grobber/image.rb
41
+ - lib/grobber/square_matrix.rb
42
+ homepage: http://rubygems.org/gems/grobber
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.2.2
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Grobber Generator
66
+ test_files: []