Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kyletolle committed Aug 17, 2014
0 parents commit 37db527
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Change Log

## v0.0.1

- Initial RgbToHexColor release.

22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.

41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

desc "Run specs"
task :default => :spec

53 changes: 53 additions & 0 deletions lib/rgb_to_hex_color.rb
Original file line number Diff line number Diff line change
@@ -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

14 changes: 14 additions & 0 deletions rgb_to_hex_color.gemspec
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]'
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

89 changes: 89 additions & 0 deletions spec/rgb_to_hex_color_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 37db527

Please sign in to comment.