borealis 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ bin/stubs
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in borealis.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Josh Steiner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Borealis
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'borealis'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install borealis
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
data/borealis.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'borealis/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "borealis"
8
+ gem.version = Borealis::VERSION
9
+ gem.authors = ["Josh Steiner"]
10
+ gem.email = ["josh@jsteiner.me"]
11
+ gem.description = %q{Finds top colors in a given image.}
12
+ gem.summary = %q{Finds top colors in a given image.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep("spec")
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_development_dependency 'rspec'
20
+
21
+ gem.add_dependency 'cocaine'
22
+ end
data/lib/borealis.rb ADDED
@@ -0,0 +1,27 @@
1
+ require "cocaine"
2
+ require "borealis/color"
3
+ require "borealis/image_analyzer"
4
+ require "borealis/version"
5
+
6
+ class Borealis
7
+ attr_reader :colors
8
+
9
+ def initialize(file, analyzer = ImageAnalyzer)
10
+ analysis = analyzer.new(file).process
11
+ @colors = analysis.split("\n").map { |line| line_to_color(line) }
12
+ colors.sort!
13
+ end
14
+
15
+ def hexes
16
+ colors.map(&:hex)
17
+ end
18
+
19
+ private
20
+
21
+ def line_to_color(line)
22
+ line = line.scan(/\(([\s+\d+,]+)\).+([\#][0-9,A-F,a-f]{6})/).flatten
23
+ rgb = line.first.split(',').map { |value| value.to_i }
24
+ hex = line.last
25
+ Color.new(rgb, hex)
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ class Borealis
2
+ class Color
3
+ attr_reader :rgb, :hex
4
+
5
+ def initialize(rgb, hex)
6
+ @rgb = rgb
7
+ @hex = hex
8
+ end
9
+
10
+ def distance
11
+ @distance ||= Math.sqrt(rgb[0] ^ 2 + rgb[1] ^ 2 + rgb[2] ^ 2)
12
+ end
13
+
14
+ def <=>(other)
15
+ distance <=> other.distance
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ class Borealis
2
+ class ImageAnalyzer
3
+ def initialize(image)
4
+ @image = image
5
+ end
6
+
7
+ def process
8
+ line = Cocaine::CommandLine.new(histogram_command)
9
+ line.run.strip
10
+ end
11
+
12
+ private
13
+
14
+ def histogram_command
15
+ quantize = "convert #{@image} +dither -colors 10 gif:-"
16
+ histogramize = "convert gif:- -define histogram:unique-colors=true \
17
+ -format %c histogram:info:-"
18
+
19
+ "#{quantize} | #{histogramize}"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ class Borealis
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Borealis, '#colors' do
4
+ it 'returns the colors from smallest to largest distance' do
5
+ image = 'spec/fixtures/winter.jpg'
6
+ histogram = Borealis.new(image)
7
+ expect(histogram.colors).to eq histogram.colors.sort
8
+ end
9
+ end
10
+
11
+ describe Borealis, '#hexes' do
12
+ it 'returns the correct hexes for the data provided' do
13
+ image = 'spec/fixtures/winter.jpg'
14
+ histogram = Borealis.new(image, FakeAnalyzer)
15
+ expect(histogram.hexes).to eq %w(#2A5E9F #2C2D48)
16
+ end
17
+ end
18
+
19
+ class FakeAnalyzer
20
+ def initialize(file)
21
+ end
22
+
23
+ def process
24
+ <<-eos
25
+ 51076: ( 1, 2,3) #2A5E9F srgb(1,2,3)
26
+ 47529: ( 4, 5, 6) #2C2D48 srgb(4,5,6)
27
+ eos
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Borealis::Color, '#distance' do
4
+ it 'calculates the distance correctly' do
5
+ color = Borealis::Color.new([1, 2, 3], 'hex')
6
+ expect(color.distance).to eq Math.sqrt(1 ^ 2 + 2 ^ 2 + 3 ^ 2)
7
+ end
8
+ end
9
+
10
+ describe Borealis::Color, 'comparison' do
11
+ it 'compares colors according to distance' do
12
+ large_color = Borealis::Color.new([4, 5, 6], 'arbitrary')
13
+ small_color = Borealis::Color.new([2, 3, 4], 'arbitrary')
14
+ smallest_color = Borealis::Color.new([1, 2, 3], 'arbitrary')
15
+
16
+ colors = [smallest_color, large_color, small_color]
17
+
18
+ expect(colors.sort).to eq [smallest_color, small_color, large_color]
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Borealis::ImageAnalyzer, '#process' do
4
+ it 'returns data in the correct format' do
5
+ image = 'spec/fixtures/winter.jpg'
6
+ analysis = Borealis::ImageAnalyzer.new(image).process
7
+
8
+ expect(analysis).to match(/\(([\s+\d+,]+)\).+([\#][0-9,A-F,a-f]{6})/)
9
+ end
10
+
11
+ it 'returns 10 lines' do
12
+ image = 'spec/fixtures/winter.jpg'
13
+ analysis = Borealis::ImageAnalyzer.new(image).process
14
+
15
+ expect(analysis.lines.count).to eq 10
16
+ end
17
+
18
+ it 'raises Cocaine::ExitStatusError when the image is not present' do
19
+ image = 'spec/fixtures/nonexistent.jpg'
20
+
21
+ silence_stderr do
22
+ expect {
23
+ Borealis::ImageAnalyzer.new(image).process
24
+ }.to raise_error(Cocaine::ExitStatusError)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Borealis::VERSION do
4
+ it { should_not be_empty }
5
+ end
Binary file
@@ -0,0 +1,7 @@
1
+ require 'borealis'
2
+
3
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |file| require file }
4
+
5
+ RSpec.configure do |config|
6
+ config.order = 'random'
7
+ end
@@ -0,0 +1,8 @@
1
+ def silence_stderr
2
+ stderr = STDERR
3
+ STDERR.reopen '/dev/null'
4
+ STDERR.sync = true
5
+ yield
6
+ ensure
7
+ STDERR.reopen stderr
8
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: borealis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josh Steiner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: cocaine
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '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'
46
+ description: Finds top colors in a given image.
47
+ email:
48
+ - josh@jsteiner.me
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rspec
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - borealis.gemspec
60
+ - lib/borealis.rb
61
+ - lib/borealis/color.rb
62
+ - lib/borealis/image_analyzer.rb
63
+ - lib/borealis/version.rb
64
+ - spec/borealis/borealis_spec.rb
65
+ - spec/borealis/color_spec.rb
66
+ - spec/borealis/image_analyzer_spec.rb
67
+ - spec/borealis/version_spec.rb
68
+ - spec/fixtures/winter.jpg
69
+ - spec/spec_helper.rb
70
+ - spec/support/silence_stderr.rb
71
+ homepage: ''
72
+ licenses: []
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.23
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Finds top colors in a given image.
95
+ test_files: []