diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..986e01e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,6 @@ +# Change Log + +## v0.0.1 + +- Initial RgbToHexColor release. + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4b29e78 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2014 Kyle Tolle + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..1781cee --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# RgbToHexColor + +Turn a tuple of rgb colors into its corresponding hexadecimal color value! + +## Install + +``` +gem install rgb_to_hex_color +``` + +## Usage + +``` +require 'rgb_to_hex_color' + +rgb_to_hex_color = RgbToHexColor.new(170, 187, 204) + +rgb_to_hex_color.rgb #=> [170, 187, 204] +rgb_to_hex_color.hex #=> '#AABBCC' +``` + +## Development + +### Install + +``` +gem install --dev rgb_to_hex_color +``` + +### Specs + +The default Rake task is to run the specs. + +``` +rake +``` + +## License + +MIT + diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..a938242 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) + +desc "Run specs" +task :default => :spec + diff --git a/lib/rgb_to_hex_color.rb b/lib/rgb_to_hex_color.rb new file mode 100644 index 0000000..9c121d7 --- /dev/null +++ b/lib/rgb_to_hex_color.rb @@ -0,0 +1,53 @@ +class RgbToHexColor + def initialize(r, g, b) + @r = r + @g = g + @b = b + end + + def rgb + [ @r, @g, @b ] + end + + def valid? + r_is_valid? && g_is_valid? && b_is_valid? + end + + def hex + if valid? + hex_string + end + end + +private + def r_is_valid? + is_valid_color_digit?(@r) + end + + def g_is_valid? + is_valid_color_digit?(@g) + end + + def b_is_valid? + is_valid_color_digit?(@b) + end + + def is_valid_color_digit?(digit) + (0..255).include? digit + end + + def hex_string + hex_r = decimal_to_hex(@r) + hex_g = decimal_to_hex(@g) + hex_b = decimal_to_hex(@b) + + "##{hex_r}#{hex_g}#{hex_b}" + end + + def decimal_to_hex(digit) + digit.to_s(16). + rjust(2, '0'). + upcase + end +end + diff --git a/rgb_to_hex_color.gemspec b/rgb_to_hex_color.gemspec new file mode 100644 index 0000000..6f14339 --- /dev/null +++ b/rgb_to_hex_color.gemspec @@ -0,0 +1,14 @@ +Gem::Specification.new do |s| + s.name = 'rgb_to_hex_color' + s.version = '0.0.1' + s.date = '2014-08-17' + s.summary = 'Convert rgb colors to their hexadecimal value.' + s.authors = ['Kyle Tolle'] + s.email = 'kyle@nullsix.com' + s.files = ['lib/rgb_to_hex_color.rb', 'spec/rgb_to_hex_color_spec.rb'] + s.license = 'MIT' + s.homepage = 'https://github.com/kyletolle/rgb_to_hex_color' + + s.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0' +end + diff --git a/spec/rgb_to_hex_color_spec.rb b/spec/rgb_to_hex_color_spec.rb new file mode 100644 index 0000000..2b817db --- /dev/null +++ b/spec/rgb_to_hex_color_spec.rb @@ -0,0 +1,89 @@ +require 'rspec' +require 'rgb_to_hex_color' + +describe RgbToHexColor do + let(:rgb_to_hex) { described_class.new(*initial_colors) } + + shared_context "with simple initial colors" do + let(:initial_colors ) { [ 1, 2, 3 ] } + end + + describe "#rgb" do + subject { rgb_to_hex.rgb } + + include_context "with simple initial colors" + + let(:expected_colors) { initial_colors } + + it "returns the colors it was initialized with" do + expect(subject).to eq expected_colors + end + end + + describe "#valid?" do + subject { rgb_to_hex.valid? } + + describe "for valid rgb colors" do + include_context "with simple initial colors" + + it "is true" do + expect(subject).to eq true + end + end + + describe "for invalid r color" do + let(:initial_colors) { [ -1, 2, 3 ] } + + it "is false" do + expect(subject).to eq false + end + end + + describe "for invalid g color" do + let(:initial_colors) { [ 1, "-2", 3 ] } + + it "is false" do + expect(subject).to eq false + end + end + + describe "for invalid b color" do + let(:initial_colors) { [ 1, 2, Object.new ] } + + it "is false" do + expect(subject).to eq false + end + end + end + + describe "#hex" do + subject { rgb_to_hex.hex } + + describe "with valid, small digits" do + let(:initial_colors) { [ 1, 2, 3 ] } + let(:expected_hex_color) { '#010203' } + + it "returns the correct hex string" do + expect(subject).to eq expected_hex_color + end + end + + describe "with valid, large digits" do + let(:initial_colors) { [ 100, 200, 222 ] } + let(:expected_hex_color) { '#64C8DE' } + + it "returns the correct hex string" do + expect(subject).to eq expected_hex_color + end + end + + describe "with invalid digits" do + let(:initial_colors) { [ -1, "b", Object.new ] } + + it "is nil" do + expect(subject).to be_nil + end + end + end +end +