color_difference 0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e24d5427cc7cb41a9fb3c49d8662697ce4aba565
4
+ data.tar.gz: 3cde460cbc45e5b12cef909033008ebc222e38ba
5
+ SHA512:
6
+ metadata.gz: bcd6166a553a5689a13b2310d743823e23af49f07174a05a0dc50c0207ec2c181a33073f67858caeb2c4a374338c108f9762178864cb7fa28bfbb773d609512f
7
+ data.tar.gz: bf94ba5e64a9dc03cb89df4560c7d21b2d42beff2baa26936b07b8e296082dc588b10edd18280e813d92c6fcf3e9c2980cd77fa2d45ae74e588d6715dd6d23a2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2014 Mindaugas Mozūras
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ # Color Difference
2
+
3
+ [![Code Climate](https://codeclimate.com/github/mmozuras/color_difference.png)](https://codeclimate.com/github/mmozuras/color_difference)
4
+ [![Build Status](https://travis-ci.org/mmozuras/color_difference.png)](https://travis-ci.org/mmozuras/color_difference)
5
+ [![Gem Version](https://badge.fury.io/rb/color_difference.png)](http://badge.fury.io/rb/color_difference)
6
+ [![Dependency Status](https://gemnasium.com/mmozuras/color_difference)](https://gemnasium.com/mmozuras/color_difference)
7
+
8
+ Implementation of [CIEDE2000 color-difference formula](http://www.ece.rochester.edu/~gsharma/ciede2000/ciede2000noteCRNA.pdf), which, unlike, something simpler, like Euclidean distance, takes into account subjective visual perception.
@@ -0,0 +1,107 @@
1
+ require 'color_difference/color'
2
+
3
+ module ColorDifference
4
+ CIRCLE = 360
5
+ HALF_CIRCLE = 180
6
+
7
+ def self.cie2000(color1, color2)
8
+ color1 = ColorDifference::Color.new(color1[:r], color1[:g], color1[:b])
9
+ color2 = ColorDifference::Color.new(color2[:r], color2[:g], color2[:b])
10
+
11
+ color_avg = color_avg(color1, color2)
12
+ g = 0.5 * (1 - convert(color_avg))
13
+
14
+ a1 = (1.0 + g) * color1.a
15
+ a2 = (1.0 + g) * color2.a
16
+ c1 = Math.sqrt((a1**2) + (color1.b**2))
17
+ c2 = Math.sqrt((a2**2) + (color2.b**2))
18
+ c_avg = (c1 + c2) / 2.0
19
+
20
+ hue1 = hue(color1, a1)
21
+ hue2 = hue(color2, a2)
22
+ hue_avg = hue_avg(hue1, hue2)
23
+ hue_delta = hue_delta(hue1, hue2)
24
+
25
+ t = 1 - 0.17 * Math.cos(radians(hue_avg - 30)) +
26
+ 0.24 * Math.cos(radians(2 * hue_avg)) +
27
+ 0.32 * Math.cos(radians(3 * hue_avg + 6)) -
28
+ 0.2 * Math.cos(radians(4 * hue_avg - 63))
29
+
30
+ delta_l = color2.l - color1.l
31
+ delta_c = c2 - c1
32
+ hue_delta = 2 * Math.sqrt(c2 * c1) * Math.sin(radians(hue_delta) / 2.0)
33
+
34
+ l_avg = (color1.l + color2.l) / 2.0
35
+ s_l = 1 + ((0.015 * ((l_avg - 50)**2)) / Math.sqrt(20 + ((l_avg - 50)**2.0)))
36
+ s_c = 1 + 0.045 * c_avg
37
+ s_h = 1 + 0.015 * c_avg * t
38
+
39
+ delta_ro = 30 * Math.exp(-((((hue_avg - 275) / 25)**2.0)))
40
+ r_t = -2 * convert(c_avg) * Math.sin(2 * radians(delta_ro))
41
+
42
+ difference = Math.sqrt(((delta_l / (s_l))**2) +
43
+ ((delta_c / (s_c))**2) +
44
+ ((hue_delta / (s_h))**2) +
45
+ r_t * (delta_c / (s_c)) * (hue_delta / (s_h)))
46
+
47
+ scale(difference)
48
+ end
49
+
50
+ private
51
+
52
+ def self.scale(number)
53
+ if number <= 0
54
+ 0
55
+ elsif number >= 100
56
+ 1
57
+ else
58
+ number.to_f / 100
59
+ end
60
+ end
61
+
62
+ def self.radians(degrees)
63
+ degrees * Math::PI / HALF_CIRCLE
64
+ end
65
+
66
+ def self.degrees(radians)
67
+ radians * HALF_CIRCLE / Math::PI
68
+ end
69
+
70
+ def self.color_avg(color1, color2)
71
+ c1 = Math.sqrt((color1.a**2) + (color1.b**2))
72
+ c2 = Math.sqrt((color2.a**2) + (color2.b**2))
73
+ (c1 + c2) / 2.0
74
+ end
75
+
76
+ def self.hue(color, a)
77
+ hue = [color.b, a] == [0.0, 0.0] ? 0.0 : degrees(Math.atan2(color.b, a))
78
+ hue += CIRCLE if hue < 0
79
+ hue
80
+ end
81
+
82
+ def self.hue_avg(hue1, hue2)
83
+ diff = hue1 - hue2
84
+ sum = hue1 + hue2
85
+
86
+ if diff.abs > HALF_CIRCLE
87
+ (sum + CIRCLE) / 2.0
88
+ else
89
+ (sum) / 2.0
90
+ end
91
+ end
92
+
93
+ def self.hue_delta(hue1, hue2)
94
+ diff = hue2 - hue1
95
+ if diff.abs <= HALF_CIRCLE
96
+ diff
97
+ elsif diff.abs > HALF_CIRCLE && hue2 <= hue1
98
+ diff + CIRCLE
99
+ else
100
+ diff - CIRCLE
101
+ end
102
+ end
103
+
104
+ def self.convert(avg)
105
+ Math.sqrt((avg**7.0) / ((avg**7.0) + (25.0**7.0)))
106
+ end
107
+ end
@@ -0,0 +1,67 @@
1
+ module ColorDifference
2
+ class Color < Struct.new(:red, :green, :blue)
3
+ def l
4
+ @l ||= (116.0 * lab_y) - 16.0
5
+ end
6
+
7
+ def a
8
+ @a ||= 500.0 * (lab_x - lab_y)
9
+ end
10
+
11
+ def b
12
+ @b ||= 200.0 * (lab_y - lab_z)
13
+ end
14
+
15
+ def x
16
+ @x ||= 0.4124 * xyz_r + 0.3576 * xyz_g + 0.1805 * xyz_b
17
+ end
18
+
19
+ def y
20
+ @y ||= 0.2126 * xyz_r + 0.7152 * xyz_g + 0.0722 * xyz_b
21
+ end
22
+
23
+ def z
24
+ @z ||= 0.0193 * xyz_r + 0.1192 * xyz_g + 0.9505 * xyz_b
25
+ end
26
+
27
+ private
28
+
29
+ def lab_x
30
+ @lab_x ||= lab(x / 95.047)
31
+ end
32
+
33
+ def lab_y
34
+ @lab_y ||= lab(y / 100.000)
35
+ end
36
+
37
+ def lab_z
38
+ @lab_z ||= lab(z / 108.883)
39
+ end
40
+
41
+ def lab(light)
42
+ if light > 0.008856
43
+ light**(1.0 / 3)
44
+ else
45
+ (7.787 * light) + (16.0 / 116)
46
+ end
47
+ end
48
+
49
+ def xyz_r
50
+ @xyz_r ||= xyz(red)
51
+ end
52
+
53
+ def xyz_g
54
+ @xyz_g ||= xyz(green)
55
+ end
56
+
57
+ def xyz_b
58
+ @xyz_b ||= xyz(blue)
59
+ end
60
+
61
+ def xyz(light)
62
+ light = light / 255.0
63
+ result = light <= 0.04045 ? light / 12.92 : ((light + 0.055) / 1.055)**2.4
64
+ result * 100
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module ColorDifference
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: color_difference
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mindaugas Mozūras
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 10.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 10.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.14.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.14.0
41
+ description:
42
+ email: mindaugas.mozuras@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/color_difference/color.rb
48
+ - lib/color_difference/version.rb
49
+ - lib/color_difference.rb
50
+ - LICENSE
51
+ - README.md
52
+ homepage: http://github.org/mmozuras/color_difference
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.6
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.0.7
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Implementation of CIEDE2000 color-difference formula
76
+ test_files: []
77
+ has_rdoc: