color-rgb 0.0.2
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/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +86 -0
- data/Rakefile +2 -0
- data/color-rgb.gemspec +25 -0
- data/lib/color-rgb.rb +1 -0
- data/lib/color.rb +10 -0
- data/lib/color/cie_lab.rb +66 -0
- data/lib/color/comparison.rb +31 -0
- data/lib/color/rgb.rb +63 -0
- data/lib/color/version.rb +3 -0
- data/lib/color/xyz.rb +54 -0
- data/spec/color/cie_lab_spec.rb +48 -0
- data/spec/color/comparison_spec.rb +50 -0
- data/spec/color/rgb_spec.rb +96 -0
- data/spec/color/xyz_spec.rb +46 -0
- data/spec/spec_helper.rb +9 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 445d364c50a40c003797bb41d7e02607f7d26686
|
4
|
+
data.tar.gz: 1fac8c6e57e1f9aaaedbee116d5d70528f32f1aa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bc64036e91f60eacb2527b212cfa0bc749c1ad4791f26141422acd9dae87dd427ef1136cc77b175d6d688694adcdf5cfaf41f45fd990b49fe72aede5b42b8acc
|
7
|
+
data.tar.gz: 01aeda0ccac119fed135fc5f6e97e38016aec0bce58a8f2ca16d4abf59a1c59a1ed8387b161fbf62b3ebd411b75fc3ceb6024c07fa89535952bffd1db5745067
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Apartment Therapy LLC
|
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,86 @@
|
|
1
|
+
# Colorkit
|
2
|
+
|
3
|
+
A set of utilities to help with dealing with RGB colors. Supporting
|
4
|
+
|
5
|
+
- colorspace conversion from RGB to XYZ or CIE l*a*b*
|
6
|
+
- determining distance between two colors using CIE76
|
7
|
+
- storing RGB colors as integers
|
8
|
+
|
9
|
+
http://en.wikipedia.org/wiki/CIE_1931_color_space
|
10
|
+
http://en.wikipedia.org/wiki/Color_difference#CIE76
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'color-rgb'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install color
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
To instantiate an RGB color
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
Color::RGB.new(r, g, b) # r, g, b arguments
|
34
|
+
Color::RGB.from_array([r, g, b]) # an array of RGB values
|
35
|
+
Color::RGB.from_int(rgb_int) # int
|
36
|
+
```
|
37
|
+
|
38
|
+
To convert to XYZ or CIE L*a*b*
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
color_rgb = Color::RGB.new(r, g, b)
|
42
|
+
color_rgb.to_xyz
|
43
|
+
color_rgb.to_lab
|
44
|
+
```
|
45
|
+
|
46
|
+
Or to convert to an int for storage
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
color_rgb = Color::RGB.new(r, g, b)
|
50
|
+
color_rgb.to_int
|
51
|
+
```
|
52
|
+
|
53
|
+
Or to go from an Color::RGB instance back to an array of r,g,b values
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
color_rgb.to_a
|
57
|
+
```
|
58
|
+
|
59
|
+
To determine distance / similarity between two RGB colors
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
rgb_one = Color::RGB.new(r, g, b)
|
63
|
+
rgb_two = Color::RGB.new(other_r, other_g, other_b)
|
64
|
+
|
65
|
+
Color::Comparison.distance(rgb_one, rgb_two)
|
66
|
+
```
|
67
|
+
|
68
|
+
If you are planning to compare rgb_one against a number of colors in sequence
|
69
|
+
tou can instantiate a comparitor object and call compare with any other RGB
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
rgb_one = Color::RGB.new(r, g, b)
|
73
|
+
|
74
|
+
comparitor = Color::Comparison.new(rgb_one)
|
75
|
+
comparitor.compare(some_other_rgb_color) # Color::RGB isntance
|
76
|
+
comparitor.compare([r, g, b]) # array of RGB values
|
77
|
+
|
78
|
+
```
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
1. Fork it ( https://github.com/apartmenttherapy/color-rgb/fork )
|
83
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
84
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
85
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
86
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/color-rgb.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'color/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "color-rgb"
|
8
|
+
spec.version = Color::VERSION
|
9
|
+
spec.authors = ["ramin keene"]
|
10
|
+
spec.email = ["raminkeene@gmail.com"]
|
11
|
+
spec.summary = %q{RGB color utlities conversion and color distance}
|
12
|
+
spec.description = %q{RGB color utilities for converting RGB color into different colorspace (CIE*Lab or XYZ), a different format, or comparing similarity of two colors}
|
13
|
+
spec.homepage = "http://github.com/apartmenttherapy/color-rgb"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "codeclimate-test-reporter"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
data/lib/color-rgb.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'color'
|
data/lib/color.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module Color
|
3
|
+
class CIELab
|
4
|
+
attr_reader :x, :y, :z
|
5
|
+
|
6
|
+
def self.from_xyz(xyz_color)
|
7
|
+
self.new(xyz_color)
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(color)
|
11
|
+
@x = color.x
|
12
|
+
@y = color.y
|
13
|
+
@z = color.z
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_a
|
17
|
+
[l, a, b]
|
18
|
+
end
|
19
|
+
|
20
|
+
def reference_x
|
21
|
+
95.047 # Observer= 2°, Illuminant= D65
|
22
|
+
end
|
23
|
+
|
24
|
+
def reference_y
|
25
|
+
100.00
|
26
|
+
end
|
27
|
+
|
28
|
+
def reference_z
|
29
|
+
108.883
|
30
|
+
end
|
31
|
+
|
32
|
+
def var_x
|
33
|
+
@var_x ||= calculate_var(x / reference_x)
|
34
|
+
end
|
35
|
+
|
36
|
+
def var_y
|
37
|
+
@var_y ||= calculate_var(y / reference_y)
|
38
|
+
end
|
39
|
+
|
40
|
+
def var_z
|
41
|
+
@var_z ||= calculate_var(z / reference_z)
|
42
|
+
end
|
43
|
+
|
44
|
+
def l
|
45
|
+
( 116.0 * var_y) - 16.0
|
46
|
+
end
|
47
|
+
|
48
|
+
def a
|
49
|
+
500.0 * ( var_x - var_y)
|
50
|
+
end
|
51
|
+
|
52
|
+
def b
|
53
|
+
200.0 * ( var_y - var_z)
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def calculate_var(value)
|
59
|
+
if ( value > 0.008856)
|
60
|
+
value ** ( 1.0/3.0)
|
61
|
+
else
|
62
|
+
( 7.787 * value) + ( 16.0 / 116.0)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Color
|
2
|
+
class Comparison
|
3
|
+
attr_reader :rgb_color
|
4
|
+
|
5
|
+
def self.distance(rgb, rgb_match)
|
6
|
+
color_comparitor = self.new(rgb)
|
7
|
+
color_comparitor.compare(rgb_match)
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(rgb_color)
|
11
|
+
@rgb_color = rgb_color.is_a?(Array) ? to_rgb_instance(rgb_color) : rgb_color
|
12
|
+
end
|
13
|
+
|
14
|
+
def compare(color)
|
15
|
+
color = color.is_a?(Array) ? to_rgb_instance(color) : color
|
16
|
+
CIE76(rgb_color, color)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def CIE76(subject, match)
|
22
|
+
subject_lab = subject.to_lab
|
23
|
+
match_lab = match.to_lab
|
24
|
+
Math.sqrt((subject_lab[0] - match_lab[0])**2 + (subject_lab[1] - match_lab[1])**2 + (subject_lab[2] - match_lab[2])**2)
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_rgb_instance(rgb_array)
|
28
|
+
RGB.from_array(rgb_array)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/color/rgb.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module Color
|
3
|
+
class RGB
|
4
|
+
attr_reader :r, :g, :b
|
5
|
+
|
6
|
+
def self.from_array(rgb_array)
|
7
|
+
if rgb_array.length != 3
|
8
|
+
raise RGBArraySizeError.new("RGB array should contain 3 values")
|
9
|
+
end
|
10
|
+
self.new(rgb_array[0], rgb_array[1], rgb_array[2])
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.from_int(rgb_int)
|
14
|
+
red = (rgb_int >> 16) & 0xFF;
|
15
|
+
green = (rgb_int >> 8) & 0xFF;
|
16
|
+
blue = rgb_int & 0xFF;
|
17
|
+
self.new(red, green, blue)
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(r, g, b)
|
21
|
+
@r = r.to_f
|
22
|
+
@g = g.to_f
|
23
|
+
@b = b.to_f
|
24
|
+
|
25
|
+
validate_values
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_lab
|
29
|
+
xyz_color = Color::XYZ.from_rgb(self)
|
30
|
+
Color::CIELab.from_xyz(xyz_color).to_a
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_xyz
|
34
|
+
Color::XYZ.from_rgb(self).to_a
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_int
|
38
|
+
rgb_int = r.to_i
|
39
|
+
rgb_int = (rgb_int << 8) + g.to_i
|
40
|
+
rgb_int = (rgb_int << 8) + b.to_i
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_a
|
44
|
+
[r.to_i, g.to_i, b.to_i]
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def validate_values
|
50
|
+
if r > 255
|
51
|
+
raise Color::RGBArrayValueOutOfBoundsError.new("R is #{r}")
|
52
|
+
end
|
53
|
+
|
54
|
+
if g > 255
|
55
|
+
raise Color::RGBArrayValueOutOfBoundsError.new("G is #{g}")
|
56
|
+
end
|
57
|
+
|
58
|
+
if b > 255
|
59
|
+
raise Color::RGBArrayValueOutOfBoundsError.new("B is #{b}")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/color/xyz.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module Color
|
3
|
+
class XYZ
|
4
|
+
attr_reader :r, :g, :b
|
5
|
+
|
6
|
+
def self.from_rgb(rgb_color)
|
7
|
+
self.new(rgb_color)
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(color)
|
11
|
+
@r = color.r.to_f
|
12
|
+
@g = color.g.to_f
|
13
|
+
@b = color.b.to_f
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_a
|
17
|
+
[x, y, z]
|
18
|
+
end
|
19
|
+
|
20
|
+
def x
|
21
|
+
@x ||= ( var_r * 0.4124 + var_g * 0.3576 + var_b * 0.1805 )
|
22
|
+
end
|
23
|
+
|
24
|
+
def y
|
25
|
+
@y ||= ( var_r * 0.2126 + var_g * 0.7152 + var_b * 0.0722)
|
26
|
+
end
|
27
|
+
|
28
|
+
def z
|
29
|
+
@z ||= ( var_r * 0.0193 + var_g * 0.1192 + var_b * 0.9505)
|
30
|
+
end
|
31
|
+
|
32
|
+
def var_r
|
33
|
+
@var_r ||= convert(:r)
|
34
|
+
end
|
35
|
+
|
36
|
+
def var_g
|
37
|
+
@var_g ||= convert(:g)
|
38
|
+
end
|
39
|
+
|
40
|
+
def var_b
|
41
|
+
@var_b ||= convert(:b)
|
42
|
+
end
|
43
|
+
|
44
|
+
def convert(value)
|
45
|
+
variable = (send(value) / 255.0)
|
46
|
+
if ( variable > 0.04045)
|
47
|
+
variable = ( ( variable + 0.055) / 1.055) ** 2.4
|
48
|
+
else
|
49
|
+
variable = variable / 12.92
|
50
|
+
end
|
51
|
+
variable * 100
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Color::CIELab do
|
4
|
+
let(:rgb) { Color::RGB.new(255, 192, 203) }
|
5
|
+
let(:xyz) { Color::XYZ.new(rgb) }
|
6
|
+
|
7
|
+
subject{ Color::CIELab.new(xyz) }
|
8
|
+
|
9
|
+
describe '.from_xyz' do
|
10
|
+
it 'returns an instance of Color::CIELab' do
|
11
|
+
expect(Color::CIELab.from_xyz(xyz)).to be_an_instance_of(Color::CIELab)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#initialize' do
|
16
|
+
it 'takes a Color::XYZ object and initiates x, y, z attributes' do
|
17
|
+
expect(subject.instance_variable_get(:@x)).to eq(xyz.x)
|
18
|
+
expect(subject.instance_variable_get(:@y)).to eq(xyz.y)
|
19
|
+
expect(subject.instance_variable_get(:@z)).to eq(xyz.z)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'l,a,b' do
|
24
|
+
describe '#b' do
|
25
|
+
it 'calculates l' do
|
26
|
+
expect(subject.l).to eq(83.58479885775868)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#a' do
|
31
|
+
it 'calculates a' do
|
32
|
+
expect(subject.a).to eq(24.14966101257138)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#b' do
|
37
|
+
it 'calculates b' do
|
38
|
+
expect(subject.b).to eq(3.315387151150806)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#to_a' do
|
44
|
+
it 'returns an array of l,a,b values' do
|
45
|
+
expect(subject.to_a).to eq([83.58479885775868, 24.14966101257138, 3.315387151150806])
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Color::Comparison do
|
4
|
+
let(:rgb) { [12, 145, 44] }
|
5
|
+
let(:match) { [13, 32, 55] }
|
6
|
+
let(:instance) { Color::Comparison.new(rgb) }
|
7
|
+
let(:rgb_instance) { Color::RGB.from_array(rgb) }
|
8
|
+
|
9
|
+
describe '#compare' do
|
10
|
+
subject{ Color::Comparison.new(rgb_instance) }
|
11
|
+
|
12
|
+
it 'CIE76 distance should be 90.7247 ' do
|
13
|
+
expect(subject.compare(Color::RGB.from_array(match)).round(4)).to eq(90.7247)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '.distance' do
|
18
|
+
it 'converts rgb_color array to RGB' do
|
19
|
+
comparitor = Color::Comparison.new(rgb)
|
20
|
+
expect(comparitor.instance_variable_get(:@rgb_color)).to be_an_instance_of(Color::RGB)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'converts match color array to RGB' do
|
24
|
+
expect(instance).to receive(:to_rgb_instance)
|
25
|
+
.with(match)
|
26
|
+
.and_return(Color::RGB.from_array(match))
|
27
|
+
|
28
|
+
expect(Color::Comparison).to receive(:new)
|
29
|
+
.with(rgb)
|
30
|
+
.and_return(instance)
|
31
|
+
|
32
|
+
Color::Comparison.distance(rgb, match)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'accepts a Color::RGB' do
|
36
|
+
comparitor = Color::Comparison.new(rgb_instance)
|
37
|
+
expect(comparitor.instance_variable_get(:@rgb_color)).to be(rgb_instance)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'accepts an RGB instance and does not convert' do
|
41
|
+
expect(instance).to_not receive(:to_rgb_instance)
|
42
|
+
|
43
|
+
expect(Color::Comparison).to receive(:new)
|
44
|
+
.with(rgb_instance)
|
45
|
+
.and_return(instance)
|
46
|
+
|
47
|
+
Color::Comparison.distance(rgb_instance, Color::RGB.from_array(match))
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Color::RGB do
|
4
|
+
describe '#inititalize' do
|
5
|
+
it 'takes R, G, B as args' do
|
6
|
+
color = Color::RGB.new(12, 13, 14)
|
7
|
+
expect(color.r).to eq(12.0)
|
8
|
+
expect(color.g).to eq(13.0)
|
9
|
+
expect(color.b).to eq(14.0)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'raises an error if R, G, B args are greater than 255' do
|
13
|
+
expect{
|
14
|
+
Color::RGB.new(12, 13, 355)
|
15
|
+
}.to raise_error(Color::RGBArrayValueOutOfBoundsError)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'packing to integers' do
|
20
|
+
let(:rgb) { [255, 20, 147] }
|
21
|
+
let(:color) { Color::RGB.from_array(rgb) }
|
22
|
+
let(:packed) { 16716947 }
|
23
|
+
|
24
|
+
describe '.to_int' do
|
25
|
+
it 'translates ' do
|
26
|
+
expect(color.to_int).to eq packed
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '.from_int' do
|
31
|
+
it 'packs rgb values into an int for db storage' do
|
32
|
+
expect(Color::RGB.from_int(packed).to_a).to eq(rgb)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#to_a' do
|
38
|
+
it 'returns an array of integers representing RGB' do
|
39
|
+
expect(Color::RGB.new(23, 56, 77).to_a).to eq([23, 56, 77])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '.from_array' do
|
44
|
+
it 'builds an RGB object' do
|
45
|
+
color = Color::RGB.from_array([12, 13, 14])
|
46
|
+
expect(color).to be_an_instance_of(Color::RGB)
|
47
|
+
expect(color.r).to eq(12.0)
|
48
|
+
expect(color.g).to eq(13.0)
|
49
|
+
expect(color.b).to eq(14.0)
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'errors' do
|
53
|
+
it 'will raise an exception if an array is < 3 values' do
|
54
|
+
expect{ Color::RGB.from_array([]) }.to raise_error(Color::RGBArraySizeError)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'will raise an exception if an array is not > values' do
|
58
|
+
expect{
|
59
|
+
Color::RGB.from_array([1, 2, 3, 4])
|
60
|
+
}.to raise_error(Color::RGBArraySizeError)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'will raise an exception if array values are greater than 255' do
|
64
|
+
expect{
|
65
|
+
Color::RGB.from_array([1, 2, 333])
|
66
|
+
}.to raise_error(Color::RGBArrayValueOutOfBoundsError)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'color conversions' do
|
72
|
+
let(:rgb) { [34, 155, 167] }
|
73
|
+
|
74
|
+
describe '#to_lab' do
|
75
|
+
it 'should convert to expected values' do
|
76
|
+
color = Color::RGB.from_array(rgb)
|
77
|
+
expect(color.to_lab).to eq([
|
78
|
+
58.57679489951133,
|
79
|
+
-27.28307080696585,
|
80
|
+
-15.45207927427481
|
81
|
+
])
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe '#to_xyz' do
|
86
|
+
it 'should convert to expected values' do
|
87
|
+
color = Color::RGB.from_array(rgb)
|
88
|
+
expect(color.to_xyz).to eq([
|
89
|
+
19.356083204751798,
|
90
|
+
26.57279128189712,
|
91
|
+
40.66810545648842
|
92
|
+
])
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Color::XYZ do
|
4
|
+
let(:rgb) { Color::RGB.new(255, 192, 203) }
|
5
|
+
subject { Color::XYZ.new(rgb) }
|
6
|
+
|
7
|
+
describe '.from_rgb' do
|
8
|
+
it 'returns an instance of Color::XYZ' do
|
9
|
+
expect(Color::XYZ.from_rgb(rgb)).to be_an_instance_of(Color::XYZ)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#initialize' do
|
14
|
+
it 'takes a Color::RGB object and initiates r, g, b attributes' do
|
15
|
+
expect(subject.instance_variable_get(:@r)).to eq(rgb.r)
|
16
|
+
expect(subject.instance_variable_get(:@g)).to eq(rgb.g)
|
17
|
+
expect(subject.instance_variable_get(:@b)).to eq(rgb.b)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'x, y, z' do
|
22
|
+
describe '#x' do
|
23
|
+
it 'calculates x' do
|
24
|
+
expect(subject.x).to eq(70.8691291752058)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#y' do
|
29
|
+
it 'calculates y' do
|
30
|
+
expect(subject.y).to eq(63.27107070246611)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#z' do
|
35
|
+
it 'calculates z' do
|
36
|
+
expect(subject.z).to eq(64.977242282389)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#to_a' do
|
42
|
+
it 'returns an array of x, y, z values' do
|
43
|
+
expect(subject.to_a).to eq([70.8691291752058, 63.27107070246611, 64.977242282389])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: color-rgb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ramin keene
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: codeclimate-test-reporter
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: RGB color utilities for converting RGB color into different colorspace
|
70
|
+
(CIE*Lab or XYZ), a different format, or comparing similarity of two colors
|
71
|
+
email:
|
72
|
+
- raminkeene@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- color-rgb.gemspec
|
83
|
+
- lib/color-rgb.rb
|
84
|
+
- lib/color.rb
|
85
|
+
- lib/color/cie_lab.rb
|
86
|
+
- lib/color/comparison.rb
|
87
|
+
- lib/color/rgb.rb
|
88
|
+
- lib/color/version.rb
|
89
|
+
- lib/color/xyz.rb
|
90
|
+
- spec/color/cie_lab_spec.rb
|
91
|
+
- spec/color/comparison_spec.rb
|
92
|
+
- spec/color/rgb_spec.rb
|
93
|
+
- spec/color/xyz_spec.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
homepage: http://github.com/apartmenttherapy/color-rgb
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.2.2
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: RGB color utlities conversion and color distance
|
119
|
+
test_files:
|
120
|
+
- spec/color/cie_lab_spec.rb
|
121
|
+
- spec/color/comparison_spec.rb
|
122
|
+
- spec/color/rgb_spec.rb
|
123
|
+
- spec/color/xyz_spec.rb
|
124
|
+
- spec/spec_helper.rb
|