scarf 0.2.3 → 0.3.0

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: a32418ae1867d5b433b912e3d2caf903fb856cc7
4
- data.tar.gz: 306f599cddd5e45ae196602e83484531d0c28aa7
2
+ SHA256:
3
+ metadata.gz: 48f39ad7df57e0c4e3ec6dd7783c3e7f7c2f01476b57506919db2cd7b46eb348
4
+ data.tar.gz: 4b874107a0b23d05ee5a5cc348ae1d73d3691d08a35de25bae1aa250014ddb24
5
5
  SHA512:
6
- metadata.gz: 903608e5c91661a502524467265a4914323efa87210ccdb5660722b20fbcec50a942869b1636e3891f96531494a5cda31ca9c9348fde42b5678b95f8eba4f2ea
7
- data.tar.gz: 5a28bb71a8b3902e4c08a28cb3c720e34034559cea2106f6d231b0f39a3f1e09440139e3d12f4ba96a2681806dd5dd032b9147c5201f7bddbdb6482b72bac2a0
6
+ metadata.gz: 15becc41b860df0959b2474733b32fdc6d009f4cfc91cbf8f103fcdfa42f6ac0fb724690688abdbcbdfdc0c74fe58da8c3f4e674d59f3b4836a8d9e131e6abbb
7
+ data.tar.gz: 412ba858472718fa74d335a73be41c2c36a1710e353d71482b448731bfa45e18954994c33b569dd82e89e3f7624a717f01aa1e4a04da82fd7e4bc5cd32fe0b40
@@ -1,10 +1,20 @@
1
1
  module Scarf
2
2
  # Store configuration for this gem.
3
3
  class Configuration
4
+ BACKGROUND = %w(#ff4040 #7f2020 #cc5c33 #734939 #bf9c8f #995200 #4c2900 #f2a200 #ffd580 #332b1a #4c3d00 #ffee00 #b0b386 #64664d #6c8020 #c3d96c #143300 #19bf00 #53a669 #bfffd9 #40ffbf #1a332e #00b3a7 #165955 #00b8e6 #69818c #005ce6 #6086bf #000e66 #202440 #393973 #4700b3 #2b0d33 #aa86b3 #ee00ff #bf60b9 #4d3949 #ff00aa #7f0044 #f20061 #330007 #d96c7b).freeze
4
5
  attr_accessor :font_family
6
+ attr_accessor :font_weight
7
+ attr_accessor :font_size
8
+ attr_accessor :background_shape
9
+ attr_accessor :background_colors
5
10
 
6
11
  def initialize
7
12
  @font_family = %w(sans-serif)
13
+ # normal | bold | bolder | lighter | <number>
14
+ @font_weight = 'normal'
15
+ @font_size = '42'
16
+ @background_shape = :circle
17
+ @background_colors = BACKGROUND
8
18
  end
9
19
  end
10
20
  end
@@ -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
@@ -1,23 +1,36 @@
1
1
  module Scarf
2
2
  class InitialAvatar
3
- def initialize(name, font_family: [])
3
+ def initialize(name, font_family: [], font_weight: nil, font_size: nil, background_shape: nil, background_colors: [])
4
4
  @name = name
5
- @font_family = font_family.length > 0 ? font_family : Scarf.configuration.font_family
5
+ @font_family = font_family.length.zero? ? Scarf.configuration.font_family : font_family
6
+ @font_weight = font_weight.nil? ? Scarf.configuration.font_weight : font_weight
7
+ @font_size = font_size.nil? ? Scarf.configuration.font_size : font_size
8
+ @background_shape = background_shape.nil? ? Scarf.configuration.background_shape : background_shape
9
+ @background_colors = background_colors.length.zero? ? Scarf.configuration.background_colors : background_colors
6
10
  end
7
11
 
8
- BACKGROUND = %w(#ff4040 #7f2020 #cc5c33 #734939 #bf9c8f #995200 #4c2900 #f2a200 #ffd580 #332b1a #4c3d00 #ffee00 #b0b386 #64664d #6c8020 #c3d96c #143300 #19bf00 #53a669 #bfffd9 #40ffbf #1a332e #00b3a7 #165955 #00b8e6 #69818c #005ce6 #6086bf #000e66 #202440 #393973 #4700b3 #2b0d33 #aa86b3 #ee00ff #bf60b9 #4d3949 #ff00aa #7f0044 #f20061 #330007 #d96c7b).freeze
9
-
10
12
  def svg
11
13
  <<~WRAPPER.gsub(/$\s+/, '').strip
12
14
  <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 100 100">
13
- <circle cx="50" cy="50" r="50" fill="#{background_fill}" />
14
- <text fill="#{foreground_fill}" font-size="50" font-family="#{quoted_fonts}" x="50" y="50" text-anchor="middle" alignment-baseline="central">#{initials}</text>
15
+ #{background}
16
+ <text fill="#{foreground_fill}" font-size="#{@font_size}" font-weight="#{@font_weight}" font-family="#{quoted_fonts}" x="50" y="54" text-anchor="middle" dominant-baseline="middle" alignment-baseline="middle">#{initials}</text>
15
17
  </svg>
16
18
  WRAPPER
17
19
  end
18
20
 
19
21
  private
20
22
 
23
+ def background
24
+ case(@background_shape)
25
+ when :square
26
+ return "<rect width=\"100\" height=\"100\" fill=\"#{background_fill}\" />"
27
+ when :circle
28
+ return "<circle cx=\"50\" cy=\"50\" r=\"50\" fill=\"#{background_fill}\" />"
29
+ else
30
+ raise "Unknown background_shape #{@background_shape} supplied to Scarf::InitialAvatar"
31
+ end
32
+ end
33
+
21
34
  # Surround the font name with single quotes if there's a space in the name, and isn't quoted already.
22
35
  def quoted_fonts
23
36
  quoted_fonts = @font_family.map do |font|
@@ -35,8 +48,8 @@ module Scarf
35
48
  def background_fill
36
49
  @background_fill ||= begin
37
50
  digest = "0.#{Digest::MD5.hexdigest(@name).to_i(16).to_s}".to_f
38
- index = (digest * (BACKGROUND.length - 1)).round
39
- BACKGROUND[index]
51
+ index = (digest * (@background_colors.length - 1)).round
52
+ @background_colors[index]
40
53
  end
41
54
  end
42
55
 
data/lib/scarf/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Scarf
2
- VERSION = '0.2.3'
2
+ VERSION = '0.3.0'
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.3
4
+ version: 0.3.0
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: 2021-12-21 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,7 +53,22 @@ dependencies:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
55
  version: '3.5'
56
- description: A Ruby library for generating initial avatars and identicons.
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'
70
+ description: A Ruby library for generating initial avatars and identicons, with configurable
71
+ fonts, colors, and shapes.
57
72
  email: mail@harigopal.in
58
73
  executables: []
59
74
  extensions: []
@@ -85,8 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
100
  - !ruby/object:Gem::Version
86
101
  version: '0'
87
102
  requirements: []
88
- rubyforge_project:
89
- rubygems_version: 2.6.11
103
+ rubygems_version: 3.2.32
90
104
  signing_key:
91
105
  specification_version: 4
92
106
  summary: A Ruby library for generating initial avatars and identicons.