color_matcher 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +32 -0
- data/lib/color_matcher.rb +45 -0
- data/spec/color_matcher_spec.rb +32 -0
- metadata +64 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 11c17e7cf33220d83cfbf69c356cfa4efd80f3e1
|
|
4
|
+
data.tar.gz: 25f6955f0d58b576542c2012a84e5d2d10d2b994
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7a8307d61802d15299ad07b3af38f27c8cf1aac62c79f4c01502fb5161cd0e6c57d549ce254eb4ebadbd7ff1f580eabbb607a72a9b3f97e3d4b988f72d2466eb
|
|
7
|
+
data.tar.gz: 80c81affa292a4033b3dfcf9a1cc157278d64a4721abb075b243175191dd8824ffb72bfed572db75ffea109e841c5e9cf3ac125a96a1e656e4d567abf7f97215
|
data/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
ColorMatcher
|
|
2
|
+
======
|
|
3
|
+
|
|
4
|
+
[](https://travis-ci.org/chicisimo/color_matcher)
|
|
5
|
+
|
|
6
|
+
ColorMatcher it's a gem that given a color, returns the nearest one from a collection.
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
You should read the [tests](https://github.com/chicisimo/color_matcher/blob/master/spec/color_matcher_spec.rb) as they are the most updated documentation.
|
|
11
|
+
|
|
12
|
+
There's only one method:
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
ColorMatcher.closest_color("000000", ["000000", "ffffff"]) # => 000000
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## How do we use it at Chicisimo
|
|
19
|
+
|
|
20
|
+
We use the color matcher at Chicisimo to match our looks with the color picked using the slider in our discover section:
|
|
21
|
+
|
|
22
|
+
<p align="center">
|
|
23
|
+
<img src="doc/discover.png" width="200">
|
|
24
|
+
</p>
|
|
25
|
+
|
|
26
|
+
## Algorithm
|
|
27
|
+
|
|
28
|
+
We are using the Euclidean distance algorithm as described [here](http://www.compuphase.com/cmetric.htm).
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
ColorMatcher is open-sourced software licensed under the MIT license.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
module ColorMatcher
|
|
2
|
+
extend self
|
|
3
|
+
|
|
4
|
+
Match = Struct.new(:color, :proximity)
|
|
5
|
+
|
|
6
|
+
def closest_color(color, collection)
|
|
7
|
+
return if color.empty? || collection.empty?
|
|
8
|
+
|
|
9
|
+
matches = collection.map do |collection_color|
|
|
10
|
+
Match.new(collection_color, proximity_of(color, collection_color))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
matches.min_by(&:proximity).color
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def proximity_of(color, another_color)
|
|
17
|
+
color_red, color_green, color_blue = split_color(color)
|
|
18
|
+
another_color_red, another_color_green, another_color_blue = split_color(another_color)
|
|
19
|
+
|
|
20
|
+
color_red_mean = (color_red + another_color_red) / 2
|
|
21
|
+
color_red_gap = color_red - another_color_red
|
|
22
|
+
color_green_gap = color_green - another_color_green
|
|
23
|
+
color_blue_gap = color_blue - another_color_blue
|
|
24
|
+
|
|
25
|
+
euclidean_distance(color_red_mean, color_red_gap, color_green_gap, color_blue_gap)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def split_color(color)
|
|
29
|
+
red = (color[0] + color[1]).to_i(16)
|
|
30
|
+
green = (color[2] + color[3]).to_i(16)
|
|
31
|
+
blue = (color[4] + color[5]).to_i(16)
|
|
32
|
+
|
|
33
|
+
[red, green, blue]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def euclidean_distance(red_mean, red_gap, green_gap, blue_gap)
|
|
37
|
+
red_weight = ((512 + red_mean) * red_gap ** 2) >> 8
|
|
38
|
+
green_weight = 4 * green_gap ** 2
|
|
39
|
+
blue_weight = ((767 - red_mean) * blue_gap ** 2) >> 8
|
|
40
|
+
|
|
41
|
+
Math.sqrt(red_weight + green_weight + blue_weight);
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private_class_method :proximity_of, :split_color, :euclidean_distance
|
|
45
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require "color_matcher"
|
|
2
|
+
|
|
3
|
+
describe ColorMatcher do
|
|
4
|
+
it "matches an exact hex color" do
|
|
5
|
+
match = ColorMatcher.closest_color("000000", ["000000", "000001"])
|
|
6
|
+
expect(match).to eq("000000")
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "matches to the closest color" do
|
|
10
|
+
match = ColorMatcher.closest_color("fffff1", ["000001", "000002", "ffffff"])
|
|
11
|
+
expect(match).to eq("ffffff")
|
|
12
|
+
|
|
13
|
+
match = ColorMatcher.closest_color("000000", ["ffffff"])
|
|
14
|
+
expect(match).to eq("ffffff")
|
|
15
|
+
|
|
16
|
+
match = ColorMatcher.closest_color("fe0e00", ["33cc00", "fe0000"])
|
|
17
|
+
expect(match).to eq("fe0000")
|
|
18
|
+
|
|
19
|
+
match = ColorMatcher.closest_color("cdf200", ["33cc00", "867f86"])
|
|
20
|
+
expect(match).to eq("33cc00")
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "returns nil when an empty collection is given" do
|
|
24
|
+
match = ColorMatcher.closest_color("cdf200", [])
|
|
25
|
+
expect(match).to be_nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "returns nil when an empty color is given" do
|
|
29
|
+
match = ColorMatcher.closest_color("", ["000000", "ffffff"])
|
|
30
|
+
expect(match).to be_nil
|
|
31
|
+
end
|
|
32
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: color_matcher
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Pedro Gimenez
|
|
8
|
+
- Juan Guerrero
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2015-04-20 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rspec
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
requirements:
|
|
18
|
+
- - ">="
|
|
19
|
+
- !ruby/object:Gem::Version
|
|
20
|
+
version: '0'
|
|
21
|
+
type: :development
|
|
22
|
+
prerelease: false
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - ">="
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: '0'
|
|
28
|
+
description: Matches the nearest HEX color in a collection given a color
|
|
29
|
+
email:
|
|
30
|
+
- pedro@chicisimo.com
|
|
31
|
+
- juan@chicisimo.com
|
|
32
|
+
executables: []
|
|
33
|
+
extensions: []
|
|
34
|
+
extra_rdoc_files:
|
|
35
|
+
- README.md
|
|
36
|
+
files:
|
|
37
|
+
- lib/color_matcher.rb
|
|
38
|
+
- spec/color_matcher_spec.rb
|
|
39
|
+
- README.md
|
|
40
|
+
homepage: http://github.com/chicisimo/color_matcher
|
|
41
|
+
licenses:
|
|
42
|
+
- MIT
|
|
43
|
+
metadata: {}
|
|
44
|
+
post_install_message:
|
|
45
|
+
rdoc_options: []
|
|
46
|
+
require_paths:
|
|
47
|
+
- lib
|
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: 2.1.0
|
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: '0'
|
|
58
|
+
requirements: []
|
|
59
|
+
rubyforge_project:
|
|
60
|
+
rubygems_version: 2.0.14
|
|
61
|
+
signing_key:
|
|
62
|
+
specification_version: 4
|
|
63
|
+
summary: Matches the nearest HEX color in a collection given a color
|
|
64
|
+
test_files: []
|