rgb_to_hex_color 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/rgb_to_hex_color.rb +53 -0
- data/spec/rgb_to_hex_color_spec.rb +89 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c1b140fb46e1eed10e95f6e6ed781beeb83f3c7a
|
4
|
+
data.tar.gz: 47fa1fc1448a8b3ab364445f09196c903f3b98d0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b17b0ce2a059b201ee29846d0dce6c1011d4763ff4242df3ba4276d52d0890b3166ae1b0252a4bf81ba1b5cd9536b2f1e7cf032b5edafcd8d3aff2d64a9e53a6
|
7
|
+
data.tar.gz: c13a206baa2687015e7d140ec38575aa5f45bd7628987d1f28618ad1fca10304b35be4fc4dd812392b039af1c35450a3c7b6f8837f606b551ed932f3db50e109
|
@@ -0,0 +1,53 @@
|
|
1
|
+
class RgbToHexColor
|
2
|
+
def initialize(r, g, b)
|
3
|
+
@r = r
|
4
|
+
@g = g
|
5
|
+
@b = b
|
6
|
+
end
|
7
|
+
|
8
|
+
def rgb
|
9
|
+
[ @r, @g, @b ]
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?
|
13
|
+
r_is_valid? && g_is_valid? && b_is_valid?
|
14
|
+
end
|
15
|
+
|
16
|
+
def hex
|
17
|
+
if valid?
|
18
|
+
hex_string
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def r_is_valid?
|
24
|
+
is_valid_color_digit?(@r)
|
25
|
+
end
|
26
|
+
|
27
|
+
def g_is_valid?
|
28
|
+
is_valid_color_digit?(@g)
|
29
|
+
end
|
30
|
+
|
31
|
+
def b_is_valid?
|
32
|
+
is_valid_color_digit?(@b)
|
33
|
+
end
|
34
|
+
|
35
|
+
def is_valid_color_digit?(digit)
|
36
|
+
(0..255).include? digit
|
37
|
+
end
|
38
|
+
|
39
|
+
def hex_string
|
40
|
+
hex_r = decimal_to_hex(@r)
|
41
|
+
hex_g = decimal_to_hex(@g)
|
42
|
+
hex_b = decimal_to_hex(@b)
|
43
|
+
|
44
|
+
"##{hex_r}#{hex_g}#{hex_b}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def decimal_to_hex(digit)
|
48
|
+
digit.to_s(16).
|
49
|
+
rjust(2, '0').
|
50
|
+
upcase
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rgb_to_hex_color'
|
3
|
+
|
4
|
+
describe RgbToHexColor do
|
5
|
+
let(:rgb_to_hex) { described_class.new(*initial_colors) }
|
6
|
+
|
7
|
+
shared_context "with simple initial colors" do
|
8
|
+
let(:initial_colors ) { [ 1, 2, 3 ] }
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#rgb" do
|
12
|
+
subject { rgb_to_hex.rgb }
|
13
|
+
|
14
|
+
include_context "with simple initial colors"
|
15
|
+
|
16
|
+
let(:expected_colors) { initial_colors }
|
17
|
+
|
18
|
+
it "returns the colors it was initialized with" do
|
19
|
+
expect(subject).to eq expected_colors
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#valid?" do
|
24
|
+
subject { rgb_to_hex.valid? }
|
25
|
+
|
26
|
+
describe "for valid rgb colors" do
|
27
|
+
include_context "with simple initial colors"
|
28
|
+
|
29
|
+
it "is true" do
|
30
|
+
expect(subject).to eq true
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "for invalid r color" do
|
35
|
+
let(:initial_colors) { [ -1, 2, 3 ] }
|
36
|
+
|
37
|
+
it "is false" do
|
38
|
+
expect(subject).to eq false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "for invalid g color" do
|
43
|
+
let(:initial_colors) { [ 1, "-2", 3 ] }
|
44
|
+
|
45
|
+
it "is false" do
|
46
|
+
expect(subject).to eq false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "for invalid b color" do
|
51
|
+
let(:initial_colors) { [ 1, 2, Object.new ] }
|
52
|
+
|
53
|
+
it "is false" do
|
54
|
+
expect(subject).to eq false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#hex" do
|
60
|
+
subject { rgb_to_hex.hex }
|
61
|
+
|
62
|
+
describe "with valid, small digits" do
|
63
|
+
let(:initial_colors) { [ 1, 2, 3 ] }
|
64
|
+
let(:expected_hex_color) { '#010203' }
|
65
|
+
|
66
|
+
it "returns the correct hex string" do
|
67
|
+
expect(subject).to eq expected_hex_color
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "with valid, large digits" do
|
72
|
+
let(:initial_colors) { [ 100, 200, 222 ] }
|
73
|
+
let(:expected_hex_color) { '#64C8DE' }
|
74
|
+
|
75
|
+
it "returns the correct hex string" do
|
76
|
+
expect(subject).to eq expected_hex_color
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "with invalid digits" do
|
81
|
+
let(:initial_colors) { [ -1, "b", Object.new ] }
|
82
|
+
|
83
|
+
it "is nil" do
|
84
|
+
expect(subject).to be_nil
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rgb_to_hex_color
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyle Tolle
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.0.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.0.0
|
33
|
+
description:
|
34
|
+
email: kyle@nullsix.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- lib/rgb_to_hex_color.rb
|
40
|
+
- spec/rgb_to_hex_color_spec.rb
|
41
|
+
homepage: https://github.com/kyletolle/rgb_to_hex_color
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.2.2
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Convert rgb colors to their hexadecimal value.
|
65
|
+
test_files: []
|
66
|
+
has_rdoc:
|