svg_palette 1.0.0

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 +28 -0
  3. data/lib/svg_palette.rb +54 -0
  4. metadata +86 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be6584ef0684e3a771308f93c910b478249d8d27
4
+ data.tar.gz: 15eaa3eaff331ebddfffe7354c95ba0fcaaea542
5
+ SHA512:
6
+ metadata.gz: 14f98bda4eb76c80ad8e3435880dd7718fedafc55b325feee24988edcd5b178047f5b07ceae1a48af37f7fb2bd7eabd752ab93d3dafe0edf51d510e2f7027a97
7
+ data.tar.gz: ee61586c13a11136710e5d51a99512c5fe4277a0850311ccabbde26959328b335250aea87585798ec3eb94a33dd30ae1d7a6c12fe4081655d7678da40c16f5d2
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ ## SVG Palette
2
+
3
+ Parse an SVG to discover which colors it contains.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ require "svg_palette"
9
+
10
+ SVGPalette.parse(File.read("flag_of_togo.svg"))
11
+ #=> [RGB [#006a4e], RGB [#ffce00], RGB [#d21034], RGB [#ffffff]]
12
+ ```
13
+
14
+ The array contains instances of [Color](http://rubydoc.info/gems/color/1.4.1/frames).
15
+
16
+ ## Properties
17
+
18
+ The following properties are considered:
19
+
20
+ ```
21
+ fill stroke stop-color flood-color lighting-color style
22
+ ```
23
+
24
+ When checking style rules, only the keys for those properties are considered, e.g:
25
+
26
+ ```xml
27
+ <foo style="fill:red"></foo>
28
+ ```
@@ -0,0 +1,54 @@
1
+ require "nokogiri"
2
+ require "css_color"
3
+
4
+ class SVGPalette
5
+
6
+ PROPERTIES = %w(
7
+ fill stroke stop-color flood-color lighting-color style
8
+ ).freeze
9
+
10
+ def initialize(xml_string)
11
+ @xml = Nokogiri::XML(xml_string)
12
+ end
13
+
14
+ def self.parse(xml_string)
15
+ new(xml_string).parse
16
+ end
17
+
18
+ def parse
19
+ squash(values.map { |v| CSSColor.parse(v) rescue nil })
20
+ end
21
+
22
+ private
23
+ def values
24
+ squash(property_values.map { |v| css_rule?(v) ? css_values(v) : v })
25
+ end
26
+
27
+ def property_values
28
+ squash(PROPERTIES.map { |p| nodes.map { |n| n.attr(p) } })
29
+ end
30
+
31
+ def nodes
32
+ matcher = lambda { |attr| "//*[@#{attr}]" }
33
+ query = PROPERTIES.map(&matcher).join("|")
34
+
35
+ @xml.xpath(query)
36
+ end
37
+
38
+ def css_rule?(string)
39
+ string =~ /:/
40
+ end
41
+
42
+ def css_values(string)
43
+ rules = string.split(";")
44
+ rules.map do |rule|
45
+ key, value = rule.split(":")
46
+ value if PROPERTIES.include?(key)
47
+ end
48
+ end
49
+
50
+ def squash(array)
51
+ array.flatten.compact.uniq
52
+ end
53
+
54
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: svg_palette
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Patuzzo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: css_color
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
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Parse an SVG to discover which colors it contains.
56
+ email: chris@patuzzo.co.uk
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - README.md
62
+ - lib/svg_palette.rb
63
+ homepage: https://github.com/cpatuzzo/svg_palette
64
+ licenses: []
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.0.0
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: SVG Palette
86
+ test_files: []