color_parser 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +0 -2
- data/README.md +7 -1
- data/color_parser.gemspec +2 -1
- data/lib/color_parser.rb +1 -3
- data/lib/color_parser/stylesheet.rb +7 -34
- data/lib/color_parser/version.rb +1 -1
- metadata +23 -8
- data/lib/color_parser/color.rb +0 -154
- data/spec/color_spec.rb +0 -10
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -11,6 +11,12 @@ Get colors on a given webpage
|
|
11
11
|
```ruby
|
12
12
|
page = ColorParser::Page.new("http://google.com/")
|
13
13
|
colors = page.colors
|
14
|
+
=> {"ffffff"=>5, "c9d7f1"=>1, "0000cc"=>2, "dd8e27"=>1, "990000"=>1,
|
15
|
+
"3366cc"=>3, "000000"=>2, "1111cc"=>5, "cccccc"=>2, "551a8b"=>1}
|
16
|
+
|
17
|
+
colors = page.colors_by_frequency
|
18
|
+
=> ["ffffff", "1111cc", "3366cc", "000000", "cccccc",
|
19
|
+
"0000cc", "dd8e27", "c9d7f1", "990000", "551a8b"]
|
14
20
|
```
|
15
21
|
|
16
22
|
## Installation
|
@@ -18,7 +24,7 @@ colors = page.colors
|
|
18
24
|
To install ColorParser, add the gem to your Gemfile:
|
19
25
|
|
20
26
|
```ruby
|
21
|
-
gem "color_parser"
|
27
|
+
gem "color_parser"
|
22
28
|
```
|
23
29
|
|
24
30
|
## LICENSE
|
data/color_parser.gemspec
CHANGED
@@ -21,7 +21,8 @@ Gem::Specification.new do |gem|
|
|
21
21
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
22
22
|
gem.require_paths = ["lib"]
|
23
23
|
|
24
|
-
gem.add_runtime_dependency("stylesheet",
|
24
|
+
gem.add_runtime_dependency("stylesheet", "~> 0.1.4")
|
25
|
+
gem.add_runtime_dependency("color_conversion", "~> 0.1.0")
|
25
26
|
|
26
27
|
gem.add_development_dependency("rake")
|
27
28
|
gem.add_development_dependency("rspec", "~> 2.9")
|
data/lib/color_parser.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
require 'uri'
|
2
|
-
require 'curb'
|
3
|
-
require 'nokogiri'
|
4
2
|
require 'stylesheet'
|
3
|
+
require 'color_conversion'
|
5
4
|
|
6
5
|
require 'color_parser/errors'
|
7
6
|
require 'color_parser/version'
|
8
7
|
require 'color_parser/page'
|
9
8
|
require 'color_parser/stylesheet'
|
10
|
-
require 'color_parser/color'
|
11
9
|
|
12
10
|
module ColorParser
|
13
11
|
end
|
@@ -82,47 +82,20 @@ module ColorParser
|
|
82
82
|
|
83
83
|
def parse_colors(property_list)
|
84
84
|
colors = {}
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
# hex
|
90
|
-
hex = if matches = value.match(/#([0-9a-f]{3,6})/i)
|
91
|
-
normalize_hex(matches[1])
|
92
|
-
|
93
|
-
# rgb/rgba
|
94
|
-
elsif matches = value.match(/rgba?\((\d{1,3}[,\s]+\d{1,3}[,\s]+\d{1,3})/)
|
95
|
-
rgb_to_hex(matches[1])
|
96
|
-
|
97
|
-
# textual
|
98
|
-
elsif matches = value.match(/(#{text_colors})/)
|
99
|
-
text_to_hex(matches[1])
|
85
|
+
property_list.each do |key, value|
|
86
|
+
color = nil
|
87
|
+
value.gsub(/\s?,\s?/, ",").split(" ").each do |part|
|
88
|
+
color ||= ColorConversion::Color.new(part) rescue nil
|
100
89
|
end
|
90
|
+
next unless color
|
101
91
|
|
102
|
-
|
103
|
-
|
92
|
+
hex = color.hex.gsub("#", "")
|
104
93
|
colors[hex] ? colors[hex] += 1 : colors[hex] = 1
|
105
94
|
end
|
106
95
|
|
107
96
|
# sort by colors with most occurrances
|
108
97
|
colors
|
109
98
|
end
|
110
|
-
|
111
|
-
# convert rgb to hex
|
112
|
-
def rgb_to_hex(rgb)
|
113
|
-
r, g, b = rgb.split(",").map {|color| color.strip }
|
114
|
-
"%02x" % r + "%02x" % g + "%02x" % b
|
115
|
-
end
|
116
|
-
|
117
|
-
# find hex for textual color
|
118
|
-
def text_to_hex(color)
|
119
|
-
ColorParser::Color.text_colors[color.intern]
|
120
|
-
end
|
121
|
-
|
122
|
-
# convert 3 digit hex to 6
|
123
|
-
def normalize_hex(hex)
|
124
|
-
(hex.length == 3 ? hex[0,1]*2 + hex[1,1]*2 + hex[2,1]*2: hex).downcase
|
125
|
-
end
|
126
|
-
|
99
|
+
|
127
100
|
end
|
128
101
|
end
|
data/lib/color_parser/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: color_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-01-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: stylesheet
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 0.1.
|
21
|
+
version: 0.1.4
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,23 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 0.1.
|
29
|
+
version: 0.1.4
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: color_conversion
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.1.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.1.0
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: rake
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,12 +138,10 @@ files:
|
|
122
138
|
- Rakefile
|
123
139
|
- color_parser.gemspec
|
124
140
|
- lib/color_parser.rb
|
125
|
-
- lib/color_parser/color.rb
|
126
141
|
- lib/color_parser/errors.rb
|
127
142
|
- lib/color_parser/page.rb
|
128
143
|
- lib/color_parser/stylesheet.rb
|
129
144
|
- lib/color_parser/version.rb
|
130
|
-
- spec/color_spec.rb
|
131
145
|
- spec/fixtures/css_color/frequency.html
|
132
146
|
- spec/fixtures/css_color/stylesheets/color_styles.css
|
133
147
|
- spec/fixtures/css_color/stylesheets/css_elements.css
|
@@ -158,6 +172,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
172
|
- - ! '>='
|
159
173
|
- !ruby/object:Gem::Version
|
160
174
|
version: '0'
|
175
|
+
segments:
|
176
|
+
- 0
|
177
|
+
hash: -62843295753187331
|
161
178
|
requirements: []
|
162
179
|
rubyforge_project:
|
163
180
|
rubygems_version: 1.8.25
|
@@ -165,7 +182,6 @@ signing_key:
|
|
165
182
|
specification_version: 3
|
166
183
|
summary: Finds colors on a given webpage
|
167
184
|
test_files:
|
168
|
-
- spec/color_spec.rb
|
169
185
|
- spec/fixtures/css_color/frequency.html
|
170
186
|
- spec/fixtures/css_color/stylesheets/color_styles.css
|
171
187
|
- spec/fixtures/css_color/stylesheets/css_elements.css
|
@@ -177,4 +193,3 @@ test_files:
|
|
177
193
|
- spec/stubs/fake_request.rb
|
178
194
|
- spec/stylesheet_spec.rb
|
179
195
|
- spec/version_spec.rb
|
180
|
-
has_rdoc:
|
data/lib/color_parser/color.rb
DELETED
@@ -1,154 +0,0 @@
|
|
1
|
-
module ColorParser
|
2
|
-
class Color
|
3
|
-
|
4
|
-
def self.text_colors
|
5
|
-
{ black: "000000",
|
6
|
-
navy: "000080",
|
7
|
-
darkblue: "00008b",
|
8
|
-
mediumblue: "0000cd",
|
9
|
-
blue: "0000ff",
|
10
|
-
darkgreen: "006400",
|
11
|
-
green: "008000",
|
12
|
-
teal: "008080",
|
13
|
-
darkcyan: "008b8b",
|
14
|
-
deepskyblue: "00bfff",
|
15
|
-
darkturquoise: "00ced1",
|
16
|
-
mediumspringgreen: "00fa9a",
|
17
|
-
lime: "00ff00",
|
18
|
-
springgreen: "00ff7f",
|
19
|
-
aqua: "00ffff",
|
20
|
-
cyan: "00ffff",
|
21
|
-
midnightblue: "191970",
|
22
|
-
dodgerblue: "1e90ff",
|
23
|
-
lightseagreen: "20b2aa",
|
24
|
-
forestgreen: "228b22",
|
25
|
-
seagreen: "2e8b57",
|
26
|
-
darkslategray: "2f4f4f",
|
27
|
-
darkslategrey: "2f4f4f",
|
28
|
-
limegreen: "32cd32",
|
29
|
-
mediumseagreen: "3cb371",
|
30
|
-
turquoise: "40e0d0",
|
31
|
-
royalblue: "4169e1",
|
32
|
-
steelblue: "4682b4",
|
33
|
-
darkslateblue: "483d8b",
|
34
|
-
mediumturquoise: "48d1cc",
|
35
|
-
indigo: "4b0082",
|
36
|
-
darkolivegreen: "556b2f",
|
37
|
-
cadetblue: "5f9ea0",
|
38
|
-
cornflowerblue: "6495ed",
|
39
|
-
mediumaquamarine: "66cdaa",
|
40
|
-
dimgray: "696969",
|
41
|
-
dimgrey: "696969",
|
42
|
-
slateblue: "6a5acd",
|
43
|
-
olivedrab: "6b8e23",
|
44
|
-
slategray: "708090",
|
45
|
-
slategrey: "708090",
|
46
|
-
lightslategray: "778899",
|
47
|
-
lightslategrey: "778899",
|
48
|
-
mediumslateblue: "7b68ee",
|
49
|
-
lawngreen: "7cfc00",
|
50
|
-
chartreuse: "7fff00",
|
51
|
-
aquamarine: "7fffd4",
|
52
|
-
maroon: "800000",
|
53
|
-
purple: "800080",
|
54
|
-
olive: "808000",
|
55
|
-
gray: "808080",
|
56
|
-
grey: "808080",
|
57
|
-
skyblue: "87ceeb",
|
58
|
-
lightskyblue: "87cefa",
|
59
|
-
blueviolet: "8a2be2",
|
60
|
-
darkred: "8b0000",
|
61
|
-
darkmagenta: "8b008b",
|
62
|
-
saddlebrown: "8b4513",
|
63
|
-
darkseagreen: "8fbc8f",
|
64
|
-
lightgreen: "90ee90",
|
65
|
-
mediumpurple: "9370db",
|
66
|
-
darkviolet: "9400d3",
|
67
|
-
palegreen: "98fb98",
|
68
|
-
darkorchid: "9932cc",
|
69
|
-
yellowgreen: "9acd32",
|
70
|
-
sienna: "a0522d",
|
71
|
-
brown: "a52a2a",
|
72
|
-
darkgray: "a9a9a9",
|
73
|
-
darkgrey: "a9a9a9",
|
74
|
-
lightblue: "add8e6",
|
75
|
-
greenyellow: "adff2f",
|
76
|
-
paleturquoise: "afeeee",
|
77
|
-
lightsteelblue: "b0c4de",
|
78
|
-
powderblue: "b0e0e6",
|
79
|
-
firebrick: "b22222",
|
80
|
-
darkgoldenrod: "b8860b",
|
81
|
-
mediumorchid: "ba55d3",
|
82
|
-
rosybrown: "bc8f8f",
|
83
|
-
darkkhaki: "bdb76b",
|
84
|
-
silver: "c0c0c0",
|
85
|
-
mediumvioletred: "c71585",
|
86
|
-
indianred: "cd5c5c",
|
87
|
-
peru: "cd853f",
|
88
|
-
chocolate: "d2691e",
|
89
|
-
tan: "d2b48c",
|
90
|
-
lightgray: "d3d3d3",
|
91
|
-
lightgrey: "d3d3d3",
|
92
|
-
thistle: "d8bfd8",
|
93
|
-
orchid: "da70d6",
|
94
|
-
goldenrod: "daa520",
|
95
|
-
palevioletred: "db7093",
|
96
|
-
crimson: "dc143c",
|
97
|
-
gainsboro: "dcdcdc",
|
98
|
-
plum: "dda0dd",
|
99
|
-
burlywood: "deb887",
|
100
|
-
lightcyan: "e0ffff",
|
101
|
-
lavender: "e6e6fa",
|
102
|
-
darksalmon: "e9967a",
|
103
|
-
violet: "ee82ee",
|
104
|
-
palegoldenrod: "eee8aa",
|
105
|
-
lightcoral: "f08080",
|
106
|
-
khaki: "f0e68c",
|
107
|
-
aliceblue: "f0f8ff",
|
108
|
-
honeydew: "f0fff0",
|
109
|
-
azure: "f0ffff",
|
110
|
-
sandybrown: "f4a460",
|
111
|
-
wheat: "f5deb3",
|
112
|
-
beige: "f5f5dc",
|
113
|
-
whitesmoke: "f5f5f5",
|
114
|
-
mintcream: "f5fffa",
|
115
|
-
ghostwhite: "f8f8ff",
|
116
|
-
salmon: "fa8072",
|
117
|
-
antiquewhite: "faebd7",
|
118
|
-
linen: "faf0e6",
|
119
|
-
lightgoldenrodyellow: "fafad2",
|
120
|
-
oldlace: "fdf5e6",
|
121
|
-
red: "ff0000",
|
122
|
-
fuchsia: "ff00ff",
|
123
|
-
magenta: "ff00ff",
|
124
|
-
deeppink: "ff1493",
|
125
|
-
orangered: "ff4500",
|
126
|
-
tomato: "ff6347",
|
127
|
-
hotpink: "ff69b4",
|
128
|
-
coral: "ff7f50",
|
129
|
-
darkorange: "ff8c00",
|
130
|
-
lightsalmon: "ffa07a",
|
131
|
-
orange: "ffa500",
|
132
|
-
lightpink: "ffb6c1",
|
133
|
-
pink: "ffc0cb",
|
134
|
-
gold: "ffd700",
|
135
|
-
peachpuff: "ffdab9",
|
136
|
-
navajowhite: "ffdead",
|
137
|
-
moccasin: "ffe4b5",
|
138
|
-
bisque: "ffe4c4",
|
139
|
-
mistyrose: "ffe4e1",
|
140
|
-
blanchedalmond: "ffebcd",
|
141
|
-
papayawhip: "ffefd5",
|
142
|
-
lavenderblush: "fff0f5",
|
143
|
-
seashell: "fff5ee",
|
144
|
-
cornsilk: "fff8dc",
|
145
|
-
lemonchiffon: "fffacd",
|
146
|
-
floralwhite: "fffaf0",
|
147
|
-
snow: "fffafa",
|
148
|
-
yellow: "ffff00",
|
149
|
-
lightyellow: "ffffe0",
|
150
|
-
ivory: "fffff0",
|
151
|
-
white: "ffffff" }
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|