colourdistance 0.9.11 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93f98c9462411cf0f285710f220832a91a7fe543
4
- data.tar.gz: 09995502796c051e61f29d328bedfade685d1cb5
3
+ metadata.gz: a975c48f2d6a1dce74d9f8a0eeeb8643c1a26f31
4
+ data.tar.gz: fdd1f5d5c9aca769fd809b47d9c28f39f978ab13
5
5
  SHA512:
6
- metadata.gz: 83a632c000502980fb249747d8f0b035f929936f9190a0c36502d8852b9b7649cebb85e1af2785a8a4df7d31dccee6bfca5d2217cdadb07987fbd0667cf6ef33
7
- data.tar.gz: 89ed50be64c3cee7eac78b9b87948ef363e67e665e2d7f282782ab4b98223987ddea8ef22b70afa4e1b5bac8d0d4d43c245d7b0b2ad5816a496b76e009e9aa90
6
+ metadata.gz: 3595ff36180543a429f72a9386e07ff13c67a0201e751d7eb25911f18282019fa0259bdc30b6988971b3014172313428c36975dc3394eb84339a75176514e80a
7
+ data.tar.gz: cc04bc88a28ba4dfb792039059ea89ab4092cd2c0a79ccc2c4ed3474b5a17cfc9e9007819dc9a8be4f44595ab52a62b4ff3994e72c4886aa80457c7dd9fd0910
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ["theseventhorange@gmail.com"]
12
12
 
13
13
  spec.summary = "Measures the difference (as a distance between colors in lab space) between colours. Takes rgb input. Will add support for xyz and lab around version 1.0."
14
- spec.description = "Currently only contains two options, ciede94 and ciede2000, both weighted to (roughly) a [0,1] scale (actually possible to get very slightly above 1). Will add more. Both are my own implementations, and ciede94 is probably wrong because of it. Thanks go out to Josh Clayton (https://robots.thoughtbot.com/get-your-c-on), for helping me to understand the structure I was attempting to acheive."
14
+ spec.description = "Currently only contains three options (ciede 76, ciede94, and ciede2000), all weighted to (roughly) a [0,1] scale (actually possible to get slightly above 1). Will add more. All are my own implementations (although ciede2000 is based on a several dozen implementations found during research), and they may be wrong because of it. Thanks go out to Josh Clayton (https://robots.thoughtbot.com/get-your-c-on), for helping me to understand the structure I was attempting to acheive."
15
15
  spec.license = "MIT"
16
16
 
17
17
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
@@ -7,91 +7,98 @@
7
7
 
8
8
  void Init_colourdistance() {
9
9
  VALUE rb_mColourdistance = rb_define_module("Colourdistance");
10
+ rb_define_singleton_method(rb_mColourdistance, "cie76", ciede2000, 2);
11
+ rb_define_singleton_method(rb_mColourdistance, "cie94", ciede2000, 2);
10
12
  rb_define_singleton_method(rb_mColourdistance, "ciede2000", ciede2000, 2);
11
13
  }
12
14
 
13
- static VALUE ciede2000(const VALUE self, VALUE color1, VALUE color2) {
15
+ static VALUE cie76(const VALUE self, VALUE color1, VALUE color2) {
14
16
  double pi = 3.1415927;
15
17
  double e = 2.7182818;
16
18
  double conversion = pi/180.0;
17
- double l1;
18
- double l2;
19
- double a1;
20
- double a2;
19
+ static double lab1[3];
20
+ static double lab2[3];
21
21
 
22
- double r1 = NUM2DBL(rb_hash_aref(color1, rb_str_intern(rb_str_new2("r"))))/255.0;
23
- double g1 = NUM2DBL(rb_hash_aref(color1, rb_str_intern(rb_str_new2("g"))))/255.0;
24
- double b1 = NUM2DBL(rb_hash_aref(color1, rb_str_intern(rb_str_new2("b"))))/255.0;
22
+ rgb_to_lab(color1, lab1);
25
23
 
26
- double r2 = NUM2DBL(rb_hash_aref(color2, rb_str_intern(rb_str_new2("r"))))/255.0;
27
- double g2 = NUM2DBL(rb_hash_aref(color2, rb_str_intern(rb_str_new2("g"))))/255.0;
28
- double b2 = NUM2DBL(rb_hash_aref(color2, rb_str_intern(rb_str_new2("b"))))/255.0;
24
+ double l1 = lab1[0];
25
+ double a1 = lab1[1];
26
+ double b1 = lab1[2];
29
27
 
30
- r1 = ((r1 <= 0.04045) ? r1 / 12.92 : pow(((r1 + 0.055) / 1.055),2.4));
31
- g1 = ((g1 <= 0.04045) ? g1 / 12.92 : pow(((g1 + 0.055) / 1.055),2.4));
32
- b1 = ((b1 <= 0.04045) ? b1 / 12.92 : pow(((b1 + 0.055) / 1.055),2.4));
28
+ rgb_to_lab(color2, lab2);
29
+ double l2 = lab2[0];
30
+ double a2 = lab2[1];
31
+ double b2 = lab2[2];
33
32
 
34
- r2 = ((r2 <= 0.04045) ? r2 / 12.92 : pow(((r2 + 0.055) / 1.055),2.4));
35
- g2 = ((g2 <= 0.04045) ? g2 / 12.92 : pow(((g2 + 0.055) / 1.055),2.4));
36
- b2 = ((b2 <= 0.04045) ? b2 / 12.92 : pow(((b2 + 0.055) / 1.055),2.4));
33
+ double ldiff = l2 - l1;
34
+ double adiff = a2 - a1;
35
+ double bdiff = b2 - b1;
37
36
 
38
- double x1 = 0.412453 * r1 + 0.357580 * g1 + 0.180423 * b1;
39
- double y1 = 0.212671 * r1 + 0.715160 * g1 + 0.072169 * b1;
40
- double z1 = 0.019334 * r1 + 0.119193 * g1 + 0.950227 * b1;
37
+ double val = sqrt(ldiff*ldiff+adiff*adiff+bdiff*bdiff)/100.0;
41
38
 
42
- double x2 = 0.412453 * r2 + 0.357580 * g2 + 0.180423 * b2;
43
- double y2 = 0.212671 * r2 + 0.715160 * g2 + 0.072169 * b2;
44
- double z2 = 0.019334 * r2 + 0.119193 * g2 + 0.950227 * b2;
39
+ return DBL2NUM(val);
40
+ }
45
41
 
46
- double fx;
47
- double fy;
48
- double fz;
42
+ static VALUE cie94(const VALUE self, VALUE color1, VALUE color2) {
43
+ double pi = 3.1415927;
44
+ double e = 2.7182818;
45
+ double conversion = pi/180.0;
46
+ double kl = 2.0;
47
+ double k1 = 0.048;
48
+ double k2 = 0.014;
49
49
 
50
- if(x1 <= 0.008856){
51
- fx = 7.787 * x1/0.95047 + 16.0/116.0;
52
- }else{
53
- fx = cbrt(x1/0.95047);
54
- }
50
+ static double lab1[3];
51
+ static double lab2[3];
55
52
 
56
- if(y1 <= 0.008856){
57
- l1 = 903.3 * y1;
58
- fy = (7.787 * y1 + 16.0/116.0);
59
- }else{
60
- l1 = 116.0 * cbrt(y1) - 16.0;
61
- fy = cbrt(y1);
62
- }
53
+ rgb_to_lab(color1, lab1);
63
54
 
64
- if(z1 <= 0.008856){
65
- fz = 7.787 * z1/1.08883 + 16.0/116.0;
66
- }else{
67
- fz = cbrt(z1/1.08883);
68
- }
55
+ double l1 = lab1[0];
56
+ double a1 = lab1[1];
57
+ double b1 = lab1[2];
69
58
 
70
- a1 = 500.0 * (fx-fy);
71
- b1 = 200.0 * (fy-fz);
59
+ rgb_to_lab(color2, lab2);
60
+ double l2 = lab2[0];
61
+ double a2 = lab2[1];
62
+ double b2 = lab2[2];
72
63
 
73
- if(x2 <= 0.008856){
74
- fx = (7.787 * x2/0.95047 + 16.0/116.0);
75
- }else{
76
- fx = cbrt(x2/0.95047);
77
- }
64
+ double ldiff = l1 - l2;
65
+ double adiff = a1 - a2;
66
+ double bdiff = b1 - b2;
78
67
 
79
- if(y2 <= 0.008856){
80
- l2 = 903.3 * y2;
81
- fy = (7.787 * y2 + 16.0/116.0);
82
- }else{
83
- l2 = 116.0 * cbrt(y2) - 16.0;
84
- fy = cbrt(y2);
85
- }
68
+ double c1 = sqrt(a1*a1+b1*b1);
69
+ double c2 = sqrt(a2*a2+b2*b2);
70
+ double deltac = c1 - c2;
86
71
 
87
- if(z2 <= 0.008856){
88
- fz = (7.787 * z2/1.08883 + 16.0/116.0);
89
- }else{
90
- fz = cbrt(z2/1.08883);
91
- }
72
+ double deltah = sqrt(adiff*adiff+bdiff*bdiff-deltac*deltac);
92
73
 
93
- a2 = 500.0 * (fx-fy);
94
- b2 = 200.0 * (fy-fz);
74
+ double sc = 1 + k1*c1;
75
+ double sh = 1 + k2*c1;
76
+
77
+ double e1 = ldiff/kl;
78
+ double e2 = deltac / (k1*sc);
79
+ double e3 = deltah / (k2*sh);
80
+ double val = sqrt(e1*e1+e2*e2+e3*e3)/100.0;
81
+
82
+ return DBL2NUM(val);
83
+ }
84
+
85
+ static VALUE ciede2000(const VALUE self, VALUE color1, VALUE color2) {
86
+ double pi = 3.1415927;
87
+ double e = 2.7182818;
88
+ double conversion = pi/180.0;
89
+ static double lab1[3];
90
+ static double lab2[3];
91
+
92
+ rgb_to_lab(color1, lab1);
93
+
94
+ double l1 = lab1[0];
95
+ double a1 = lab1[1];
96
+ double b1 = lab1[2];
97
+
98
+ rgb_to_lab(color2, lab2);
99
+ double l2 = lab2[0];
100
+ double a2 = lab2[1];
101
+ double b2 = lab2[2];
95
102
 
96
103
  double c1 = sqrt(a1*a1+b1*b1);
97
104
  double c2 = sqrt(a2*a2+b2*b2);
@@ -165,4 +172,47 @@ static VALUE ciede2000(const VALUE self, VALUE color1, VALUE color2) {
165
172
  double val = sqrt(ldiff*ldiff+adiff*adiff+bdiff*bdiff+rdiff)/100.0;
166
173
 
167
174
  return DBL2NUM(val);
175
+ }
176
+
177
+
178
+ void rgb_to_lab(VALUE color, double *lab) {
179
+
180
+ double r = NUM2DBL(rb_hash_aref(color, rb_str_intern(rb_str_new2("r"))))/255.0;
181
+ double g = NUM2DBL(rb_hash_aref(color, rb_str_intern(rb_str_new2("g"))))/255.0;
182
+ double b = NUM2DBL(rb_hash_aref(color, rb_str_intern(rb_str_new2("b"))))/255.0;
183
+
184
+ r = ((r <= 0.04045) ? r / 12.92 : pow(((r + 0.055) / 1.055),2.4));
185
+ g = ((g <= 0.04045) ? g / 12.92 : pow(((g + 0.055) / 1.055),2.4));
186
+ b = ((b <= 0.04045) ? b / 12.92 : pow(((b + 0.055) / 1.055),2.4));
187
+
188
+ double x = 0.412453 * r + 0.357580 * g + 0.180423 * b;
189
+ double y = 0.212671 * r + 0.715160 * g + 0.072169 * b;
190
+ double z = 0.019334 * r + 0.119193 * g + 0.950227 * b;
191
+
192
+ double fx;
193
+ double fy;
194
+ double fz;
195
+
196
+ if(x <= 0.008856){
197
+ fx = 7.787 * x/0.95047 + 16.0/116.0;
198
+ }else{
199
+ fx = cbrt(x/0.95047);
200
+ }
201
+
202
+ if(y <= 0.008856){
203
+ lab[0] = 903.3 * y;
204
+ fy = (7.787 * y + 16.0/116.0);
205
+ }else{
206
+ lab[0] = 116.0 * cbrt(y) - 16.0;
207
+ fy = cbrt(y);
208
+ }
209
+
210
+ if(z <= 0.008856){
211
+ fz = 7.787 * z/1.08883 + 16.0/116.0;
212
+ }else{
213
+ fz = cbrt(z/1.08883);
214
+ }
215
+
216
+ lab[1] = 500.0 * (fx-fy);
217
+ lab[2] = 200.0 * (fy-fz);
168
218
  }
@@ -1,6 +1,9 @@
1
1
  #ifndef __colourdistance_h__
2
2
  #define __colourdistance_h__
3
3
 
4
+ static VALUE ciee76(VALUE,VALUE,VALUE);
5
+ static VALUE cie94(VALUE,VALUE,VALUE);
4
6
  static VALUE ciede2000(VALUE,VALUE,VALUE);
7
+ void rgb_to_lab(VALUE, double *);
5
8
 
6
9
  #endif
@@ -1,3 +1,3 @@
1
1
  module Colourdistance
2
- VERSION = "0.9.11"
2
+ VERSION = "1.0.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colourdistance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.11
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Orange Seven
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-26 00:00:00.000000000 Z
11
+ date: 2015-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,9 +66,10 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: Currently only contains two options, ciede94 and ciede2000, both weighted
70
- to (roughly) a [0,1] scale (actually possible to get very slightly above 1). Will
71
- add more. Both are my own implementations, and ciede94 is probably wrong because
69
+ description: Currently only contains three options (ciede 76, ciede94, and ciede2000),
70
+ all weighted to (roughly) a [0,1] scale (actually possible to get slightly above
71
+ 1). Will add more. All are my own implementations (although ciede2000 is based on
72
+ a several dozen implementations found during research), and they may be wrong because
72
73
  of it. Thanks go out to Josh Clayton (https://robots.thoughtbot.com/get-your-c-on),
73
74
  for helping me to understand the structure I was attempting to acheive.
74
75
  email: