color_porter 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.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/LICENSE +22 -0
- data/README.md +58 -0
- data/color_porter.gemspec +20 -0
- data/lib/.DS_Store +0 -0
- data/lib/color_porter/hex.rb +45 -0
- data/lib/color_porter/rgb.rb +46 -0
- data/lib/color_porter/version.rb +3 -0
- data/lib/color_porter.rb +22 -0
- data/spec/color_porter_spec.rb +104 -0
- data/spec/spec_helper.rb +21 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA512:
|
3
|
+
data.tar.gz: 494204b013a17898b7acc1e42d09c92483a4401afa6cf8b6c440f4473094173ad3a305e84a6c10cb4d13eb3200a37c322f5f584c03235437db71399a4dcfc59f
|
4
|
+
metadata.gz: b7c2c59a57d01723ee9f80977c242a2104650d095798e4636bb2680ecf371ae601f16b8c8db9e0766f77446820b67d01edf9fa0b4dea25f54ef73e197f3d205c
|
5
|
+
SHA1:
|
6
|
+
data.tar.gz: 51d835aa4618c46dc89caf0fdf3d4a731e789b36
|
7
|
+
metadata.gz: e5e9e44c269924d249bb1409a7b6ab853a1ad75a
|
data/.rspec
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Nathan Bertram
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# ColorPorter
|
2
|
+
|
3
|
+
The easy way to work with with RGB and Hex values in ruby. This code was written during the [48 hour Rails Rumble 13 contest](http://railsrumble.com/) and has been extracted from the code base of [our entry (elementcss.com)](http://railsrumble.com/entries/124-elementcss).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'color_porter'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Parse a string and color porter will return an RGB or Hex object if a match is found
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
ColorPorter.parse('#ccc') # => #<ColorPorter::Hex @code="cccccc">
|
23
|
+
ColorPorter.parse('rgb(204,204,204)') # => #<ColorPorter::RGB @blue=204, @green=204, @red=204>
|
24
|
+
```
|
25
|
+
|
26
|
+
Do conversions between RGB and Hex
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
hex = ColorPorter::Hex.new('cccccc')
|
30
|
+
rgb = hex.to_rgb # => #<ColorPorter::RGB @blue=204, @green=204, @red=204>
|
31
|
+
rgb.to_s # => "rgb(204,204,204)"
|
32
|
+
|
33
|
+
hex = rgb.to_hex # => #<ColorPorter::Hex @code="cccccc">
|
34
|
+
hex.to_s # => "#cccccc"
|
35
|
+
```
|
36
|
+
|
37
|
+
Grab the luminosity (brightness) of a RGB or Hex value.
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
hex = ColorPorter::Hex.new('cccccc')
|
41
|
+
hex.luminosity # => 13421772
|
42
|
+
|
43
|
+
rgb = ColorPorter::RGB.new(204,204,204)
|
44
|
+
rgb.luminosity # => 13421772
|
45
|
+
```
|
46
|
+
|
47
|
+
## Author
|
48
|
+
[Nathan Bertram](https://github.com/nathanbertram)
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Ensure tests are passing with `rspec`
|
55
|
+
4. Commit your changes (`git commit -am 'Added some feature'`)
|
56
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
6. Create new Pull Request
|
58
|
+
7. You da man!
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/color_porter/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Nathan Bertram"]
|
6
|
+
gem.email = ["nbertram@gmail.com"]
|
7
|
+
gem.description = %q{Color Porter does the heavy lifting when working Hex & RGB in Ruby}
|
8
|
+
gem.summary = %q{Work directly with Hex & RGB objects in Ruby}
|
9
|
+
gem.homepage = "http://github.com/nathanbertram/color_porter"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "color_porter"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ColorPorter::VERSION
|
17
|
+
gem.license = 'MIT'
|
18
|
+
|
19
|
+
gem.add_development_dependency('rspec')
|
20
|
+
end
|
data/lib/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module ColorPorter
|
2
|
+
class Hex
|
3
|
+
attr_accessor :code
|
4
|
+
|
5
|
+
def initialize(code='')
|
6
|
+
code = code.to_s.gsub('#', '')
|
7
|
+
@code = (code.length == 3 ? code*2 : code).gsub!(/[\W_]*/, '')
|
8
|
+
@code = nil unless valid?
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
@code.nil? ? false : @code.size == 6
|
13
|
+
end
|
14
|
+
|
15
|
+
def luminosity
|
16
|
+
@code.hex
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
"##{@code.to_s}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_hex
|
24
|
+
return self
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_rgb
|
28
|
+
hex_str_rev = @code.each_char.to_a.reverse.join
|
29
|
+
if hex_str_rev.size == 3
|
30
|
+
hex_str_rev = hex_str_rev[0] + hex_str_rev[0] + hex_str_rev[1] + hex_str_rev[1] + hex_str_rev[2] + hex_str_rev[2]
|
31
|
+
elsif hex_str_rev.size != 6
|
32
|
+
return nil
|
33
|
+
end
|
34
|
+
|
35
|
+
hex = hex_str_rev.to_s.hex
|
36
|
+
rgb = {}
|
37
|
+
%w(r g b).inject(hex) {|a,i| rest, rgb[i] = a.divmod 256; rest}
|
38
|
+
red = rgb["r"].to_s
|
39
|
+
green = rgb["g"].to_s
|
40
|
+
blue = rgb["b"].to_s
|
41
|
+
|
42
|
+
RGB.new(red, green, blue)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ColorPorter
|
2
|
+
class RGB
|
3
|
+
attr_accessor :red, :green, :blue
|
4
|
+
|
5
|
+
def initialize(red, green, blue)
|
6
|
+
@red, @green, @blue = red.to_i, green.to_i, blue.to_i
|
7
|
+
@red = 0 unless (0..255).include?(@red)
|
8
|
+
@green = 0 unless (0..255).include?(@green)
|
9
|
+
@blue = 0 unless (0..255).include?(@blue)
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?
|
13
|
+
(0..255).include?(@red) && (0..255).include?(@green) && (0..255).include?(@blue)
|
14
|
+
end
|
15
|
+
|
16
|
+
def luminosity
|
17
|
+
to_hex.luminosity
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
"rgb(#{@red},#{@green},#{@blue})"
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_rgb
|
25
|
+
return self
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_hex
|
29
|
+
r, g, b = @red.to_s(16), @green.to_s(16), @blue.to_s(16)
|
30
|
+
|
31
|
+
if r.size < 2
|
32
|
+
r = "0" + r
|
33
|
+
end
|
34
|
+
|
35
|
+
if g.size < 2
|
36
|
+
g = "0" + g
|
37
|
+
end
|
38
|
+
|
39
|
+
if b.size < 2
|
40
|
+
b = "0" + b
|
41
|
+
end
|
42
|
+
|
43
|
+
return Hex.new("##{r}#{g}#{b}")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/color_porter.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "color_porter/version"
|
2
|
+
|
3
|
+
module ColorPorter
|
4
|
+
CSS_RGB_REGEX = /rgb\((?<r>\d{1,3}),(?<g>\d{1,3}),(?<b>\d{1,3})\)/
|
5
|
+
CSS_HEX_REGEX = /([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?\b/
|
6
|
+
|
7
|
+
def self.parse(color_string)
|
8
|
+
# Consider Implementing: parsing values that come in as red, green, blue, pink and convert to Hex value
|
9
|
+
# as browsers parse them.
|
10
|
+
|
11
|
+
if color_string =~ CSS_RGB_REGEX
|
12
|
+
rgb_data = color_string.match(CSS_RGB_REGEX)
|
13
|
+
RGB.new(rgb_data[:r], rgb_data[:g], rgb_data[:b])
|
14
|
+
elsif color_string =~ CSS_HEX_REGEX
|
15
|
+
Hex.new(color_string)
|
16
|
+
else
|
17
|
+
return nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ColorPorter do
|
4
|
+
|
5
|
+
describe "parse" do
|
6
|
+
it "should be able to parse out hex values" do
|
7
|
+
ColorPorter.parse('#cdcdcd').class.should == ColorPorter::Hex
|
8
|
+
ColorPorter.parse('#cdcdcd').to_s.should == '#cdcdcd'
|
9
|
+
|
10
|
+
ColorPorter.parse('cccccc').class.should == ColorPorter::Hex
|
11
|
+
ColorPorter.parse('cccccc').to_s.should == '#cccccc'
|
12
|
+
|
13
|
+
ColorPorter.parse('ccc').class.should == ColorPorter::Hex
|
14
|
+
ColorPorter.parse('ccc').to_s.should == '#cccccc'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be able to parse out rgb values" do
|
18
|
+
ColorPorter.parse("rgb(5,5,5)").class.should == ColorPorter::RGB
|
19
|
+
ColorPorter.parse("rgb(5,5,5)").to_s.should == "rgb(5,5,5)"
|
20
|
+
|
21
|
+
ColorPorter.parse("rgb(55,55,55)").class.should == ColorPorter::RGB
|
22
|
+
ColorPorter.parse("rgb(55,55,55)").to_s.should == "rgb(55,55,55)"
|
23
|
+
|
24
|
+
ColorPorter.parse("rgb(120,121,123)").class.should == ColorPorter::RGB
|
25
|
+
ColorPorter.parse("rgb(120,121,123)").to_s.should == "rgb(120,121,123)"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return nil if no match is found" do
|
29
|
+
ColorPorter.parse("#internet").should == nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe ColorPorter::Hex do
|
34
|
+
describe "initialize" do
|
35
|
+
it "it should initialize with a valid hex value" do
|
36
|
+
ColorPorter::Hex.new('ccc').to_s.should == '#cccccc'
|
37
|
+
ColorPorter::Hex.new('cccccc').to_s.should == '#cccccc'
|
38
|
+
ColorPorter::Hex.new('#cccccc').to_s.should == '#cccccc'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "valid?" do
|
43
|
+
it "should return true/false if valid" do
|
44
|
+
ColorPorter::Hex.new('#cccccc').valid?.should == true
|
45
|
+
ColorPorter::Hex.new('not valid').valid?.should == false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "luminosity" do
|
50
|
+
it "should return the value for luminosity" do
|
51
|
+
ColorPorter::Hex.new('#cccccc').luminosity.should == 13421772
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "to_s" do
|
56
|
+
it "should return a string value" do
|
57
|
+
ColorPorter::Hex.new('#cccccc').to_s.should == '#cccccc'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "to_rgb" do
|
62
|
+
it "should convert to an RGB value" do
|
63
|
+
rgb = ColorPorter::Hex.new('#cccccc').to_rgb
|
64
|
+
rgb.class.should == ColorPorter::RGB
|
65
|
+
rgb.to_s.should == "rgb(204,204,204)"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
describe ColorPorter::RGB do
|
72
|
+
describe "initialize" do
|
73
|
+
it "it should initialize with a valid rgb value" do
|
74
|
+
ColorPorter::RGB.new(100,100,100).to_s.should == "rgb(100,100,100)"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "valid?" do
|
79
|
+
it "should parse a hex string" do
|
80
|
+
ColorPorter::RGB.new(120,120,121).valid?.should == true
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "luminosity" do
|
85
|
+
it "should return the luminosity" do
|
86
|
+
ColorPorter::RGB.new(120,120,121).luminosity.should == 7895161
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "to_s" do
|
91
|
+
it "should return a string rgb" do
|
92
|
+
ColorPorter::RGB.new(120,120,121).to_s.should == "rgb(120,120,121)"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "to_hex" do
|
97
|
+
it "should return a hex value" do
|
98
|
+
ColorPorter::RGB.new(120,120,121).to_hex.class.should == ColorPorter::Hex
|
99
|
+
ColorPorter::RGB.new(120,120,121).to_hex.to_s.should == "#787879"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.expand_path('../../lib/color_porter', __FILE__)
|
2
|
+
require File.expand_path('../../lib/color_porter/hex', __FILE__)
|
3
|
+
require File.expand_path('../../lib/color_porter/rgb', __FILE__)
|
4
|
+
|
5
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
6
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
7
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
8
|
+
# loaded once.
|
9
|
+
#
|
10
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
13
|
+
config.run_all_when_everything_filtered = true
|
14
|
+
config.filter_run :focus
|
15
|
+
|
16
|
+
# Run specs in random order to surface order dependencies. If you find an
|
17
|
+
# order dependency and want to debug it, you can fix the order by providing
|
18
|
+
# the seed, which is printed after each run.
|
19
|
+
# --seed 1234
|
20
|
+
config.order = 'random'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: color_porter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nathan Bertram
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2013-10-22 00:00:00 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
prerelease: false
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- &id002
|
20
|
+
- ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
type: :development
|
24
|
+
version_requirements: *id001
|
25
|
+
description: Color Porter does the heavy lifting when working Hex & RGB in Ruby
|
26
|
+
email:
|
27
|
+
- nbertram@gmail.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files: []
|
33
|
+
|
34
|
+
files:
|
35
|
+
- .rspec
|
36
|
+
- LICENSE
|
37
|
+
- README.md
|
38
|
+
- color_porter.gemspec
|
39
|
+
- lib/.DS_Store
|
40
|
+
- lib/color_porter.rb
|
41
|
+
- lib/color_porter/hex.rb
|
42
|
+
- lib/color_porter/rgb.rb
|
43
|
+
- lib/color_porter/version.rb
|
44
|
+
- spec/color_porter_spec.rb
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
homepage: http://github.com/nathanbertram/color_porter
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
metadata: {}
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- *id002
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- *id002
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 2.1.9
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: Work directly with Hex & RGB objects in Ruby
|
69
|
+
test_files:
|
70
|
+
- spec/color_porter_spec.rb
|
71
|
+
- spec/spec_helper.rb
|