icodi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +24 -0
  3. data/lib/icodi.rb +110 -0
  4. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1b1bdd7935d65c413f8cf5045617e65fdc577ec29791d6b6d057f5136b09ae21
4
+ data.tar.gz: b2fe8269f46af45a14fe623cec635db2411100c14d27f971cb72da55d37ec356
5
+ SHA512:
6
+ metadata.gz: 1c10feaef8ce4d94eb903c7470fb305f6438f81ffca59f4c7fd690ee04c86bcf38af06360eebbde832282a5789cd561742309546e256c5aa8a8a38b43a54104e
7
+ data.tar.gz: 80a364f7250b254af25de1dfcc2bcf3a820fc5cb27d6c0608457d27bb45092cec01bc3721f9c37f9a7211d53352277411655fa7c1c49ca3fbb3f9e9804ebaf8a
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ Icodi - Deterministic Random SVG Icon Generator
2
+ ==================================================
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/icodi.svg)](https://badge.fury.io/rb/icodi)
5
+ [![Build Status](https://travis-ci.com/DannyBen/icodi.svg?branch=master)](https://travis-ci.com/DannyBen/icodi)
6
+
7
+ ---
8
+
9
+ Generate repeatable random SVG icons from any string, similar to GitHub's
10
+ identicons.
11
+
12
+ ---
13
+
14
+ Installation
15
+ --------------------------------------------------
16
+
17
+ $ gem install icodi
18
+
19
+
20
+
21
+ Usage
22
+ --------------------------------------------------
23
+
24
+ TODO
data/lib/icodi.rb ADDED
@@ -0,0 +1,110 @@
1
+ require 'victor'
2
+ require 'digest/md5'
3
+
4
+ class Icodi < Victor::SVGBase
5
+ attr_reader :text, :options
6
+
7
+ def initialize(text = nil, options = {})
8
+ text, options = nil, text if text.is_a? Hash
9
+ @text, @options = text, options
10
+ super template: template, viewBox: "0 0 #{size} #{size}"
11
+ generate
12
+ end
13
+
14
+ def template
15
+ options[:template] ||= :default
16
+ end
17
+
18
+ def pixels
19
+ options[:pixels] ||= 5
20
+ end
21
+
22
+ def density
23
+ options[:density] ||= 0.5
24
+ end
25
+
26
+ def stroke
27
+ options[:stroke] ||= 1
28
+ end
29
+
30
+ def background
31
+ options[:background] ||= '#fff'
32
+ end
33
+
34
+ def color
35
+ options[:color] ||= "#%06x" % (random.rand * 0xffffff)
36
+ end
37
+
38
+ def mirror
39
+ options[:mirror] ||= :x
40
+ end
41
+
42
+ private
43
+
44
+ def seed(string)
45
+ Digest::MD5.hexdigest(string).to_i(16)
46
+ end
47
+
48
+ def random
49
+ @random ||= (text ? Random.new(seed(text)) : Random.new)
50
+ end
51
+
52
+ def size
53
+ @size ||= pixels * 10
54
+ end
55
+
56
+ def style
57
+ @style ||= { stroke: color, stroke_width: stroke }
58
+ end
59
+
60
+ def mirror_x
61
+ [:x, :both].include? mirror
62
+ end
63
+
64
+ def mirror_y
65
+ [:y, :both].include? mirror
66
+ end
67
+
68
+ def mirror_both
69
+ mirror == :both
70
+ end
71
+
72
+ def generate
73
+ color # always randomize color first
74
+ element :rect, x: 0, y: 0, width: size, height: size, fill: background
75
+ half = (pixels / 2.0).round
76
+ x = mirror_x ? half : pixels
77
+ y = mirror_y ? half : pixels
78
+
79
+ draw matrix x, y
80
+ end
81
+
82
+ def draw(grid)
83
+ grid.each_with_index do |row, y|
84
+ row.each_with_index do |enabled, x|
85
+ if enabled
86
+ add_pixel x, y
87
+ add_pixel pixels-1-x, y if mirror_x and x != pixels/2
88
+ add_pixel x, pixels-1-y if mirror_y and y != pixels/2
89
+ add_pixel pixels-1-x, pixels-1-y if mirror_both and x != pixels/2 and y != pixels/2
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ def matrix(xtimes, ytimes)
96
+ matrix = []
97
+ ytimes.times do |y|
98
+ matrix[y] = []
99
+ xtimes.times do |x|
100
+ matrix[y][x] = random.rand < density
101
+ end
102
+ end
103
+
104
+ matrix
105
+ end
106
+
107
+ def add_pixel(x, y)
108
+ element :rect, x: x*10, y: y*10, width: 10, height: 10, fill: color, style: style
109
+ end
110
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: icodi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: victor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ description: Generate repeatable random icons from any string
28
+ email: db@dannyben.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - README.md
34
+ - lib/icodi.rb
35
+ homepage: https://github.com/dannyben/icodi
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.4.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.7.6
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Deterministic Random SVG Icon Generator
59
+ test_files: []