icodi 0.1.2 → 0.1.3

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: 9d272a6dc8d9e595006ce36f72421892a2381cb0d51285e2390143d257c9f119
4
- data.tar.gz: e7fc61dfee1364269ffe07d4100b14fa7b48ec100680ed5094b6e40b673ea6c3
3
+ metadata.gz: ed662658e812ed352bae13ab22ade162554042af8894e4ac6b979cb5f5b33d49
4
+ data.tar.gz: 68f723bc2f69ac46f051796ffcf69713c5e1c1d2d4d6cb45902afdcbffb7872b
5
5
  SHA512:
6
- metadata.gz: 86b7e257b95b0ead2d4ec44ee27edb3187cafa18ba519f99776831520502294b6a47c80732cc71abbf5f3e544a17307f449d66153391b8d806c7df03298a6b86
7
- data.tar.gz: 68fa6d9f3ed793c99f3cdce6437781026e2bd44c8192bae60e8925988ec8b496840083bbf4a5c2314b1a6fa33f4069e0d150413928e91054fe6d35f71d54bdd4
6
+ metadata.gz: 115aa2c08283428ea5883737b118b4bbd4f6dd29996964cdfcf6fd1e9c7abdc8212708b6bbaf446048e692d105cf6858e6aa2ff54b1d944a056620efefbf0d73
7
+ data.tar.gz: 211be6a5f5d6b96c8e2e2e22d9a5b9fbcebaa3594d0f624582a4a584dbb40a5aa352b574ed38245fcfb022a5641b9e44f5ec505490dbe7bad2b1a73ab1ad0150
data/README.md CHANGED
@@ -122,6 +122,7 @@ Parameter | Default | Type | Description
122
122
  `stroke` | `0.1` | Float | Width of the border around each pixel. Note that each pixel is a 10x10 box, so a stroke of 1 means it will take 10% of the box. Higher values generate more overlap between the pixels.
123
123
  `jitter` | `0` | Float | A value between 0 and 1 representing the chance for a pixel to be dislocated by half of its size in a random direction.
124
124
  `background`| `#fff` | String | A named SVG color string (`blue`, `yellow` etc.) or RGB color (for example `#dddddd`).
125
+ `id` | `icodi` | String | The ID to assign the SVG object. Normally this should not matter, but if you intend to embed this icon in an HTML, or in another SVG, this can be useful.
125
126
  `template` | `:default` | Symbol/String | SVG template to use. Can be `:default`, `:html` or a path to a file. Read more on [Victor SVG Templates].
126
127
 
127
128
  ---
@@ -1,81 +1,33 @@
1
1
  require 'victor'
2
2
  require 'digest/md5'
3
+ require 'icodi/option_handling'
4
+ require 'icodi/randomization'
3
5
 
4
6
  class Icodi < Victor::SVGBase
7
+ include IcodiCore::OptionHandling
8
+ include IcodiCore::Randomization
9
+
5
10
  attr_reader :text, :options
6
11
 
7
- def initialize(text = nil, options = {})
8
- text, options = nil, text if text.is_a? Hash
12
+ def initialize(text = nil, opts = {})
13
+ text, opts = nil, text if text.is_a? Hash
9
14
 
10
15
  @text = text
11
- @options = default_options.merge options
16
+ @options = default_options.merge opts
12
17
 
13
- super template: template, viewBox: "0 0 #{size} #{size}", clip_path: "url(#rect)"
18
+ super template: template, viewBox: "0 0 #{size} #{size}", id: id
14
19
 
15
20
  generate
16
21
  end
17
22
 
18
- def method_missing(method_name, *_args, &_block)
19
- respond_to?(method_name) ? options[method_name] : super
20
- end
21
-
22
- def respond_to?(method_name, include_private = false)
23
- options.has_key?(method_name) ? true : super
24
- end
25
-
26
23
  private
27
24
 
28
- def default_options
29
- {
30
- template: :default,
31
- pixels: 5,
32
- density: 0.5,
33
- stroke: 0.1,
34
- background: '#fff',
35
- color: random_color,
36
- mirror: :x,
37
- jitter: 0,
38
- }
39
- end
40
-
41
- def random_color
42
- "#%06x" % (random(:color).rand * 0xffffff)
43
- end
44
-
45
- def seed(string)
46
- Digest::MD5.hexdigest(string).to_i(16)
47
- end
48
-
49
- def random(set = nil)
50
- @random_sets ||= {}
51
- set ||= :default
52
- @random_sets[set] ||= (text ? Random.new(seed(text)) : Random.new)
53
- end
54
-
55
- def size
56
- @size ||= pixels * 10
57
- end
58
-
59
- def style
60
- @style ||= { stroke: color, stroke_width: stroke }
61
- end
62
-
63
- def mirror_x?
64
- [:x, :both].include? mirror
65
- end
66
-
67
- def mirror_y?
68
- [:y, :both].include? mirror
69
- end
70
-
71
- def mirror_both?
72
- mirror == :both
73
- end
74
-
75
25
  def generate
26
+ clip_path_id = "#{id}-#{random_id}"
27
+
76
28
  element :rect, x: 0, y: 0, width: size, height: size, fill: background
77
29
  element :defs do
78
- element :clipPath, id: :clipper do
30
+ element :clipPath, id: clip_path_id do
79
31
  element :rect, width: size, height: size
80
32
  end
81
33
  end
@@ -84,7 +36,7 @@ private
84
36
  x = mirror_x? ? half : pixels
85
37
  y = mirror_y? ? half : pixels
86
38
 
87
- element :g, clip_path: "url(#clipper)" do
39
+ element :g, clip_path: "url(##{clip_path_id})" do
88
40
  draw x, y
89
41
  end
90
42
  end
@@ -106,6 +58,20 @@ private
106
58
  draw_pixel mirror_value(x), mirror_value(y) if mirror? x: x, y: y
107
59
  end
108
60
 
61
+ def add_jitter(x, y)
62
+ return [x, y] unless jitter > 0 and random(:jitter).rand < jitter
63
+
64
+ x += [0, 0.5, -0.5][random(:jitter).rand(3)] unless mirror_x? and mid? x: x
65
+ y += [0, 0.5, -0.5][random(:jitter).rand(3)] unless mirror_y? and mid? y: y
66
+ [x, y]
67
+ end
68
+
69
+ def draw_pixel(x, y)
70
+ element :rect, x: x*10, y: y*10, width: 10, height: 10, fill: color, style: style
71
+ end
72
+
73
+ # Drawing Utilities
74
+
109
75
  def mirror?(x: nil, y: nil)
110
76
  if x and y
111
77
  mirror_both? and !mid?(x: x) and !mid?(y: y)
@@ -120,20 +86,11 @@ private
120
86
  x ? x == pixels/2 : y ? y == pixels/2 : nil
121
87
  end
122
88
 
123
- def add_jitter(x, y)
124
- return [x, y] unless jitter > 0 and random(:jitter).rand < jitter
125
-
126
- x += [0, 0.5, -0.5][random(:jitter).rand(3)] unless mirror_x? and mid? x: x
127
- y += [0, 0.5, -0.5][random(:jitter).rand(3)] unless mirror_y? and mid? y: y
128
- [x, y]
129
- end
130
-
131
- def draw_pixel(x, y)
132
- element :rect, x: x*10, y: y*10, width: 10, height: 10, fill: color, style: style
133
- end
134
-
135
89
  def mirror_value(value)
136
90
  pixels - 1 - value
137
91
  end
138
92
 
93
+ def random_id
94
+ random(:nonvisual).rand(9999999)
95
+ end
139
96
  end
@@ -0,0 +1,48 @@
1
+ module IcodiCore
2
+ module OptionHandling
3
+ def method_missing(method_name, *_args, &_block)
4
+ respond_to?(method_name) ? options[method_name] : super
5
+ end
6
+
7
+ def respond_to?(method_name, include_private = false)
8
+ options.has_key?(method_name) ? true : super
9
+ end
10
+
11
+ def default_options
12
+ {
13
+ template: :default,
14
+ pixels: 5,
15
+ density: 0.5,
16
+ stroke: 0.1,
17
+ background: '#fff',
18
+ color: random_color,
19
+ mirror: :x,
20
+ jitter: 0,
21
+ id: :icodi,
22
+ }
23
+ end
24
+
25
+ # Derivative Properties
26
+
27
+ def size
28
+ @size ||= pixels * 10
29
+ end
30
+
31
+ def style
32
+ @style ||= { stroke: color, stroke_width: stroke }
33
+ end
34
+
35
+ def mirror_x?
36
+ [:x, :both].include? mirror
37
+ end
38
+
39
+ def mirror_y?
40
+ [:y, :both].include? mirror
41
+ end
42
+
43
+ def mirror_both?
44
+ mirror == :both
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,21 @@
1
+ module IcodiCore
2
+ module Randomization
3
+ def random_color
4
+ "#%06x" % (random(:color).rand * 0xffffff)
5
+ end
6
+
7
+ def seed(string)
8
+ Digest::MD5.hexdigest(string).to_i(16)
9
+ end
10
+
11
+ def random(set = nil)
12
+ set ||= :default
13
+ random_sets[set] ||= (text ? Random.new(seed(text)) : Random.new)
14
+ end
15
+
16
+ def random_sets
17
+ @random_sets ||= {}
18
+ end
19
+
20
+ end
21
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: icodi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-18 00:00:00.000000000 Z
11
+ date: 2018-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: victor
@@ -32,6 +32,8 @@ extra_rdoc_files: []
32
32
  files:
33
33
  - README.md
34
34
  - lib/icodi.rb
35
+ - lib/icodi/option_handling.rb
36
+ - lib/icodi/randomization.rb
35
37
  homepage: https://github.com/dannyben/icodi
36
38
  licenses:
37
39
  - MIT