color-converter 0.0.1 → 1.0.0
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 +4 -4
- data/.travis.yml +1 -1
- data/README.md +9 -9
- data/lib/color_converter.rb +18 -13
- data/lib/color_converter/version.rb +1 -1
- data/spec/color_converter_spec.rb +15 -15
- data/spec/spec_helper.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93ca73eb327003cd451e0b28913b6b291041544c
|
4
|
+
data.tar.gz: a0ba32573c471494bf9bad0c94701fb531aa1aaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21fd054616384a69070329f1cef03664bcc5941b59742dc81e81010a14058528febf6172a01ec7b594a8ebf0c460e8102705488db1204bb2e02132c8f92e9891
|
7
|
+
data.tar.gz: d1bd50de0146e73a1ab96e6d374cbd345e863ed00b3d0c13ae880bbdb99f5253680bb1faec4e0e9d645c39c2351c0e3840308d462068fce5532b1f5092d86663
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
# ColorConverter
|
2
|
+
[](http://badge.fury.io/rb/color-converter)
|
3
|
+
[](https://travis-ci.org/dkan/color-converter)
|
4
|
+
[](https://gemnasium.com/dkan/color-converter)
|
5
|
+
[](https://coveralls.io/r/dkan/color-converter)
|
2
6
|
|
3
7
|
ColorConverter is a simple way to convert between hexcolor, RGB, and CMYK values.
|
4
8
|
|
@@ -12,24 +16,20 @@ And then execute:
|
|
12
16
|
|
13
17
|
$ bundle install
|
14
18
|
|
15
|
-
## Usage
|
16
|
-
|
17
|
-
Convert to hexcolor example:
|
19
|
+
## Usage:
|
18
20
|
|
21
|
+
require 'color_converter'
|
22
|
+
|
19
23
|
# Returns a hexcolor code string
|
20
24
|
# e.g. "#FFFFFF"
|
21
25
|
ColorConverter.hex(255, 255, 255)
|
22
26
|
ColorConverter.hex(0, 0, 0, 0)
|
23
|
-
|
24
|
-
Convert to RGB example:
|
25
|
-
|
27
|
+
|
26
28
|
# Returns an array of color values
|
27
29
|
# e.g. [r, g, b]
|
28
30
|
ColorConverter.rgb('#FFFFFF')
|
29
31
|
ColorConverter.rgb(0, 0, 0, 0)
|
30
|
-
|
31
|
-
Convert to CMYK example:
|
32
|
-
|
32
|
+
|
33
33
|
# Returns an array of color values
|
34
34
|
# e.g. [c, m, y, k]
|
35
35
|
ColorConverter.cmyk('#000000')
|
data/lib/color_converter.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "color_converter/version"
|
2
2
|
|
3
3
|
module ColorConverter
|
4
|
+
HEX = "0123456789ABCDEF".freeze
|
4
5
|
|
5
6
|
# Takes RGB or CMYK values as parameters and returns hexcolor code in
|
6
7
|
# a string. Raises errors for incorrect inputs.
|
@@ -16,13 +17,13 @@ module ColorConverter
|
|
16
17
|
|
17
18
|
return values[0] if values.count == 1
|
18
19
|
|
19
|
-
# Raise error if values aren't
|
20
|
-
values
|
20
|
+
# Raise error if values aren't Integers.
|
21
|
+
ensure_all_integers! values
|
21
22
|
|
22
23
|
hex_array = values
|
23
|
-
hex_array = rgb(values
|
24
|
+
hex_array = rgb(*values) if values.count == 4
|
24
25
|
"#" + hex_array.map do |v|
|
25
|
-
|
26
|
+
HEX[(v-v%16)/16] + HEX[v%16]
|
26
27
|
end.join
|
27
28
|
end
|
28
29
|
|
@@ -41,11 +42,11 @@ module ColorConverter
|
|
41
42
|
return values if values.count == 3
|
42
43
|
|
43
44
|
if values.count == 1
|
44
|
-
hex_to_rgb(values
|
45
|
+
hex_to_rgb(*values)
|
45
46
|
elsif values.count == 4
|
46
|
-
# Raise error if values aren't
|
47
|
-
values
|
48
|
-
cmyk_to_rgb(values
|
47
|
+
# Raise error if values aren't Integers.
|
48
|
+
ensure_all_integers! values
|
49
|
+
cmyk_to_rgb(*values)
|
49
50
|
end
|
50
51
|
end
|
51
52
|
|
@@ -65,12 +66,11 @@ module ColorConverter
|
|
65
66
|
|
66
67
|
# Set cmky_array to array of RGB values.
|
67
68
|
cmyk_array = values
|
68
|
-
cmyk_array = rgb(values
|
69
|
+
cmyk_array = rgb(*values) if values.count == 1
|
69
70
|
|
70
|
-
# Raise error if values aren't
|
71
|
-
cmyk_array
|
72
|
-
|
73
|
-
rgb_to_cmyk(cmyk_array[0], cmyk_array[1], cmyk_array[2])
|
71
|
+
# Raise error if values aren't Integers
|
72
|
+
ensure_all_integers! cmyk_array
|
73
|
+
rgb_to_cmyk(*cmyk_array)
|
74
74
|
end
|
75
75
|
|
76
76
|
# Returns RGB values in array from hexcolor code.
|
@@ -123,4 +123,9 @@ module ColorConverter
|
|
123
123
|
def self.correct_values? (values)
|
124
124
|
[1, 3, 4].include? values.count
|
125
125
|
end
|
126
|
+
|
127
|
+
# Raise error if values are not Integers.
|
128
|
+
def self.ensure_all_integers! (values)
|
129
|
+
raise "Expecting Integers." unless values.all?(&:integer?)
|
130
|
+
end
|
126
131
|
end
|
@@ -2,70 +2,70 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe ColorConverter do
|
4
4
|
context "#hex" do
|
5
|
-
it "
|
5
|
+
it "converts RGB to hexcolor" do
|
6
6
|
ColorConverter.hex(0, 0, 0).should eq "#000000"
|
7
7
|
ColorConverter.hex(210, 105, 30).should eq "#D2691E"
|
8
8
|
end
|
9
9
|
|
10
|
-
it "
|
10
|
+
it "converts CMYK to hexcolor" do
|
11
11
|
ColorConverter.hex(0, 50, 86, 18).should eq "#D2691E"
|
12
12
|
end
|
13
13
|
|
14
|
-
it "
|
14
|
+
it "returns parameter if 1 value is entered" do
|
15
15
|
ColorConverter.hex("#D2691E").should eq "#D2691E"
|
16
16
|
end
|
17
17
|
|
18
|
-
it "
|
18
|
+
it "raises error if incorrect number of values is entered" do
|
19
19
|
expect { ColorConverter.hex(0, 0, 0, 0, 0) }.to raise_error
|
20
20
|
end
|
21
21
|
|
22
|
-
it "
|
22
|
+
it "expects proper values" do
|
23
23
|
expect { ColorConverter.hex("hello", "world", "foobar", "baz") }.to raise_error
|
24
24
|
expect { ColorConverter.hex("hello", "world", "foobar") }.to raise_error
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
context "#rgb" do
|
29
|
-
it "
|
29
|
+
it "converts hexcolor to RGB" do
|
30
30
|
ColorConverter.rgb("#D2691E").should eq [210, 105, 30]
|
31
31
|
end
|
32
32
|
|
33
|
-
it "
|
33
|
+
it "converts CMYK to RGB" do
|
34
34
|
ColorConverter.rgb(0, 50, 86, 18).should eq [210, 105, 30]
|
35
35
|
end
|
36
36
|
|
37
|
-
it "
|
37
|
+
it "returns parameter if 3 values are entered" do
|
38
38
|
ColorConverter.rgb(210, 105, 30).should eq [210, 105, 30]
|
39
39
|
end
|
40
40
|
|
41
|
-
it "
|
41
|
+
it "raises error if incorrect number of values is entered" do
|
42
42
|
expect { ColorConverter.rgb(0, 0) }.to raise_error
|
43
43
|
end
|
44
44
|
|
45
|
-
it "
|
45
|
+
it "expects proper values" do
|
46
46
|
expect { ColorConverter.rgb("hello", "world", "foobar", "baz") }.to raise_error
|
47
47
|
expect { ColorConverter.rgb("hello") }.to raise_error
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
51
|
context "#cmyk" do
|
52
|
-
it "
|
52
|
+
it "converts hexcolor to CMYK" do
|
53
53
|
ColorConverter.cmyk("#D2691E").should eq [0, 50, 86, 18]
|
54
54
|
end
|
55
55
|
|
56
|
-
it "
|
56
|
+
it "converts RGB to CMYK" do
|
57
57
|
ColorConverter.cmyk(210, 105, 30).should eq [0, 50, 86, 18]
|
58
58
|
end
|
59
59
|
|
60
|
-
it "
|
60
|
+
it "returns parameter if 4 values are entered" do
|
61
61
|
ColorConverter.cmyk(0, 50, 86, 18).should eq [0, 50, 86, 18]
|
62
62
|
end
|
63
63
|
|
64
|
-
it "
|
64
|
+
it "raises error if incorrect number of values is entered" do
|
65
65
|
expect { ColorConverter.cmyk(0, 0, 0, 0, 0) }.to raise_error
|
66
66
|
end
|
67
67
|
|
68
|
-
it "
|
68
|
+
it "expects proper values" do
|
69
69
|
expect { ColorConverter.cmyk("hello", "world", "foobar") }.to raise_error
|
70
70
|
expect { ColorConverter.cmyk("hello") }.to raise_error
|
71
71
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: color-converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Damien Kan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|