colorant 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +89 -0
- data/Rakefile +10 -0
- data/Readme.md +57 -0
- data/bin/colorant +26 -0
- data/colorant.gemspec +28 -0
- data/features/cli.feature +30 -0
- data/features/support/env.rb +10 -0
- data/lib/colorant.rb +10 -0
- data/lib/colorant/parser.rb +39 -0
- data/lib/colorant/processer.rb +61 -0
- data/lib/colorant/reporter.rb +43 -0
- data/lib/colorant/version.rb +3 -0
- data/spec/parser_spec.rb +44 -0
- data/spec/processer_spec.rb +59 -0
- data/spec/reporter_spec.rb +92 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/support/arnolfini.jpg +0 -0
- data/specs.watchr +75 -0
- metadata +169 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm --create use ruby-1.9.2@colorist > /dev/null
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in colorist.gemspec
|
4
|
+
gemspec
|
5
|
+
# gem "color_namer", :git => 'git://github.com/retro/color-namer.git'
|
6
|
+
# gem "bundler"
|
7
|
+
# gem "rspec", :git => 'git://github.com/rspec/rspec.git'
|
8
|
+
# gem "rspec-expectations", :git => 'git://github.com/rspec/rspec-expectations.git'
|
9
|
+
# gem "rspec-mocks", :git => 'git://github.com/rspec/rspec-mocks.git'
|
10
|
+
# gem "rspec-core", :git => 'git://github.com/rspec/rspec-core.git'
|
11
|
+
#
|
12
|
+
# gem "aruba", :git => 'git://github.com/aslakhellesoy/aruba.git'
|
13
|
+
# gem "cucumber", :git => 'git://github.com/aslakhellesoy/cucumber.git'
|
14
|
+
# gem "watchr"
|
15
|
+
# gem "trollop"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
GIT
|
2
|
+
remote: git://github.com/aslakhellesoy/aruba.git
|
3
|
+
revision: 1cca0f2433b418e7e3a4a3d5671c2531bf5a7101
|
4
|
+
specs:
|
5
|
+
aruba (0.2.4)
|
6
|
+
background_process
|
7
|
+
cucumber (~> 0.9.3)
|
8
|
+
|
9
|
+
GIT
|
10
|
+
remote: git://github.com/aslakhellesoy/cucumber.git
|
11
|
+
revision: 19a87c14098afb7ed418b234b22a9e5b91eda5f4
|
12
|
+
specs:
|
13
|
+
cucumber (0.9.4)
|
14
|
+
builder (~> 2.1.2)
|
15
|
+
diff-lcs (~> 1.1.2)
|
16
|
+
gherkin (~> 2.2.9)
|
17
|
+
json (~> 1.4.6)
|
18
|
+
term-ansicolor (~> 1.0.5)
|
19
|
+
|
20
|
+
GIT
|
21
|
+
remote: git://github.com/retro/color-namer.git
|
22
|
+
revision: e5ba1bd142ecc4c22ece12784decd5e9ce1ba43e
|
23
|
+
specs:
|
24
|
+
color_namer (0.1.0)
|
25
|
+
color (>= 1.4.1)
|
26
|
+
|
27
|
+
GIT
|
28
|
+
remote: git://github.com/rspec/rspec-core.git
|
29
|
+
revision: 62402739a3e2f0e29ca025e60c600c1100f21424
|
30
|
+
specs:
|
31
|
+
rspec-core (2.2.pre)
|
32
|
+
|
33
|
+
GIT
|
34
|
+
remote: git://github.com/rspec/rspec-expectations.git
|
35
|
+
revision: 8729e9adbb2e1b50b8db2877afefd2e61ba18ad5
|
36
|
+
specs:
|
37
|
+
rspec-expectations (2.2.pre)
|
38
|
+
diff-lcs (~> 1.1.2)
|
39
|
+
|
40
|
+
GIT
|
41
|
+
remote: git://github.com/rspec/rspec-mocks.git
|
42
|
+
revision: ed672c6a2b943eac5b537d1f2c5beef71bb3aee9
|
43
|
+
specs:
|
44
|
+
rspec-mocks (2.2.pre)
|
45
|
+
|
46
|
+
GIT
|
47
|
+
remote: git://github.com/rspec/rspec.git
|
48
|
+
revision: 79447df6d32ec9e9eb3194bd1897293d824ca298
|
49
|
+
specs:
|
50
|
+
rspec (2.2.pre)
|
51
|
+
rspec-core (~> 2.2)
|
52
|
+
rspec-expectations (~> 2.2)
|
53
|
+
rspec-mocks (~> 2.2)
|
54
|
+
|
55
|
+
PATH
|
56
|
+
remote: .
|
57
|
+
specs:
|
58
|
+
colorist (0.0.1)
|
59
|
+
|
60
|
+
GEM
|
61
|
+
remote: http://rubygems.org/
|
62
|
+
specs:
|
63
|
+
background_process (1.2)
|
64
|
+
builder (2.1.2)
|
65
|
+
color (1.4.1)
|
66
|
+
diff-lcs (1.1.2)
|
67
|
+
gherkin (2.2.9)
|
68
|
+
json (~> 1.4.6)
|
69
|
+
term-ansicolor (~> 1.0.5)
|
70
|
+
json (1.4.6)
|
71
|
+
term-ansicolor (1.0.5)
|
72
|
+
trollop (1.16.2)
|
73
|
+
watchr (0.7)
|
74
|
+
|
75
|
+
PLATFORMS
|
76
|
+
ruby
|
77
|
+
|
78
|
+
DEPENDENCIES
|
79
|
+
aruba!
|
80
|
+
bundler
|
81
|
+
color_namer!
|
82
|
+
colorist!
|
83
|
+
cucumber!
|
84
|
+
rspec!
|
85
|
+
rspec-core!
|
86
|
+
rspec-expectations!
|
87
|
+
rspec-mocks!
|
88
|
+
trollop
|
89
|
+
watchr
|
data/Rakefile
ADDED
data/Readme.md
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
# colorant
|
2
|
+
|
3
|
+
Colorant is a small utility to determine which colors are present in a
|
4
|
+
particular image, and how much they appear. It wraps ImageMagick's `convert`
|
5
|
+
command, so it has to be installed on the system.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
Just put it in your Gemfile:
|
10
|
+
|
11
|
+
gem "colorant"
|
12
|
+
|
13
|
+
And wherever you want, you only need a filename, and you get an array of color
|
14
|
+
percentual frequencies of your image.
|
15
|
+
|
16
|
+
colorant.process "path/to/my/leaves.jpg"
|
17
|
+
# => [["Brown", 78.95], ["Green", 3.85], ...]
|
18
|
+
|
19
|
+
These are other options you can specify:
|
20
|
+
|
21
|
+
colorant.process "file", :colors => 5, # Default is 8
|
22
|
+
:depth => 24, # Default is 16
|
23
|
+
:extended => true # Use more accurate color names
|
24
|
+
|
25
|
+
# => [["Vanilla Brown", 78.95], ["Lime Green", 3.85], ...] (up to 5 colors)
|
26
|
+
|
27
|
+
## Command Line Usage
|
28
|
+
|
29
|
+
You can also use colorant from the command line. It is simple:
|
30
|
+
|
31
|
+
gem install colorant
|
32
|
+
colorant path/to/my/photo.jpg --colors 9 --extended
|
33
|
+
|
34
|
+
And the output:
|
35
|
+
|
36
|
+
Cocoa Brown -- 40.54 %
|
37
|
+
Moccaccino Red -- 17.88 %
|
38
|
+
Rock Brown -- 12.76 %
|
39
|
+
...
|
40
|
+
|
41
|
+
The options you can use are the same as the above explained, just with the
|
42
|
+
CLI-like format.
|
43
|
+
|
44
|
+
## Note on Patches/Pull Requests
|
45
|
+
|
46
|
+
* Fork the project.
|
47
|
+
* Make your feature addition or bug fix.
|
48
|
+
* Add specs for it. This is important so I don't break it in a
|
49
|
+
future version unintentionally.
|
50
|
+
* Commit, do not mess with rakefile, version, or history.
|
51
|
+
If you want to have your own version, that is fine but bump version
|
52
|
+
in a commit by itself I can ignore when I pull.
|
53
|
+
* Send me a pull request. Bonus points for topic branches.
|
54
|
+
|
55
|
+
## Copyright
|
56
|
+
|
57
|
+
Copyright (c) 2010 Codegram. See LICENSE for details.
|
data/bin/colorant
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
|
3
|
+
|
4
|
+
require 'trollop'
|
5
|
+
require 'colorant'
|
6
|
+
|
7
|
+
opts = Trollop::options do
|
8
|
+
version "colorant 0.1.0 (c) 2010 Codegram"
|
9
|
+
banner <<-EOS
|
10
|
+
Colorant is a simple utility to extract the colors present in an image.
|
11
|
+
|
12
|
+
Usage:
|
13
|
+
colorant <filename> [options]
|
14
|
+
where [options] are:
|
15
|
+
EOS
|
16
|
+
opt :colors, "Number of colors to process", :default => "8"
|
17
|
+
opt :depth, "Image depth in bits", :default => "16"
|
18
|
+
opt :extended, "Extended report (more accurate color names)"
|
19
|
+
end
|
20
|
+
Trollop::die :colors, "must be at least 1" if opts[:colors].to_i < 1
|
21
|
+
Trollop::die :depth, "must be at least 1" if opts[:depth].to_i < 1
|
22
|
+
|
23
|
+
opts.update(:reporter => :stdout) # Default for CLI
|
24
|
+
|
25
|
+
file = ARGV.shift
|
26
|
+
Colorant.process(file, opts)
|
data/colorant.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/colorant/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "colorant"
|
6
|
+
s.version = Colorant::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ['Josep M. Bach', 'Josep Jaume Rey', 'Oriol Gual']
|
9
|
+
s.email = ['info@codegram.com']
|
10
|
+
s.homepage = "http://github.com/codegram/colorant"
|
11
|
+
s.summary = "Easily extract the colors of any image file!"
|
12
|
+
s.description = "Easily extract the colors of any image file!"
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
15
|
+
s.rubyforge_project = "colorant"
|
16
|
+
|
17
|
+
s.add_development_dependency "bundler", ">= 1.0.3"
|
18
|
+
s.add_development_dependency 'rspec'
|
19
|
+
s.add_development_dependency 'cucumber'
|
20
|
+
s.add_development_dependency 'aruba'
|
21
|
+
|
22
|
+
s.add_runtime_dependency 'trollop'
|
23
|
+
s.add_runtime_dependency "color_namer"
|
24
|
+
|
25
|
+
s.files = `git ls-files`.split("\n")
|
26
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
27
|
+
s.require_path = 'lib'
|
28
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Feature: Command line interpreter
|
2
|
+
|
3
|
+
You can use Colorist from the command line as well.
|
4
|
+
|
5
|
+
Scenario: get the most relevant colors out of the Arnolfini Portrait
|
6
|
+
When I run "colorist arnolfini.jpg --colors 8 --depth 24"
|
7
|
+
|
8
|
+
Then the exit status should be 0
|
9
|
+
And the output should contain "Brown -- 40.54 %"
|
10
|
+
And the output should contain "Red -- 17.88 %"
|
11
|
+
And the output should contain "Brown -- 12.76 %"
|
12
|
+
And the output should contain "Red -- 9.95 %"
|
13
|
+
And the output should contain "Brown -- 8.03 %"
|
14
|
+
And the output should contain "Brown -- 4.25 %"
|
15
|
+
And the output should contain "Red -- 3.55 %"
|
16
|
+
And the output should contain "Brown -- 3.03 %"
|
17
|
+
|
18
|
+
Scenario: get an extended report over the Arnolfini Portrait
|
19
|
+
When I run "colorist arnolfini.jpg --extended"
|
20
|
+
|
21
|
+
Then the exit status should be 0
|
22
|
+
And the output should contain "Cocoa Brown -- 40.54 %"
|
23
|
+
And the output should contain "Moccaccino Red -- 17.88 %"
|
24
|
+
And the output should contain "Rock Brown -- 12.76 %"
|
25
|
+
And the output should contain "Mojo Red -- 9.95 %"
|
26
|
+
And the output should contain "Brown Derby -- 8.03 %"
|
27
|
+
And the output should contain "Dark Tan Brown -- 4.25 %"
|
28
|
+
And the output should contain "Brandy Rose Red -- 3.55 %"
|
29
|
+
And the output should contain "Vanilla Brown -- 3.03 %"
|
30
|
+
|
data/lib/colorant.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'colorant/parser'
|
2
|
+
require 'colorant/reporter'
|
3
|
+
require 'colorant/processer'
|
4
|
+
|
5
|
+
module Colorant
|
6
|
+
def self.process(*args)
|
7
|
+
raise "You must have `convert` (ImageMagick) installed." unless `which convert` =~ /convert/
|
8
|
+
Processer.process(*args)
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Colorant
|
2
|
+
class Parser
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def parse(*args)
|
6
|
+
new.parse(*args)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse(raw_data)
|
11
|
+
collection = []
|
12
|
+
raw_data.split("\n").map(&:strip).each do |line|
|
13
|
+
line.match /^(\d+):.*rgb\((.*)%,(.*)%,(.*)%\)$/
|
14
|
+
freq = $1.to_i
|
15
|
+
red, green, blue = [$2,$3,$4].map do |color|
|
16
|
+
color.to_f / 100.to_f
|
17
|
+
end
|
18
|
+
collection << { :freq => freq,
|
19
|
+
:red => percent_to_number(red),
|
20
|
+
:green => percent_to_number(green),
|
21
|
+
:blue => percent_to_number(blue) }
|
22
|
+
end
|
23
|
+
total = collection.map{|element| element[:freq]}.inject(:+)
|
24
|
+
collection.each{|element| element[:freq] = number_to_percent(element[:freq], total)}
|
25
|
+
collection
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def percent_to_number(percent)
|
31
|
+
("%.2f" % (255 * percent)).to_f
|
32
|
+
end
|
33
|
+
|
34
|
+
def number_to_percent(number, total)
|
35
|
+
("%.2f" % ((number * 100.to_f) / total)).to_f
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Colorant
|
2
|
+
class Processer
|
3
|
+
attr_reader :colors, :depth, :file, :data
|
4
|
+
|
5
|
+
def initialize(file, options = {})
|
6
|
+
begin
|
7
|
+
File.open(file)
|
8
|
+
rescue=>e
|
9
|
+
raise ArgumentError.new "#{file} does not exist or is not a valid image file"
|
10
|
+
end
|
11
|
+
@file = file
|
12
|
+
@colors = options[:colors] ? options[:colors].to_i : 8
|
13
|
+
@depth = options[:depth] ? options[:depth].to_i : 16
|
14
|
+
@reporter_options = {:reporter => options[:reporter],
|
15
|
+
:extended => options[:extended]}
|
16
|
+
@data = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def process!
|
20
|
+
raw_data = `convert #{file} -format %c -colors #{colors} -depth #{depth} histogram:info:- | sort -r -k 1`
|
21
|
+
@data = Parser.parse(raw_data)
|
22
|
+
Reporter.report(@data, @reporter_options)
|
23
|
+
end
|
24
|
+
|
25
|
+
class << self
|
26
|
+
def process(*args)
|
27
|
+
instance = new(*args)
|
28
|
+
instance.process!
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def parse(raw_data)
|
35
|
+
collection = []
|
36
|
+
raw_data.split("\n").map(&:strip).each do |line|
|
37
|
+
line.match /^(\d+):.*rgb\((.*)%,(.*)%,(.*)%\)$/
|
38
|
+
freq = $1.to_i
|
39
|
+
red, green, blue = [$2,$3,$4].map do |color|
|
40
|
+
color.to_f / 100.to_f
|
41
|
+
end
|
42
|
+
collection << { :freq => freq,
|
43
|
+
:red => percent_to_number(red),
|
44
|
+
:green => percent_to_number(green),
|
45
|
+
:blue => percent_to_number(blue) }
|
46
|
+
end
|
47
|
+
total = collection.map{|element| element[:freq]}.inject(:+)
|
48
|
+
collection.each{|element| element[:freq] = number_to_percent(element[:freq], total)}
|
49
|
+
collection
|
50
|
+
end
|
51
|
+
|
52
|
+
def percent_to_number(percent)
|
53
|
+
("%.2f" % (255 * percent)).to_f
|
54
|
+
end
|
55
|
+
|
56
|
+
def number_to_percent(number, total)
|
57
|
+
("%.2f" % ((number * 100.to_f) / total)).to_f
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'color_namer'
|
2
|
+
|
3
|
+
module Colorant
|
4
|
+
class Reporter
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def report(*args)
|
8
|
+
new.report(*args)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def report(data, options = {})
|
13
|
+
data.map! do |row|
|
14
|
+
color = ColorNamer.name_from_rgb(row[:red], row[:green], row[:blue])
|
15
|
+
if options[:extended]
|
16
|
+
name = color[1].match(color.last) ? color[1] : "#{color[1]} #{color.last}"
|
17
|
+
[name, row[:freq]]
|
18
|
+
else
|
19
|
+
[color.last, row[:freq]]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
send(:"#{options[:reporter] || :ruby}_report", data)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def ruby_report(array)
|
28
|
+
array
|
29
|
+
end
|
30
|
+
|
31
|
+
def stdout_report(array)
|
32
|
+
array.each do |line|
|
33
|
+
stdout << "\t#{line.first}\t\t-- #{line.last} %\n"
|
34
|
+
end
|
35
|
+
stdout << "\n"
|
36
|
+
end
|
37
|
+
|
38
|
+
def stdout
|
39
|
+
$stdout
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
data/spec/parser_spec.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Colorant
|
4
|
+
describe Parser do
|
5
|
+
|
6
|
+
subject { Parser.new }
|
7
|
+
|
8
|
+
describe "instance methods" do
|
9
|
+
|
10
|
+
let(:raw_data) { " 3970743: (16087,11029, 8895) #3ED72B1522BF rgb(24.5472%,16.8292%,13.5729%)\n 1649905: (42205,28716,22999) #A4DD702C59D7 rgb(64.4007%,43.8178%,35.0942%)\n\n" }
|
11
|
+
|
12
|
+
let(:arnolfini_data) do
|
13
|
+
[
|
14
|
+
{:freq => 70.65, :red => 62.60, :green => 42.91, :blue => 34.61},
|
15
|
+
{:freq => 29.35, :red => 164.22, :green => 111.74, :blue => 89.49}
|
16
|
+
]
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#parse" do
|
20
|
+
it 'gets the colors from the image' do
|
21
|
+
subject.parse(raw_data).should == arnolfini_data
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "class methods" do
|
28
|
+
|
29
|
+
describe "#parse" do
|
30
|
+
it 'initializes a new instance and calls parse' do
|
31
|
+
instance = double('parser')
|
32
|
+
data = double('data')
|
33
|
+
|
34
|
+
Parser.should_receive(:new).and_return instance
|
35
|
+
instance.should_receive(:parse).with(data)
|
36
|
+
|
37
|
+
Parser.parse(data)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Colorant
|
4
|
+
describe Processer do
|
5
|
+
|
6
|
+
subject { Processer.new('spec/support/arnolfini.jpg') }
|
7
|
+
|
8
|
+
it "sets the default colors to 8" do
|
9
|
+
subject.colors.should == 8
|
10
|
+
end
|
11
|
+
|
12
|
+
it "sets the default depth to 16" do
|
13
|
+
subject.depth.should == 16
|
14
|
+
end
|
15
|
+
|
16
|
+
it "raises an argument error if provided an invalid file" do
|
17
|
+
expect {
|
18
|
+
Processer.new('invalid_file.txt')
|
19
|
+
}.to raise_error(ArgumentError, "invalid_file.txt does not exist or is not a valid image file")
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "instance methods" do
|
23
|
+
|
24
|
+
subject { Processer.new('spec/support/arnolfini.jpg', :colors => 2) }
|
25
|
+
|
26
|
+
let(:arnolfini_data) do
|
27
|
+
[
|
28
|
+
{:freq => 70.65, :red => 62.60, :green => 42.91, :blue => 34.61},
|
29
|
+
{:freq => 29.35, :red => 164.22, :green => 111.74, :blue => 89.49}
|
30
|
+
]
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#process!" do
|
34
|
+
it 'calls first the Parser and then the Reporter' do
|
35
|
+
Parser.should_receive(:parse).and_return arnolfini_data
|
36
|
+
Reporter.should_receive(:report).with(arnolfini_data, :reporter => nil, :extended => nil).ordered
|
37
|
+
subject.process!
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "class methods" do
|
45
|
+
|
46
|
+
describe "#process" do
|
47
|
+
it 'initializes a new instance and calls process' do
|
48
|
+
instance = double('processer')
|
49
|
+
Processer.should_receive(:new).with('file', :colors => 2).and_return instance
|
50
|
+
instance.should_receive(:process!)
|
51
|
+
|
52
|
+
Processer.process('file', :colors => 2)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Colorant
|
4
|
+
describe Reporter do
|
5
|
+
|
6
|
+
describe "class methods" do
|
7
|
+
|
8
|
+
describe "#report" do
|
9
|
+
it 'initializes a new instance and calls report' do
|
10
|
+
instance = double('reporter')
|
11
|
+
Reporter.should_receive(:new).and_return instance
|
12
|
+
instance.should_receive(:report).with('data')
|
13
|
+
|
14
|
+
Reporter.report('data')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "instance methods" do
|
21
|
+
|
22
|
+
let(:arnolfini_data) do
|
23
|
+
[
|
24
|
+
{:freq => 70.65, :red => 62.60, :green => 42.91, :blue => 34.61},
|
25
|
+
{:freq => 29.35, :red => 164.22, :green => 111.74, :blue => 89.49}
|
26
|
+
]
|
27
|
+
end
|
28
|
+
|
29
|
+
subject { Reporter.new }
|
30
|
+
|
31
|
+
describe "#report" do
|
32
|
+
|
33
|
+
it 'outputs a simple ordered array with the image color composition' do
|
34
|
+
subject.report(arnolfini_data).should == [["Brown", 70.65],
|
35
|
+
["Brown", 29.35]]
|
36
|
+
end
|
37
|
+
|
38
|
+
context "when called with :extended => true" do
|
39
|
+
|
40
|
+
it 'outputs more accurate info about the color' do
|
41
|
+
subject.report(arnolfini_data, :extended => true).should == [["Bistre Brown", 70.65],
|
42
|
+
["Au Chico Brown", 29.35]]
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'removes duplication to keep the color name nice' do
|
46
|
+
ColorNamer.should_receive(:name_from_rgb).and_return(["594537", "Brown Derby", "Brown"],
|
47
|
+
["FFFF00", "Yellow", "Yellow"])
|
48
|
+
|
49
|
+
subject.report(arnolfini_data, :extended => true).should == [["Brown Derby", 70.65],
|
50
|
+
["Yellow", 29.35]]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "with :stdout reporter (default from CLI)" do
|
55
|
+
|
56
|
+
before do
|
57
|
+
@out = StringIO.new
|
58
|
+
subject.stub(:stdout).and_return @out
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'outputs a simple ordered array with the image color composition' do
|
62
|
+
subject.report(arnolfini_data, :reporter => :stdout)
|
63
|
+
@out.string.should include "\tBrown\t\t-- 70.65 %\n"
|
64
|
+
@out.string.should include "\tBrown\t\t-- 29.35 %\n"
|
65
|
+
end
|
66
|
+
|
67
|
+
context "when called with :extended => true" do
|
68
|
+
|
69
|
+
it 'outputs more accurate info about the color' do
|
70
|
+
subject.report(arnolfini_data, :extended => true, :reporter => :stdout)
|
71
|
+
@out.string.should include "\tBistre Brown\t\t-- 70.65 %\n"
|
72
|
+
@out.string.should include "\tAu Chico Brown\t\t-- 29.35 %\n"
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'removes duplication to keep the color name nice' do
|
76
|
+
ColorNamer.should_receive(:name_from_rgb).and_return(["594537", "Brown Derby", "Brown"],
|
77
|
+
["FFFF00", "Yellow", "Yellow"])
|
78
|
+
|
79
|
+
subject.report(arnolfini_data, :extended => true, :reporter => :stdout)
|
80
|
+
@out.string.should include "\tBrown Derby\t\t-- 70.65 %\n"
|
81
|
+
@out.string.should include "\tYellow\t\t-- 29.35 %\n"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
# require 'simplecov'
|
3
|
+
# SimpleCov.start do
|
4
|
+
# add_group "Lib", "lib"
|
5
|
+
# end
|
6
|
+
begin
|
7
|
+
Bundler.setup(:default, :development)
|
8
|
+
rescue Bundler::BundlerError => e
|
9
|
+
$stderr.puts e.message
|
10
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
11
|
+
exit e.status_code
|
12
|
+
end
|
13
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
14
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
15
|
+
|
16
|
+
require 'colorant'
|
17
|
+
require 'rspec'
|
18
|
+
|
19
|
+
# Requires supporting files with custom matchers and macros, etc,
|
20
|
+
# in ./support/ and its subdirectories.
|
21
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
Binary file
|
data/specs.watchr
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# adapted from http://github.com/rspec/rspec-rails/blob/master/specs.watchr
|
2
|
+
|
3
|
+
# Run me with:
|
4
|
+
#
|
5
|
+
# $ watchr specs.watchr
|
6
|
+
|
7
|
+
# --------------------------------------------------
|
8
|
+
# Convenience Methods
|
9
|
+
# --------------------------------------------------
|
10
|
+
def all_spec_files
|
11
|
+
Dir['spec/**/*_spec.rb']
|
12
|
+
end
|
13
|
+
|
14
|
+
def run_spec_matching(thing_to_match)
|
15
|
+
matches = all_spec_files.grep(/#{thing_to_match}/i)
|
16
|
+
if matches.empty?
|
17
|
+
puts "Sorry, thanks for playing, but there were no matches for #{thing_to_match}"
|
18
|
+
else
|
19
|
+
run matches.join(' ')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def run(files_to_run)
|
24
|
+
puts("Running: #{files_to_run}")
|
25
|
+
system("bundle exec rspec -c #{files_to_run}")
|
26
|
+
no_int_for_you
|
27
|
+
end
|
28
|
+
|
29
|
+
def run_feature(feature)
|
30
|
+
system("cucumber #{feature}")
|
31
|
+
end
|
32
|
+
|
33
|
+
def run_all_specs
|
34
|
+
run(all_spec_files.join(' '))
|
35
|
+
end
|
36
|
+
|
37
|
+
def run_all_features
|
38
|
+
system('clear')
|
39
|
+
system('cucumber')
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
# --------------------------------------------------
|
44
|
+
# Watchr Rules
|
45
|
+
# --------------------------------------------------
|
46
|
+
watch('^spec/(.*)_spec\.rb') { |m| run_spec_matching(m[1]) }
|
47
|
+
watch('^app/(.*)\.rb') { |m| run_spec_matching(m[1]) }
|
48
|
+
watch('^app/(.*)\.haml') { |m| run_spec_matching(m[1]) }
|
49
|
+
watch('^lib/(.*)\.rb') { |m| run_spec_matching(m[1]) }
|
50
|
+
watch('^spec/spec_helper\.rb') { run_all_specs }
|
51
|
+
watch('^spec/support/.*\.rb') { run_all_specs }
|
52
|
+
watch('^features/(.*)\.feature') { |m| run_feature(m) }
|
53
|
+
watch('^features/(.*)\.rb') { |m| run_feature('features') }
|
54
|
+
|
55
|
+
# --------------------------------------------------
|
56
|
+
# Signal Handling
|
57
|
+
# --------------------------------------------------
|
58
|
+
|
59
|
+
def no_int_for_you
|
60
|
+
@sent_an_int = nil
|
61
|
+
end
|
62
|
+
|
63
|
+
Signal.trap 'INT' do
|
64
|
+
if @sent_an_int then
|
65
|
+
puts " A second INT? Ok, I get the message. Shutting down now."
|
66
|
+
exit
|
67
|
+
else
|
68
|
+
puts " Did you just send me an INT? Ugh. I'll quit for real if you do it again."
|
69
|
+
@sent_an_int = true
|
70
|
+
Kernel.sleep 1.5
|
71
|
+
run_all_specs
|
72
|
+
run_all_features
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: colorant
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Josep M. Bach
|
13
|
+
- Josep Jaume Rey
|
14
|
+
- Oriol Gual
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2010-11-10 00:00:00 +01:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: bundler
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
- 3
|
34
|
+
version: 1.0.3
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: cucumber
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: aruba
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
type: :development
|
75
|
+
version_requirements: *id004
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: trollop
|
78
|
+
prerelease: false
|
79
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
type: :runtime
|
88
|
+
version_requirements: *id005
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: color_namer
|
91
|
+
prerelease: false
|
92
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
version: "0"
|
100
|
+
type: :runtime
|
101
|
+
version_requirements: *id006
|
102
|
+
description: Easily extract the colors of any image file!
|
103
|
+
email:
|
104
|
+
- info@codegram.com
|
105
|
+
executables:
|
106
|
+
- colorant
|
107
|
+
extensions: []
|
108
|
+
|
109
|
+
extra_rdoc_files: []
|
110
|
+
|
111
|
+
files:
|
112
|
+
- .gitignore
|
113
|
+
- .rspec
|
114
|
+
- .rvmrc
|
115
|
+
- Gemfile
|
116
|
+
- Gemfile.lock
|
117
|
+
- Rakefile
|
118
|
+
- Readme.md
|
119
|
+
- bin/colorant
|
120
|
+
- colorant.gemspec
|
121
|
+
- features/cli.feature
|
122
|
+
- features/support/env.rb
|
123
|
+
- lib/colorant.rb
|
124
|
+
- lib/colorant/parser.rb
|
125
|
+
- lib/colorant/processer.rb
|
126
|
+
- lib/colorant/reporter.rb
|
127
|
+
- lib/colorant/version.rb
|
128
|
+
- spec/parser_spec.rb
|
129
|
+
- spec/processer_spec.rb
|
130
|
+
- spec/reporter_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
- spec/support/arnolfini.jpg
|
133
|
+
- specs.watchr
|
134
|
+
has_rdoc: true
|
135
|
+
homepage: http://github.com/codegram/colorant
|
136
|
+
licenses: []
|
137
|
+
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
none: false
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
segments:
|
149
|
+
- 0
|
150
|
+
version: "0"
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
segments:
|
157
|
+
- 1
|
158
|
+
- 3
|
159
|
+
- 6
|
160
|
+
version: 1.3.6
|
161
|
+
requirements: []
|
162
|
+
|
163
|
+
rubyforge_project: colorant
|
164
|
+
rubygems_version: 1.3.7
|
165
|
+
signing_key:
|
166
|
+
specification_version: 3
|
167
|
+
summary: Easily extract the colors of any image file!
|
168
|
+
test_files: []
|
169
|
+
|