reggieb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2009 Wyatt M. Greene
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,14 @@
1
+ = Reggie B. -- A Hex/RGB Color Converter
2
+
3
+ == Description
4
+
5
+ Reggie B. is a utility for converting colors between rgb and hex format. It was created
6
+ to support http://www.2rgb.com.
7
+
8
+ == Examples
9
+
10
+ ReggieB.convert "rgb(255, 255, 255)" => "0xffffff"
11
+ ReggieB.convert "255, 255, 255" => "0xffffff"
12
+ ReggieB.convert "0xffffff" => [255, 255, 255]
13
+ ReggieB.convert "#ffffff" => [255, 255, 255]
14
+ ReggieB.convert "#fff" => [255, 255, 255]
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+ require 'rubygems'
6
+
7
+ desc 'Default: run unit tests.'
8
+ task :default => :test
9
+
10
+ desc 'Test Reggie B.'
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << 'lib'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for Reggie B.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'Reggie B.'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ gem_spec = Gem::Specification.new do |s|
26
+ s.name = "reggieb"
27
+ s.version = "0.0.1"
28
+ s.author = "Wyatt Greene"
29
+ s.email = "techiferous@gmail.com"
30
+ s.summary = "A Hex/RGB color converter."
31
+ s.description = %Q{
32
+ Reggie B. converts colors between hex format and rgb format.
33
+ It is very forgiving in the syntax it allows.
34
+ }
35
+ s.require_path = "lib"
36
+ s.files = FileList["lib/**/*.rb", "[A-Z]*", "test/**/*"].to_a
37
+ s.homepage = "http://github.com/techiferous/reggieb"
38
+ s.requirements << "none"
39
+ s.has_rdoc = true
40
+ s.test_files = Dir.glob("test/**/*.rb")
41
+ end
42
+
43
+ Rake::GemPackageTask.new(gem_spec) do |pkg|
44
+ end
data/lib/reggieb.rb ADDED
@@ -0,0 +1,82 @@
1
+ class ReggieB
2
+
3
+ # converts rgb colors to hex colors and vice versa. The input is a string representing
4
+ # an rgb or hex color. Many formats are allowed, including:
5
+ #
6
+ # - "rgb(255, 255, 255)"
7
+ # - "rgb(255, 255, 255);"
8
+ # - "rgb 255, 255, 255 "
9
+ # - "(255, 255, 255)"
10
+ # - "255, 255, 255"
11
+ # - "255 255 255"
12
+ # - "0xffffff"
13
+ # - "0xFFFFFF"
14
+ # - "#ffffff"
15
+ # - "ffffff"
16
+ # - "fff"
17
+ #
18
+ # In the case only three hex digits are given, the three digits are assumed to expand
19
+ # into six digits like so:
20
+ #
21
+ # - fff => ffffff
22
+ # - 78e => 7788ee
23
+ # - 084 => 008844
24
+ # - ace => aaccee
25
+ #
26
+ # The output is either a hex color or an rgb color. A hex color is represented as a
27
+ # string formatted like "0xdddddd" where d represents a hex digit. An rgb color is
28
+ # represented as an array of three integers between 0 and 255.
29
+ #
30
+ def self.convert(s)
31
+ s = s.downcase
32
+ s.gsub!(';', '')
33
+ if s =~ /^\s*((0x|#|)[0-9,a-f]{6})\s*$/
34
+ return to_rgb($1)
35
+ end
36
+ if s =~ /^\s*(0x|#|)([0-9,a-f])([0-9,a-f])([0-9,a-f])\s*$/
37
+ s = $2 + $2 + $3 + $3 + $4 + $4
38
+ return to_rgb(s)
39
+ end
40
+ if s =~ /^\D*(\d{1,3})\D+(\d{1,3})\D+(\d{1,3})\D*$/
41
+ return to_hex($1.to_i, $2.to_i, $3.to_i)
42
+ end
43
+ raise ArgumentError.new("Bad color format.")
44
+ end
45
+
46
+ # converts an rgb color to hex. rgb values must be between 0 and 255. The output
47
+ # is a string formatted like "0xdddddd" where d represents a hex digit.
48
+ #
49
+ def self.to_hex(r, g, b)
50
+ unless (0..255).include?(r) && (0..255).include?(g) && (0..255).include?(b)
51
+ message = "RGB values should be between 0 and 255. Received #{r}, #{g}, #{b}."
52
+ raise ArgumentError.new(message)
53
+ end
54
+ "0x#{to_hex_byte(r)}#{to_hex_byte(g)}#{to_hex_byte(b)}"
55
+ end
56
+
57
+ # converts a hex color to rgb. The hex string should be formatted as "0xdddddd" or
58
+ # "dddddd" where d represents a hex digit. An array of three integers is returned
59
+ # representing red, green, and blue values between 0 and 255.
60
+ #
61
+ def self.to_rgb(hex)
62
+ hex = hex.downcase
63
+ if hex !~ /0x[0-9,a-f]{6}/ && hex !~ /[0-9,a-f]{6}/
64
+ raise ArgumentError.new("Hex number should be formatted as 0xdddddd or dddddd.")
65
+ end
66
+ last_byte = hex[-2, 2]
67
+ middle_byte = hex[-4, 2]
68
+ first_byte = hex[-6, 2]
69
+ return [first_byte.to_i(16), middle_byte.to_i(16), last_byte.to_i(16)]
70
+ end
71
+
72
+ def self.to_hex_byte(digits)
73
+ hex_string = digits.to_s(16)
74
+ if hex_string.length == 1
75
+ "0" + hex_string
76
+ else
77
+ hex_string
78
+ end
79
+ end
80
+ private_class_method :to_hex_byte
81
+
82
+ end
@@ -0,0 +1,178 @@
1
+ require 'test/test_helper'
2
+ require 'reggieb'
3
+
4
+ class ReggieBTest < Test::Unit::TestCase
5
+
6
+ def test_simple_rgb_to_hex
7
+ assert_equal "0xffffff", ReggieB.to_hex(255, 255, 255)
8
+ assert_equal "0x000000", ReggieB.to_hex(0, 0, 0)
9
+ assert_equal "0x00000f", ReggieB.to_hex(0, 0, 15)
10
+ assert_equal "0x001100", ReggieB.to_hex(0, 17, 0)
11
+ assert_equal "0xa01100", ReggieB.to_hex(160, 17, 0)
12
+ end
13
+
14
+ def test_simple_rgb_to_hex_errors
15
+ assert_raise ArgumentError do
16
+ ReggieB.to_hex(255, 256, 255)
17
+ end
18
+ assert_raise ArgumentError do
19
+ ReggieB.to_hex(-10, 10, 10)
20
+ end
21
+ assert_raise ArgumentError do
22
+ ReggieB.to_hex(8, 9, 1000)
23
+ end
24
+ end
25
+
26
+ def test_simple_hex_to_rgb
27
+ assert_equal [255, 255, 255], ReggieB.to_rgb("0xffffff")
28
+ assert_equal [0, 0, 0], ReggieB.to_rgb("0x000000")
29
+ assert_equal [0, 0, 15], ReggieB.to_rgb("0x00000f")
30
+ assert_equal [0, 17, 0], ReggieB.to_rgb("0x001100")
31
+ assert_equal [160, 17, 0], ReggieB.to_rgb("0xa01100")
32
+ assert_equal [0, 17, 0], ReggieB.to_rgb("001100")
33
+ assert_equal [160, 17, 0], ReggieB.to_rgb("a01100")
34
+ assert_equal [160, 17, 0], ReggieB.to_rgb("A01100")
35
+ assert_equal [255, 255, 255], ReggieB.to_rgb("0xFFFFFF")
36
+ end
37
+
38
+ def test_simple_hex_to_rgb_errors
39
+ assert_raise ArgumentError do
40
+ ReggieB.to_rgb("0xfff")
41
+ end
42
+ assert_raise ArgumentError do
43
+ ReggieB.to_rgb("fff")
44
+ end
45
+ assert_raise ArgumentError do
46
+ ReggieB.to_rgb("forks")
47
+ end
48
+ assert_raise ArgumentError do
49
+ ReggieB.to_rgb("0x7897zq")
50
+ end
51
+ end
52
+
53
+ def test_six_digit_hex
54
+ assert_equal [255, 255, 255], ReggieB.convert("0xffffff")
55
+ assert_equal [0, 0, 0], ReggieB.convert("0x000000")
56
+ assert_equal [0, 0, 15], ReggieB.convert("0x00000f")
57
+ assert_equal [0, 17, 0], ReggieB.convert("0x001100")
58
+ assert_equal [160, 17, 0], ReggieB.convert("0xa01100")
59
+ assert_equal [0, 17, 0], ReggieB.convert("001100")
60
+ assert_equal [160, 17, 0], ReggieB.convert("a01100")
61
+ assert_equal [160, 17, 0], ReggieB.convert("A01100")
62
+ assert_equal [255, 255, 255], ReggieB.convert("0xFFFFFF")
63
+ end
64
+
65
+ def test_three_digit_hex
66
+ assert_equal [255, 255, 255], ReggieB.convert("0xfff")
67
+ assert_equal [0, 0, 0], ReggieB.convert("0x000")
68
+ assert_equal [0, 0, 255], ReggieB.convert("0x00f")
69
+ assert_equal [0, 17, 0], ReggieB.convert("0x010")
70
+ assert_equal [34, 0, 170], ReggieB.convert("0x20a")
71
+ assert_equal [255, 255, 255], ReggieB.convert("0xFFF")
72
+ assert_equal [0, 0, 255], ReggieB.convert("0x00F")
73
+ assert_equal [34, 0, 170], ReggieB.convert("0x20A")
74
+ assert_equal [255, 255, 255], ReggieB.convert("FFF")
75
+ assert_equal [0, 0, 255], ReggieB.convert("00F")
76
+ assert_equal [34, 0, 170], ReggieB.convert("20A")
77
+ assert_equal [255, 255, 255], ReggieB.convert("fff")
78
+ end
79
+
80
+ def test_wrong_digit_hex
81
+ assert_raise ArgumentError do
82
+ ReggieB.convert("0xffff")
83
+ end
84
+ assert_raise ArgumentError do
85
+ ReggieB.convert("0x12345")
86
+ end
87
+ assert_raise ArgumentError do
88
+ ReggieB.convert("0x123456789")
89
+ end
90
+ assert_raise ArgumentError do
91
+ ReggieB.convert("0x1")
92
+ end
93
+ assert_raise ArgumentError do
94
+ ReggieB.convert("0x")
95
+ end
96
+ end
97
+
98
+ def test_hex_prefixes
99
+ assert_equal [255, 255, 255], ReggieB.convert("0xffffff")
100
+ assert_equal [0, 170, 0], ReggieB.convert("0x0a0")
101
+ assert_equal [0, 0, 11], ReggieB.convert("0x00000B")
102
+ assert_equal [255, 255, 255], ReggieB.convert("ffffff")
103
+ assert_equal [0, 170, 0], ReggieB.convert("0a0")
104
+ assert_equal [0, 0, 11], ReggieB.convert("00000B")
105
+ assert_equal [255, 255, 255], ReggieB.convert("#ffffff")
106
+ assert_equal [0, 170, 0], ReggieB.convert("#0a0")
107
+ assert_equal [0, 0, 11], ReggieB.convert("#00000B")
108
+ end
109
+
110
+ def test_whitespace
111
+ assert_equal [255, 255, 255], ReggieB.convert(" 0xffffff ")
112
+ assert_equal [0, 170, 0], ReggieB.convert(" 0x0a0 ")
113
+ assert_equal [0, 0, 11], ReggieB.convert("0x00000B ")
114
+ assert_equal [255, 255, 255], ReggieB.convert(" ffffff")
115
+ assert_equal [0, 170, 0], ReggieB.convert("\t0a0")
116
+ assert_equal [0, 0, 11], ReggieB.convert("\n00000B\t")
117
+ assert_equal [255, 255, 255], ReggieB.convert(" #ffffff")
118
+ assert_equal [0, 170, 0], ReggieB.convert("#0a0\n")
119
+ assert_equal [0, 0, 11], ReggieB.convert(" #00000B ")
120
+ end
121
+
122
+ def test_bad_hex_digits
123
+ assert_raise ArgumentError do
124
+ ReggieB.convert("0xbcq")
125
+ end
126
+ assert_raise ArgumentError do
127
+ ReggieB.convert("ffg")
128
+ end
129
+ assert_raise ArgumentError do
130
+ ReggieB.convert("0x0967uf")
131
+ end
132
+ end
133
+
134
+ def test_semicolons
135
+ assert_equal [255, 255, 255], ReggieB.convert("0xffffff;")
136
+ assert_equal [0, 170, 0], ReggieB.convert(" 0x0a0;")
137
+ assert_equal [0, 0, 11], ReggieB.convert("0x00000B; ")
138
+ assert_equal [255, 255, 255], ReggieB.convert(" ffffff;")
139
+ assert_equal [0, 170, 0], ReggieB.convert("\t0a0 ;")
140
+ assert_equal [0, 0, 11], ReggieB.convert("\n00000B\t;")
141
+ assert_equal [255, 255, 255], ReggieB.convert(" #ffffff;")
142
+ assert_equal [0, 170, 0], ReggieB.convert("#0a0;")
143
+ assert_equal [0, 0, 11], ReggieB.convert("#00000B;")
144
+ end
145
+
146
+ def test_simple_rgb
147
+ assert_equal "0xffffff", ReggieB.convert("255, 255, 255")
148
+ assert_equal "0x000000", ReggieB.convert("0, 0, 0")
149
+ assert_equal "0x00000f", ReggieB.convert("0, 0, 15")
150
+ assert_equal "0x001100", ReggieB.convert("0, 17, 0")
151
+ assert_equal "0xa01100", ReggieB.convert("160, 17, 0")
152
+ end
153
+
154
+ def test_rgb_whitespace
155
+ assert_equal "0xffffff", ReggieB.convert("255,255,255")
156
+ assert_equal "0x000000", ReggieB.convert(" 0, 0, 0 ")
157
+ assert_equal "0x00000f", ReggieB.convert("0\t0\t15")
158
+ assert_equal "0x001100", ReggieB.convert("\t0 17,0")
159
+ assert_equal "0xa01100", ReggieB.convert(" 160, 17, 0 ")
160
+ end
161
+
162
+ def test_rgb_semicolons
163
+ assert_equal "0xffffff", ReggieB.convert("255,255,255;")
164
+ assert_equal "0x000000", ReggieB.convert(" 0, 0, 0; ")
165
+ assert_equal "0x00000f", ReggieB.convert("0\t0\t15;")
166
+ assert_equal "0x001100", ReggieB.convert("\t0 17,0;")
167
+ assert_equal "0xa01100", ReggieB.convert(" 160, 17, 0 ;")
168
+ end
169
+
170
+ def test_rgb_delimeters
171
+ assert_equal "0xffffff", ReggieB.convert("255-255-255")
172
+ assert_equal "0x000000", ReggieB.convert("0 0 0")
173
+ assert_equal "0x00000f", ReggieB.convert("0 0 15")
174
+ assert_equal "0x001100", ReggieB.convert("rgb(0, 17, 0)")
175
+ assert_equal "0xa01100", ReggieB.convert("rgb(160, 17, 0);")
176
+ end
177
+
178
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'redgreen'
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reggieb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Wyatt Greene
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-27 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: "\n Reggie B. converts colors between hex format and rgb format.\n It is very forgiving in the syntax it allows.\n "
17
+ email: techiferous@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/reggieb.rb
26
+ - LICENSE
27
+ - Rakefile
28
+ - README
29
+ - test/reggieb_test.rb
30
+ - test/test_helper.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/techiferous/reggieb
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements:
53
+ - none
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: A Hex/RGB color converter.
59
+ test_files:
60
+ - test/reggieb_test.rb
61
+ - test/test_helper.rb