svg_profiler 1.1.1 → 2.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 403478f11dec0b9d0c87c8446fc723c0b40014a2
4
+ data.tar.gz: 60c06392446473c358969008666e971377493260
5
+ SHA512:
6
+ metadata.gz: 6d18996cd7623910260042b1238d98119c0b7a01637b10fdefee528fb36add99263d96d674e7ac9710bc121f22588a2e69d71b46bcc8bdcf260896863419c97d
7
+ data.tar.gz: 1fe9346efa9380632229012c407079c36e991222c1893575982519d0516c641ef06352b2b6b48d0a3f493df9fad60e57fe7c6a7be3500df59c18199c2f4ead74
data/README.md CHANGED
@@ -28,21 +28,13 @@ profile.height
28
28
  profile.aspect
29
29
  #=> 1.5
30
30
 
31
- # The #colors method calculates the histogram for the SVG.
32
- profile.colors
31
+ profile.histogram
33
32
  #=> { "#FF0000" => 0.7, "#FFFFFF" => 0.29, "#000000" => 0.01 }
34
33
  ```
35
34
 
36
- You may find that the SVG is [antialiased](http://en.wikipedia.org/wiki/Spatial_anti-aliasing) before the histogram is calculated.
35
+ When calculating the histogram, the SVG is rasterized to a PNG and anti-aliased.
37
36
 
38
- You can pass an optional threshold to the `#colors` method, to help cope with this:
39
-
40
- ```ruby
41
- profile.colors(0.02)
42
- #=> { "#FF0000" => 0.7070707070707071, "#FFFFFF" => 0.29292929292929293 }
43
- ```
44
-
45
- The sum of the values in the hash will always add up to one.
37
+ This may have a small impact on the color ratios.
46
38
 
47
39
  ## Contribution
48
40
 
data/lib/svg_profiler.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "tempfile"
2
2
 
3
3
  require "rmagick"
4
+ require "svg_palette"
4
5
 
5
6
  require "svg_profiler/base"
6
7
  require "svg_profiler/inkscape"
@@ -1,6 +1,7 @@
1
1
  class SVGProfiler
2
2
 
3
3
  def initialize(xml_string)
4
+ @xml_string = xml_string
4
5
  @inkscape = Inkscape.new(xml_string)
5
6
  end
6
7
 
@@ -20,8 +21,8 @@ class SVGProfiler
20
21
  width.to_f / height
21
22
  end
22
23
 
23
- def histogram(threshold = 0)
24
- @inkscape.yield_png { |png| Histogram.for(png, threshold) }
24
+ def histogram
25
+ @inkscape.yield_png { |png| Histogram.for(@xml_string, png) }
25
26
  end
26
27
 
27
28
  end
@@ -1,15 +1,18 @@
1
1
  class SVGProfiler::Histogram
2
2
 
3
- def self.for(png, threshold)
4
- new(png).histogram(threshold)
3
+ WARN_THRESHOLD = 0.02
4
+
5
+ def self.for(xml_string, png)
6
+ new(xml_string, png).histogram
5
7
  end
6
8
 
7
- def initialize(png)
9
+ def initialize(xml_string, png)
8
10
  check_for_imagemagick
9
11
  @image = Magick::Image.read(png.path).first
12
+ @palette = SVGPalette.parse(xml_string)
10
13
  end
11
14
 
12
- def histogram(threshold)
15
+ def histogram
13
16
  @image.quantize(8) # 00-FF is 8-bits.
14
17
 
15
18
  histogram = @image.color_histogram
@@ -17,11 +20,8 @@ class SVGProfiler::Histogram
17
20
  hash.merge(to_hex(p) => f)
18
21
  end
19
22
 
20
- ratios = normalize(frequencies)
21
- ratios.reject! { |_, v| v < threshold }
22
-
23
- # Re-normalize after thresholding.
24
- normalize(ratios)
23
+ filter_by_palette!(frequencies)
24
+ normalize(frequencies)
25
25
  end
26
26
 
27
27
  private
@@ -38,6 +38,30 @@ class SVGProfiler::Histogram
38
38
  pixel.to_color(Magick::AllCompliance, false, 8, true)
39
39
  end
40
40
 
41
+ # When the png is created, it gets anti-aliased.
42
+ # If it is down-sampled, you'll end up with many midway colors.
43
+ # These have a negligible pixel count and could be thresholded out.
44
+ # This seems like a better approach, assuming the palette is correct.
45
+ def filter_by_palette!(hash)
46
+ hex_codes = @palette.map(&:html)
47
+ pre_filter = normalize(hash)
48
+
49
+ hash.reject! do |k, v|
50
+ if !hex_codes.include?(k.downcase)
51
+ warn_if_filtering_a_prominent_color(k, pre_filter)
52
+ true
53
+ end
54
+ end
55
+ end
56
+
57
+ def warn_if_filtering_a_prominent_color(hex, normalized)
58
+ if normalized[hex] > WARN_THRESHOLD
59
+ puts "\n#{__FILE__}:#{__LINE__}"
60
+ puts "WARNING: '#{hex}' was been assumed to be an artifact of anti-aliasing when perhaps it was not."
61
+ puts "Please email your SVG to chris@patuzzo.co.uk, with a subject of `SVG Filter Threshold'."
62
+ end
63
+ end
64
+
41
65
  def normalize(frequencies)
42
66
  sum = frequencies.values.inject(:+)
43
67
  frequencies.inject({}) do |hash, (k, v)|
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svg_profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
5
- prerelease:
4
+ version: 2.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Chris Patuzzo
@@ -14,33 +13,43 @@ dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rmagick
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: svg_palette
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
28
39
  - !ruby/object:Gem::Version
29
40
  version: '0'
30
41
  - !ruby/object:Gem::Dependency
31
42
  name: rspec
32
43
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
44
  requirements:
35
- - - ! '>='
45
+ - - '>='
36
46
  - !ruby/object:Gem::Version
37
47
  version: '0'
38
48
  type: :development
39
49
  prerelease: false
40
50
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
51
  requirements:
43
- - - ! '>='
52
+ - - '>='
44
53
  - !ruby/object:Gem::Version
45
54
  version: '0'
46
55
  description: Profiles a Scalable Vector Graphics xml string.
@@ -56,27 +65,25 @@ files:
56
65
  - lib/svg_profiler.rb
57
66
  homepage: https://github.com/cpatuzzo/svg_profiler
58
67
  licenses: []
68
+ metadata: {}
59
69
  post_install_message:
60
70
  rdoc_options: []
61
71
  require_paths:
62
72
  - lib
63
73
  required_ruby_version: !ruby/object:Gem::Requirement
64
- none: false
65
74
  requirements:
66
- - - ! '>='
75
+ - - '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: '0'
69
78
  required_rubygems_version: !ruby/object:Gem::Requirement
70
- none: false
71
79
  requirements:
72
- - - ! '>='
80
+ - - '>='
73
81
  - !ruby/object:Gem::Version
74
82
  version: '0'
75
83
  requirements: []
76
84
  rubyforge_project:
77
- rubygems_version: 1.8.23
85
+ rubygems_version: 2.0.0
78
86
  signing_key:
79
- specification_version: 3
87
+ specification_version: 4
80
88
  summary: SVG Profiler
81
89
  test_files: []
82
- has_rdoc: