Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Nice reporter -- oh hai thx

  • Loading branch information...
commit 5c7302aafb062ee9c0fb960af561f3735a17f28c 1 parent 120fe61
@txus txus authored
View
14 bin/colorist
@@ -1,3 +1,15 @@
#!/usr/bin/env ruby
+$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
+
+require 'trollop'
require 'colorist'
-require 'colorist/cli'
+
+opts = Trollop::options do
+ opt :file, "Image file to process"
+ opt :colors, "Number of colors to process", :default => "8"
+ opt :depth, "Image depth in bits", :default => "16"
+ opt :output, "Output reporter", :default => "stdout"
+end
+
+file = opts.delete(:file)
+Colorist.process(file, opts)
View
8 lib/colorist.rb
@@ -1,5 +1,9 @@
-require 'colorist/extractor'
+require 'colorist/parser'
+require 'colorist/reporter'
+require 'colorist/processer'
module Colorist
-
+ def process(*args)
+ Processor.process(*args)
+ end
end
View
0  lib/colorist/parser.rb
No changes.
View
25 lib/colorist/extractor.rb → lib/colorist/processer.rb
@@ -1,12 +1,5 @@
module Colorist
- class Extractor
-
- class << self
- def process(*args)
- new(*args).process!
- end
- end
-
+ class Processer
attr_reader :colors, :depth, :file, :data
def initialize(file, options = {})
@@ -18,16 +11,22 @@ def initialize(file, options = {})
@file = file
@colors = options[:colors] || 8
@depth = options[:depth] || 16
+ @reporter_options = {:reporter => options[:reporter],
+ :extended => options[:extended]}
@data = []
end
def process!
raw_data = `convert #{file} -format %c -colors #{colors} -depth #{depth} histogram:info:- | sort -r -k 1`
@data = parse(raw_data)
+ Reporter.report(@data, @reporter_options)
end
- def report
- raise "Must call #process first!" if @data.empty?
+ class << self
+ def process(*args)
+ instance = new(*args)
+ instance.process!
+ end
end
private
@@ -45,6 +44,8 @@ def parse(raw_data)
:green => percent_to_number(green),
:blue => percent_to_number(blue) }
end
+ total = collection.map{|element| element[:freq]}.inject(:+)
+ collection.each{|element| element[:freq] = number_to_percent(element[:freq], total)}
collection
end
@@ -52,5 +53,9 @@ def percent_to_number(percent)
("%.2f" % (255 * percent)).to_f
end
+ def number_to_percent(number, total)
+ ("%.2f" % ((number * 100.to_f) / total)).to_f
+ end
+
end
end
View
43 lib/colorist/reporter.rb
@@ -0,0 +1,43 @@
+require 'color_namer'
+
+module Colorist
+ class Reporter
+
+ class << self
+ def report(*args)
+ new.report(*args)
+ end
+ end
+
+ def report(data, options = {})
+ data.map! do |row|
+ color = ColorNamer.name_from_rgb(row[:red], row[:green], row[:blue])
+ if options[:extended]
+ name = color[1].match(color.last) ? color[1] : "#{color[1]} #{color.last}"
+ [name, row[:freq]]
+ else
+ [color.last, row[:freq]]
+ end
+ end
+ send(:"#{options[:reporter] || :ruby}_report", data)
+ end
+
+ private
+
+ def ruby_report(array)
+ array
+ end
+
+ def stdout_report(array)
+ array.each do |line|
+ stdout << "\t#{line.first}\t\t-- #{line.last} %\n"
+ end
+ stdout << "\n"
+ end
+
+ def stdout
+ $stdout
+ end
+
+ end
+end
View
43 spec/extractor_spec.rb → spec/processer_spec.rb
@@ -1,9 +1,9 @@
require 'spec_helper'
module Colorist
- describe Extractor do
+ describe Processer do
- subject { Extractor.new('spec/support/arnolfini.jpg') }
+ subject { Processer.new('spec/support/arnolfini.jpg') }
it "sets the default colors to 8" do
subject.colors.should == 8
@@ -15,47 +15,46 @@ module Colorist
it "raises an argument error if provided an invalid file" do
expect {
- Extractor.new('invalid_file.txt')
+ Processer.new('invalid_file.txt')
}.to raise_error(ArgumentError, "invalid_file.txt does not exist or is not a valid image file")
end
describe "instance methods" do
- subject { Extractor.new('spec/support/arnolfini.jpg', :colors => 2) }
-
+ subject { Processer.new('spec/support/arnolfini.jpg', :colors => 2) }
+
+ let(:arnolfini_data) do
+ [
+ {:freq => 70.65, :red => 62.60, :green => 42.91, :blue => 34.61},
+ {:freq => 29.35, :red => 164.22, :green => 111.74, :blue => 89.49}
+ ]
+ end
+
describe "#process!" do
it 'gets the colors from the image' do
+ Reporter.stub(:report)
subject.process!
- subject.data.should == [
- {:freq => 3970743, :red => 62.60, :green => 42.91, :blue => 34.61},
- {:freq => 1649905, :red => 164.22, :green => 111.74, :blue => 89.49}
- ]
+ subject.data.should == arnolfini_data
+ end
+ it 'calls Reporter.report with parsed data' do
+ Reporter.should_receive(:report).with(arnolfini_data, :reporter => nil, :extended => nil)
+ subject.process!
end
end
- describe "#report" do
- it 'raises an error if called without having processed any file' do
- expect {
- subject.report
- }.to raise_error("Must call #process first!")
- end
- it 'outputs a handy hash with all the information' do
- end
- end
-
end
describe "class methods" do
describe "#process" do
it 'initializes a new instance and calls process' do
- instance = double('extractor')
- Extractor.should_receive(:new).with('file', :colors => 2).and_return instance
+ instance = double('processer')
+ Processer.should_receive(:new).with('file', :colors => 2).and_return instance
instance.should_receive(:process!)
- Extractor.process('file', :colors => 2)
+ Processer.process('file', :colors => 2)
end
end
View
92 spec/reporter_spec.rb
@@ -0,0 +1,92 @@
+require 'spec_helper'
+
+module Colorist
+ describe Reporter do
+
+ describe "class methods" do
+
+ describe "#report" do
+ it 'initializes a new instance and calls report' do
+ instance = double('reporter')
+ Reporter.should_receive(:new).and_return instance
+ instance.should_receive(:report).with('data')
+
+ Reporter.report('data')
+ end
+ end
+
+ end
+
+ describe "instance methods" do
+
+ let(:arnolfini_data) do
+ [
+ {:freq => 70.65, :red => 62.60, :green => 42.91, :blue => 34.61},
+ {:freq => 29.35, :red => 164.22, :green => 111.74, :blue => 89.49}
+ ]
+ end
+
+ subject { Reporter.new }
+
+ describe "#report" do
+
+ it 'outputs a simple ordered array with the image color composition' do
+ subject.report(arnolfini_data).should == [["Brown", 70.65],
+ ["Brown", 29.35]]
+ end
+
+ context "when called with :extended => true" do
+
+ it 'outputs more accurate info about the color' do
+ subject.report(arnolfini_data, :extended => true).should == [["Bistre Brown", 70.65],
+ ["Au Chico Brown", 29.35]]
+ end
+
+ it 'removes duplication to keep the color name nice' do
+ ColorNamer.should_receive(:name_from_rgb).and_return(["594537", "Brown Derby", "Brown"],
+ ["FFFF00", "Yellow", "Yellow"])
+
+ subject.report(arnolfini_data, :extended => true).should == [["Brown Derby", 70.65],
+ ["Yellow", 29.35]]
+ end
+ end
+
+ context "with :stdout reporter (default from CLI)" do
+
+ before do
+ @out = StringIO.new
+ subject.stub(:stdout).and_return @out
+ end
+
+ it 'outputs a simple ordered array with the image color composition' do
+ subject.report(arnolfini_data, :reporter => :stdout)
+ @out.string.should include "\tBrown\t\t-- 70.65 %\n"
+ @out.string.should include "\tBrown\t\t-- 29.35 %\n"
+ end
+
+ context "when called with :extended => true" do
+
+ it 'outputs more accurate info about the color' do
+ subject.report(arnolfini_data, :extended => true, :reporter => :stdout)
+ @out.string.should include "\tBistre Brown\t\t-- 70.65 %\n"
+ @out.string.should include "\tAu Chico Brown\t\t-- 29.35 %\n"
+ end
+
+ it 'removes duplication to keep the color name nice' do
+ ColorNamer.should_receive(:name_from_rgb).and_return(["594537", "Brown Derby", "Brown"],
+ ["FFFF00", "Yellow", "Yellow"])
+
+ subject.report(arnolfini_data, :extended => true, :reporter => :stdout)
+ @out.string.should include "\tBrown Derby\t\t-- 70.65 %\n"
+ @out.string.should include "\tYellow\t\t-- 29.35 %\n"
+ end
+ end
+
+ end
+
+ end
+
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.