color-converter 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: 21661f8d4684b788378faf7e5294fa9ac46f22c5
4
+ data.tar.gz: 18a2c8cc08696a3568c7c8b1680b8b2dc32718a5
5
+ SHA512:
6
+ metadata.gz: 7e46fd6cc51a8b87c3ce20e9507f288c46f2d5809f382c475b5d7e482c8bc5919e236e82a8405576a27a208e3971011ebdc394475625f999d9b48916c449dd61
7
+ data.tar.gz: 0bc3b5d9ea124765a63aefbb27aa1e61a0be5f65cd90a8808c5350b242667f929e4c0bd4e8108a13d44fb7f5fbf1e47a398fff59be0998a7018b673abf6b08a7
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ branches:
6
+ only:
7
+ - master
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gem 'coveralls', require: false
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Damien
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.
@@ -0,0 +1,44 @@
1
+ # ColorConverter
2
+
3
+ ColorConverter is a simple way to convert between hexcolor, RGB, and CMYK values.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'color-converter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ ## Usage
16
+
17
+ Convert to hexcolor example:
18
+
19
+ # Returns a hexcolor code string
20
+ # e.g. "#FFFFFF"
21
+ ColorConverter.hex(255, 255, 255)
22
+ ColorConverter.hex(0, 0, 0, 0)
23
+
24
+ Convert to RGB example:
25
+
26
+ # Returns an array of color values
27
+ # e.g. [r, g, b]
28
+ ColorConverter.rgb('#FFFFFF')
29
+ ColorConverter.rgb(0, 0, 0, 0)
30
+
31
+ Convert to CMYK example:
32
+
33
+ # Returns an array of color values
34
+ # e.g. [c, m, y, k]
35
+ ColorConverter.cmyk('#000000')
36
+ ColorConverter.cmyk(255, 255, 255)
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'color_converter/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'color-converter'
7
+ s.version = Color::Converter::VERSION
8
+ s.authors = ['Damien Kan']
9
+ s.email = ['damien.kan@gmail.com']
10
+ s.description = 'Color-converter is an easy way to convert hexcolor, RGB, and CMYK between each other.'
11
+ s.summary = ''
12
+ s.homepage = 'http://github.com/dkan/color-converter'
13
+ s.license = 'MIT'
14
+
15
+ s.files = `git ls-files`.split($/)
16
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+ s.require_paths = ['lib']
19
+
20
+ s.required_ruby_version = '>= 1.9.3'
21
+
22
+ s.add_development_dependency 'bundler', '~> 1.3'
23
+ s.add_development_dependency 'rake'
24
+ s.add_development_dependency 'rspec'
25
+ s.add_development_dependency 'simplecov'
26
+ s.add_development_dependency 'rack-test'
27
+ end
@@ -0,0 +1,126 @@
1
+ require "color_converter/version"
2
+
3
+ module ColorConverter
4
+
5
+ # Takes RGB or CMYK values as parameters and returns hexcolor code in
6
+ # a string. Raises errors for incorrect inputs.
7
+ #
8
+ # Example use:
9
+ # ColorConverter.hex(255, 255, 255)
10
+ # ColorConverter.hex(0, 0, 0, 0)
11
+ #
12
+ # Would return '#FFFFFF'
13
+ def self.hex (*values)
14
+ # Raise error if there are the wrong number of values.
15
+ raise "Incorrect values." unless correct_values? (values)
16
+
17
+ return values[0] if values.count == 1
18
+
19
+ # Raise error if values aren't Fixnums.
20
+ values.each { |v| raise "Expecting Fixnum." unless v.class == Fixnum }
21
+
22
+ hex_array = values
23
+ hex_array = rgb(values[0], values[1], values[2], values[3]) if values.count == 4
24
+ "#" + hex_array.map do |v|
25
+ "0123456789ABCDEF"[(v-v%16)/16] + "0123456789ABCDEF"[v%16]
26
+ end.join
27
+ end
28
+
29
+ # Takes hexcolor code or CMYK values as parameters and returns RGB
30
+ # values in an array. Raises errors for incorrect inputs.
31
+ #
32
+ # Example use:
33
+ # ColorConverter.rgb('#FFFFFF')
34
+ # ColorConverter.rgb(0, 0, 0, 0)
35
+ #
36
+ # Would return [255, 255, 255]
37
+ def self.rgb (*values)
38
+ # Raise error if there are the wrong number of values.
39
+ raise "Incorrect values" unless correct_values? (values)
40
+
41
+ return values if values.count == 3
42
+
43
+ if values.count == 1
44
+ hex_to_rgb(values[0])
45
+ elsif values.count == 4
46
+ # Raise error if values aren't Fixnums.
47
+ values.each { |v| raise "Expecting Fixnum." unless v.class == Fixnum }
48
+ cmyk_to_rgb(values[0], values[1], values[2], values[3])
49
+ end
50
+ end
51
+
52
+ # Takes hexcolor code or RGB values as parameters and returns CMYK
53
+ # values in an array. Raises errors for incorrect inputs.
54
+ #
55
+ # Example use:
56
+ # ColorConverter.rgb('#FFFFFF')
57
+ # ColorConverter.rgb(255, 255, 255)
58
+ #
59
+ # Would return [0, 0, 0, 0]
60
+ def self.cmyk (*values)
61
+ # Raise error if there are the wrong number of values.
62
+ raise "Incorrect values." unless correct_values? (values)
63
+
64
+ return values if values.count == 4
65
+
66
+ # Set cmky_array to array of RGB values.
67
+ cmyk_array = values
68
+ cmyk_array = rgb(values[0]) if values.count == 1
69
+
70
+ # Raise error if values aren't Fixnums
71
+ cmyk_array.each { |v| raise "Expecting Fixnum." unless v.class == Fixnum }
72
+
73
+ rgb_to_cmyk(cmyk_array[0], cmyk_array[1], cmyk_array[2])
74
+ end
75
+
76
+ # Returns RGB values in array from hexcolor code.
77
+ #
78
+ # Raise exception for invalid hexcolor codes.
79
+ def self.hex_to_rgb (hex)
80
+ remove_hex(hex).scan(/../).map do |v|
81
+ begin
82
+ Integer(v, 16)
83
+ rescue Exception => e
84
+ raise "Invalid hexcolor."
85
+ end
86
+ end
87
+ end
88
+
89
+ # Returns RGB values in array from CMYK inputs.
90
+ def self.cmyk_to_rgb (c, m, y, k)
91
+ c = c * 0.01
92
+ m = m * 0.01
93
+ y = y * 0.01
94
+ k = k * 0.01
95
+ r = 255 * (1-c) * (1-k)
96
+ g = 255 * (1-m) * (1-k)
97
+ b = 255 * (1-y) * (1-k)
98
+ [r.ceil, g.ceil, b.ceil]
99
+ end
100
+
101
+ # Returns CMYK values in array from RGB inputs.
102
+ def self.rgb_to_cmyk (r, g, b)
103
+ k = [r, g, b].map{|v| 1-(v.to_f/255)}.min
104
+ r = r.to_f / 255
105
+ g = g.to_f / 255
106
+ b = b.to_f / 255
107
+ c = (1-r-k) / (1-k)
108
+ m = (1-g-k) / (1-k)
109
+ y = (1-b-k) / (1-k)
110
+ [c, m, y, k].map do |v|
111
+ (v * 100).round
112
+ end
113
+ end
114
+
115
+ # Remove '#' from the beginning of the hexcolor code
116
+ # if it exists.
117
+ def self.remove_hex (hex)
118
+ hex[0] == '#' ? hex[1..7] : hex
119
+ end
120
+
121
+ # Check count of values parameters. Return true if
122
+ # it is the right count, false if it isn't.
123
+ def self.correct_values? (values)
124
+ [1, 3, 4].include? values.count
125
+ end
126
+ end
@@ -0,0 +1,5 @@
1
+ module Color
2
+ module Converter
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe ColorConverter do
4
+ context "#hex" do
5
+ it "should convert RGB to hexcolor" do
6
+ ColorConverter.hex(0, 0, 0).should eq "#000000"
7
+ ColorConverter.hex(210, 105, 30).should eq "#D2691E"
8
+ end
9
+
10
+ it "should convert CMYK to hexcolor" do
11
+ ColorConverter.hex(0, 50, 86, 18).should eq "#D2691E"
12
+ end
13
+
14
+ it "should return parameter if 1 value is entered" do
15
+ ColorConverter.hex("#D2691E").should eq "#D2691E"
16
+ end
17
+
18
+ it "should raise error if incorrect number of values is entered" do
19
+ expect { ColorConverter.hex(0, 0, 0, 0, 0) }.to raise_error
20
+ end
21
+
22
+ it "should expect proper values" do
23
+ expect { ColorConverter.hex("hello", "world", "foobar", "baz") }.to raise_error
24
+ expect { ColorConverter.hex("hello", "world", "foobar") }.to raise_error
25
+ end
26
+ end
27
+
28
+ context "#rgb" do
29
+ it "should convert hexcolor to RGB" do
30
+ ColorConverter.rgb("#D2691E").should eq [210, 105, 30]
31
+ end
32
+
33
+ it "should convert CMYK to RGB" do
34
+ ColorConverter.rgb(0, 50, 86, 18).should eq [210, 105, 30]
35
+ end
36
+
37
+ it "should return parameter if 3 values are entered" do
38
+ ColorConverter.rgb(210, 105, 30).should eq [210, 105, 30]
39
+ end
40
+
41
+ it "should raise error if incorrect number of values is entered" do
42
+ expect { ColorConverter.rgb(0, 0) }.to raise_error
43
+ end
44
+
45
+ it "should expect proper values" do
46
+ expect { ColorConverter.rgb("hello", "world", "foobar", "baz") }.to raise_error
47
+ expect { ColorConverter.rgb("hello") }.to raise_error
48
+ end
49
+ end
50
+
51
+ context "#cmyk" do
52
+ it "should convert hexcolor to CMYK" do
53
+ ColorConverter.cmyk("#D2691E").should eq [0, 50, 86, 18]
54
+ end
55
+
56
+ it "should convert RGB to CMYK" do
57
+ ColorConverter.cmyk(210, 105, 30).should eq [0, 50, 86, 18]
58
+ end
59
+
60
+ it "should return parameter if 4 values are entered" do
61
+ ColorConverter.cmyk(0, 50, 86, 18).should eq [0, 50, 86, 18]
62
+ end
63
+
64
+ it "should raise error if incorrect number of values is entered" do
65
+ expect { ColorConverter.cmyk(0, 0, 0, 0, 0) }.to raise_error
66
+ end
67
+
68
+ it "should expect proper values" do
69
+ expect { ColorConverter.cmyk("hello", "world", "foobar") }.to raise_error
70
+ expect { ColorConverter.cmyk("hello") }.to raise_error
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,14 @@
1
+ require 'rspec'
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+
5
+ require 'simplecov'
6
+ require 'coveralls'
7
+ SimpleCov.start
8
+
9
+ require 'rack/test'
10
+ require 'color_converter'
11
+
12
+ RSpec.configure do |config|
13
+ config.color_enabled = true
14
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: color-converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Damien Kan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-16 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: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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: rspec
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: simplecov
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
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Color-converter is an easy way to convert hexcolor, RGB, and CMYK between
84
+ each other.
85
+ email:
86
+ - damien.kan@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .travis.yml
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - color-converter.gemspec
98
+ - lib/color_converter.rb
99
+ - lib/color_converter/version.rb
100
+ - spec/color_converter_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: http://github.com/dkan/color-converter
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: 1.9.3
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.3
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: ''
126
+ test_files:
127
+ - spec/color_converter_spec.rb
128
+ - spec/spec_helper.rb