scarf 0.2.5 → 0.2.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 77612c75d65fee88ee919647a380bcce491fe3e0
4
- data.tar.gz: 65f8017c98365138bd71bc6f30f9a6887a7a7d18
2
+ SHA256:
3
+ metadata.gz: 35752b488c66f0c6531356f863c482aa726fd701d00d26f4d6fbb063629553cf
4
+ data.tar.gz: 5657738a866479e6ee540f184491244aaa9559673a630293383fbbb73c23cb0d
5
5
  SHA512:
6
- metadata.gz: 9b550ced796548c5c26c11d095506f4ce5aadf5deeacf69fe84ff6e37151b0117b56d8ecb0657ce6a9fc5afcab86af846465eb7aea5d40aa1a88ded1a514c7e9
7
- data.tar.gz: a106f0b75fcd42547d52867c27436508697d37692d08708a803adda2b8240c1291c14b9b51144a214687fd025a057e95c69baa08cefd7572754ead637146d162
6
+ metadata.gz: 05a05ebd174085dd060904e16703b89fedd974ef79c2a77a6dc7cd5739b07504f6024b8f21fdb22d63121b3b11b1896fd7a75cd614091de781b42f0022277a78
7
+ data.tar.gz: 5776353197f6e6e09e290b2141a100cf3086a58aa7b68851b4f7aef904bf0a69f0140d5e76300abcef05b74a05116730656be252d14412cd2c74e1725b6f9819
@@ -17,56 +17,54 @@ module Scarf
17
17
  [10, 2, 12, 10],
18
18
  [0, 2, 10, 0],
19
19
  [],
20
- ]
20
+ ].freeze
21
21
 
22
- CENTER_PATCHES = [0, 4, 8, 15]
22
+ CENTER_PATCHES = [0, 4, 8, 15].freeze
23
23
  PATCH_SIZE = 5
24
24
 
25
25
  attr_reader :code
26
26
 
27
27
  def initialize(str = '', opt = {})
28
- case opt[:type]
28
+ @code = case opt[:type]
29
29
  when :code
30
- @code = str.to_i
30
+ str.to_i
31
31
  when :ip
32
- @code = Identicon.ip2code str
32
+ Identicon.ip2code str
33
33
  else
34
- @code = Identicon.calc_code str.to_s
34
+ Identicon.calc_code str.to_s
35
35
  end
36
36
 
37
37
  @decode = decode @code
38
38
 
39
- if opt[:size]
40
- @scale = opt[:size].to_f / (PATCH_SIZE * 3)
39
+ @scale = if opt[:size]
40
+ opt[:size].to_f / (PATCH_SIZE * 3)
41
41
  else
42
- @scale = opt[:scale] || 1
42
+ opt[:scale] || 1
43
43
  end
44
44
 
45
45
  @image_lib = ImageSVG
46
-
47
- @transparent = !!opt[:transparent] &&
48
- [ImageRmagick, ImageSVG].include?(@image_lib)
46
+ @transparent = opt[:transparent]
49
47
  @patch_width = PATCH_SIZE * @scale
50
- @image = @image_lib.new(@patch_width * 3, @patch_width * 3,
51
- :transparent => @transparent)
48
+ @image = @image_lib.new(@patch_width * 3, @patch_width * 3, transparent: @transparent)
52
49
  @back_color = @image.color 255, 255, 255
53
50
  @fore_color = opt[:color] || @image.color(@decode[:red], @decode[:green], @decode[:blue])
51
+
54
52
  render
55
53
  end
56
54
 
57
55
  def decode(code)
58
56
  {
59
- :center_type => (code & 0x3),
60
- :center_invert => (((code >> 2) & 0x01) != 0),
61
- :corner_type => ((code >> 3) & 0x0f),
62
- :corner_invert => (((code >> 7) & 0x01) != 0),
63
- :corner_turn => ((code >> 8) & 0x03),
64
- :side_type => ((code >> 10) & 0x0f),
65
- :side_invert => (((code >> 14) & 0x01) != 0),
66
- :side_turn => ((code >> 15) & 0x03),
67
- :red => (((code >> 16) & 0x01f) << 3),
68
- :green => (((code >> 21) & 0x01f) << 3),
69
- :blue => (((code >> 27) & 0x01f) << 3),
57
+ center_type: (code & 0x3),
58
+ center_invert: (((code >> 2) & 0x01) != 0),
59
+ corner_type: ((code >> 3) & 0x0f),
60
+ corner_invert: (((code >> 7) & 0x01) != 0),
61
+ corner_turn: ((code >> 8) & 0x03),
62
+ side_type: ((code >> 10) & 0x0f),
63
+ side_invert: (((code >> 14) & 0x01) != 0),
64
+ side_turn: ((code >> 15) & 0x03),
65
+ red: (((code >> 16) & 0x01f) << 3),
66
+ green: (((code >> 21) & 0x01f) << 3),
67
+ blue: (((code >> 27) & 0x01f) << 3),
70
68
  }
71
69
  end
72
70
 
@@ -75,18 +73,14 @@ module Scarf
75
73
  side = [[1, 0], [2, 1], [1, 2], [0, 1]]
76
74
  corner = [[0, 0], [2, 0], [2, 2], [0, 2]]
77
75
 
78
- draw_patches(center, CENTER_PATCHES[@decode[:center_type]],
79
- 0, @decode[:center_invert])
80
- draw_patches(side, @decode[:side_type],
81
- @decode[:side_turn], @decode[:side_invert])
82
- draw_patches(corner, @decode[:corner_type],
83
- @decode[:corner_turn], @decode[:corner_invert])
76
+ draw_patches(center, CENTER_PATCHES[@decode[:center_type]], 0, @decode[:center_invert])
77
+ draw_patches(side, @decode[:side_type], @decode[:side_turn], @decode[:side_invert])
78
+ draw_patches(corner, @decode[:corner_type], @decode[:corner_turn], @decode[:corner_invert])
84
79
  end
85
80
 
86
81
  def draw_patches(list, patch, turn, invert)
87
82
  list.each do |i|
88
- draw(:x => i[0], :y => i[1], :patch => patch,
89
- :turn => turn, :invert => invert)
83
+ draw(x: i[0], y: i[1], patch: patch, turn: turn, invert: invert)
90
84
  turn += 1
91
85
  end
92
86
  end
@@ -97,37 +91,22 @@ module Scarf
97
91
  patch = opt[:patch] % PATCHES.size
98
92
  turn = opt[:turn] % 4
99
93
 
100
- if opt[:invert]
101
- fore, back = @back_color, @fore_color
94
+ fore, back = if opt[:invert]
95
+ [@back_color, @fore_color]
102
96
  else
103
- fore, back = @fore_color, @back_color
97
+ [@fore_color, @back_color]
104
98
  end
105
99
 
106
- offset = (@image_lib == Scarf::ImageSVG) ? 0 : 1
100
+ offset = @image_lib == Scarf::ImageSVG ? 0 : 1
107
101
 
108
- if !(@transparent && back == @back_color)
109
- @image.fill_rect(x, y, x + @patch_width - offset,
110
- y + @patch_width - offset, back)
102
+ unless @transparent && back == @back_color
103
+ @image.fill_rect(
104
+ x, y, x + @patch_width - offset,
105
+ y + @patch_width - offset, back
106
+ )
111
107
  end
112
108
 
113
- points = []
114
- PATCHES[patch].each do |pt|
115
- dx = pt % PATCH_SIZE
116
- dy = pt / PATCH_SIZE
117
- len = @patch_width - offset
118
- px = dx.to_f / (PATCH_SIZE - 1) * len
119
- py = dy.to_f / (PATCH_SIZE - 1) * len
120
-
121
- case turn
122
- when 1
123
- px, py = len - py, px
124
- when 2
125
- px, py = len - px, len - py
126
- when 3
127
- px, py = py, len - px
128
- end
129
- points << [x + px, y + py]
130
- end
109
+ points = compute_points(offset, patch, turn, x, y)
131
110
 
132
111
  if @transparent
133
112
  if fore == @back_color
@@ -162,15 +141,39 @@ module Scarf
162
141
  end
163
142
 
164
143
  def self.extract_code(list)
165
- if list.respond_to?(:getbyte)
166
- tmp = [list.getbyte(0) << 24, list.getbyte(1) << 16,
167
- list.getbyte(2) << 8, list.getbyte(3)]
144
+ tmp = if list.respond_to?(:getbyte)
145
+ [list.getbyte(0) << 24, list.getbyte(1) << 16, list.getbyte(2) << 8, list.getbyte(3)]
168
146
  else
169
- tmp = [list[0].to_i << 24, list[1].to_i << 16,
170
- list[2].to_i << 8, list[3].to_i]
147
+ [list[0].to_i << 24, list[1].to_i << 16, list[2].to_i << 8, list[3].to_i]
171
148
  end
149
+
172
150
  tmp.inject(0) do |r, i|
173
- r | ((i[31] == 1) ? -(i & 0x7fffffff) : i)
151
+ r | (i[31] == 1 ? -(i & 0x7fffffff) : i)
152
+ end
153
+ end
154
+
155
+ private
156
+
157
+ def compute_points(offset, patch, turn, x, y)
158
+ PATCHES[patch].map do |pt|
159
+ dx = pt % PATCH_SIZE
160
+ dy = pt / PATCH_SIZE
161
+ len = @patch_width - offset
162
+ px = dx.to_f / (PATCH_SIZE - 1) * len
163
+ py = dy.to_f / (PATCH_SIZE - 1) * len
164
+
165
+ px, py = case turn
166
+ when 1
167
+ [len - py, px]
168
+ when 2
169
+ [len - px, len - py]
170
+ when 3
171
+ [py, len - px]
172
+ else
173
+ [px, py]
174
+ end
175
+
176
+ [x + px, y + py]
174
177
  end
175
178
  end
176
179
  end
@@ -12,7 +12,7 @@ module Scarf
12
12
  <<~WRAPPER.gsub(/$\s+/, '').strip
13
13
  <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100">
14
14
  #{background}
15
- <text fill="#{foreground_fill}" font-size="50" font-family="#{quoted_fonts}" x="50" y="50" text-anchor="middle" alignment-baseline="central">#{initials}</text>
15
+ <text fill="#{foreground_fill}" font-size="42" font-family="#{quoted_fonts}" x="50" y="54" text-anchor="middle" dominant-baseline="middle" alignment-baseline="middle">#{initials}</text>
16
16
  </svg>
17
17
  WRAPPER
18
18
  end
@@ -1,3 +1,3 @@
1
1
  module Scarf
2
- VERSION = '0.2.5'
2
+ VERSION = '0.2.6'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scarf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hari Gopal
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-04-21 00:00:00.000000000 Z
12
+ date: 2020-02-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '12.0'
20
+ version: '13.0'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '12.0'
27
+ version: '13.0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: test-unit
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -53,6 +53,20 @@ dependencies:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '3.5'
56
+ - !ruby/object:Gem::Dependency
57
+ name: sinatra
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.0'
56
70
  description: A Ruby library for generating initial avatars and identicons.
57
71
  email: mail@harigopal.in
58
72
  executables: []
@@ -85,8 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
99
  - !ruby/object:Gem::Version
86
100
  version: '0'
87
101
  requirements: []
88
- rubyforge_project:
89
- rubygems_version: 2.6.11
102
+ rubygems_version: 3.1.2
90
103
  signing_key:
91
104
  specification_version: 4
92
105
  summary: A Ruby library for generating initial avatars and identicons.