color-proximity 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 488d3bfd9f4b8ebadea3e5d8bc25436ded1df9a3
4
+ data.tar.gz: 64026bab9444f1ec94d354e752e0763660b75030
5
+ SHA512:
6
+ metadata.gz: 2b9ff497a438edd22b1e6fa0164bc3b62c88dd77b299d79be423873c96f655cc75b14206b5029bac605d27793a06b82a29c632456d821fd64f168ab1c3e587d3
7
+ data.tar.gz: ff97ac4face64b0b324768359a51618acbf544ffc4727b5e858e04f22be204de18dc635899765e9b6c920dec8a784407cf346cdc2ec486f8489c0f18ea598d54
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0
4
+ - 2.1
5
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in color-proximity.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Garen Torikian
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,39 @@
1
+ # ColorProximity
2
+
3
+ [![Build Status](https://travis-ci.org/gjtorikian/color-proximity.svg?branch=master)](https://travis-ci.org/gjtorikian/color-proximity)
4
+
5
+ Given a threshold and a collection of colors, you'll be able to determine how close a given color is to matching a list.
6
+
7
+ A [Euclidean distance algorithm](http://www.compuphase.com/cmetric.htm) is used to determine matches.
8
+
9
+ ## Usage
10
+
11
+ ``` ruby
12
+ require 'color-proximity'
13
+ cp = ColorProximity.new(0.5, ['000000'])
14
+ cp.within_threshold?('000000') # [false, ['000000']]
15
+
16
+ cp.collection = ['ffffff'])
17
+ cp.within_threshold?('000000') # [true]
18
+
19
+ cp.collection = ['000001', '000002', 'ffffff'])
20
+ cp.thresholds('000003') # [3.3166247903554, 1.4142135623730951, 762.3437544835007]
21
+ ```
22
+
23
+ A failure case returns an array with two elements: `false`, and the collection of colors that failed the threshold. Success cases return an array with one element: `true`. This is designed to make it easy to call `.first` on the result to validate it.
24
+
25
+ ## Installation
26
+
27
+ Add this line to your application's Gemfile:
28
+
29
+ ```ruby
30
+ gem 'color-proximity'
31
+ ```
32
+
33
+ And then execute:
34
+
35
+ $ bundle
36
+
37
+ Or install it yourself as:
38
+
39
+ $ gem install color-proximity
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => [:spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "color/proximity"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'color-proximity/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "color-proximity"
8
+ spec.version = ColorProximity::VERSION
9
+ spec.authors = ["Garen Torikian"]
10
+ spec.email = ["gjtorikian@gmail.com"]
11
+
12
+ spec.summary = %q{Match the threshold of a color against a collection of colors.}
13
+ spec.homepage = 'https://github.com/gjtorikian/color-proximity'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency 'rspec', '~> 3.1'
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,55 @@
1
+ require "color-proximity/version"
2
+
3
+ class ColorProximity
4
+
5
+ attr_accessor :threshold, :collection
6
+
7
+ def initialize(threshold, collection)
8
+ @threshold = threshold
9
+ @collection = collection
10
+ end
11
+
12
+ def within_threshold?(color)
13
+ @collection.each_with_object([true]) do |collection_color, arr|
14
+ proximity = proximity_of(color, collection_color)
15
+ next unless @threshold > proximity
16
+ arr[0] = false
17
+ arr[1] = [] if arr[1].nil?
18
+ arr[1] << collection_color
19
+ end
20
+ end
21
+
22
+ def thresholds(color)
23
+ @collection.map { |collection_color| proximity_of(color, collection_color) }
24
+ end
25
+
26
+ private
27
+
28
+ def proximity_of(color, another_color)
29
+ color_red, color_green, color_blue = split_color(color)
30
+ another_color_red, another_color_green, another_color_blue = split_color(another_color)
31
+
32
+ color_red_mean = (color_red + another_color_red) / 2
33
+ color_red_gap = color_red - another_color_red
34
+ color_green_gap = color_green - another_color_green
35
+ color_blue_gap = color_blue - another_color_blue
36
+
37
+ euclidean_distance(color_red_mean, color_red_gap, color_green_gap, color_blue_gap)
38
+ end
39
+
40
+ def split_color(color)
41
+ red = (color[0] + color[1]).to_i(16)
42
+ green = (color[2] + color[3]).to_i(16)
43
+ blue = (color[4] + color[5]).to_i(16)
44
+
45
+ [red, green, blue]
46
+ end
47
+
48
+ def euclidean_distance(red_mean, red_gap, green_gap, blue_gap)
49
+ red_weight = ((512 + red_mean) * red_gap**2) >> 8
50
+ green_weight = 4 * green_gap**2
51
+ blue_weight = ((767 - red_mean) * blue_gap**2) >> 8
52
+
53
+ Math.sqrt(red_weight + green_weight + blue_weight)
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ class ColorProximity
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: color-proximity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Garen Torikian
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - gjtorikian@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - color-proximity.gemspec
57
+ - lib/color-proximity.rb
58
+ - lib/color-proximity/version.rb
59
+ homepage: https://github.com/gjtorikian/color-proximity
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Match the threshold of a color against a collection of colors.
83
+ test_files: []