coloral 0.0.1 → 0.0.2
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 +4 -4
- data/.travis.yml +2 -0
- data/Guardfile +2 -2
- data/README.md +18 -2
- data/coloral.gemspec +2 -0
- data/lib/coloral.rb +13 -8
- data/lib/coloral/color.rb +21 -0
- data/lib/coloral/model.rb +8 -0
- data/lib/coloral/models/rgb_model.rb +41 -0
- data/lib/coloral/version.rb +1 -1
- data/spec/color_spec.rb +79 -0
- data/spec/coloral_spec.rb +5 -3
- metadata +9 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6ef43a625d7e426d32a9235d87ddc1e45816478
|
4
|
+
data.tar.gz: f3760d2634cf7a1ab454a21231b4a3810d377d90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e57a6b3ba50c351398c54b312ab80776aa1da56367bc5393394b01a43775c0d7cc9c42cba6c3589cd8eb3c0830d0f7ecd6484968e8e401fa444bf3b7f8133fa
|
7
|
+
data.tar.gz: 787ba952d7fde0b540a9bd846b61d02cf2e2d2f4d4edc2c7de15ce5fca5c3f8c2535d8188e994e550160e463c5aa8e5fdc0c66858be69138050ec63be24d6d4d
|
data/.travis.yml
CHANGED
data/Guardfile
CHANGED
@@ -7,10 +7,10 @@
|
|
7
7
|
# * bundler binstubs: 'bin/rspec'
|
8
8
|
# * spring: 'bin/rsspec' (This will use spring if running and you have
|
9
9
|
# installed the spring binstubs per the docs)
|
10
|
-
# * zeus: 'zeus rspec' (requires the server to be started
|
10
|
+
# * zeus: 'zeus rspec' (requires the server to be started separatly)
|
11
11
|
# * 'just' rspec: 'rspec'
|
12
12
|
guard :rspec, cmd: 'bundle exec rspec' do
|
13
13
|
watch(%r{^spec/.+_spec\.rb$})
|
14
|
-
watch(%r{^lib/(.+)\.rb$}) { |m| "spec
|
14
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
15
15
|
watch('spec/spec_helper.rb') { "spec" }
|
16
16
|
end
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# Coloral
|
2
|
+
[](http://badge.fury.io/rb/coloral)
|
2
3
|
[](https://travis-ci.org/pducks32/coloral)
|
3
4
|
[](https://codeclimate.com/github/pducks32/coloral)
|
4
5
|
[](https://codeclimate.com/github/pducks32/coloral)
|
@@ -22,12 +23,27 @@ Or install it yourself as:
|
|
22
23
|
$ gem install coloral
|
23
24
|
|
24
25
|
## Usage
|
26
|
+
Below is the basic API for using `Coloral` with RGB and all other plugins follow a identical format substituting `rgb`. In addition to RGB `Coloral` also supports
|
25
27
|
|
26
|
-
|
28
|
+
* RGB
|
29
|
+
* HSL
|
30
|
+
* Hex
|
31
|
+
* HSB/HSV
|
32
|
+
* *and many more comming soon*
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'coloral'
|
36
|
+
Coloral.from_rgb 250, 75, 119 # Also supports array
|
37
|
+
Coloral.from_rgb red: 250, green: 75, blue: 119 # Also supports hash input
|
38
|
+
my_color = Coloral.from_rgb 250, 75, 119 #=> #<Coloral::Color @hue=345, @saturation=95, @lightness=64>
|
39
|
+
my_color.to_hex #=> "#F94A77"
|
40
|
+
my_color.to_css_rgb #=> "rgb(250, 75, 119)"s
|
41
|
+
|
42
|
+
```
|
27
43
|
|
28
44
|
## Contributing
|
29
45
|
|
30
|
-
1. Fork it ( https://github.com/
|
46
|
+
1. Fork it ( https://github.com/pducks/coloral/fork )
|
31
47
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
32
48
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
33
49
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/coloral.gemspec
CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.required_ruby_version = '~> 2.0'
|
22
|
+
|
21
23
|
spec.add_development_dependency "bundler", ">= 1.6.2"
|
22
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
25
|
spec.add_development_dependency "rspec"
|
data/lib/coloral.rb
CHANGED
@@ -1,15 +1,20 @@
|
|
1
1
|
require "coloral/version"
|
2
2
|
|
3
3
|
module Coloral
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
class Color
|
9
|
-
def initialize; end
|
4
|
+
autoload :Model, "coloral/model"
|
5
|
+
autoload :RGBModel, "coloral/models/rgb_model"
|
6
|
+
autoload :Color, "coloral/color"
|
10
7
|
|
11
|
-
|
12
|
-
|
8
|
+
def self.method_missing(meth, *args, &block)
|
9
|
+
if meth.to_s.start_with?("from", "for") && Color.respond_to?(meth)
|
10
|
+
Color.public_send(meth, *args)
|
11
|
+
else
|
12
|
+
super
|
13
13
|
end
|
14
14
|
end
|
15
|
+
|
16
|
+
def respond_to_missing?(meth, include_private = false)
|
17
|
+
meth.to_s.start_with?("from", "for") && Color.respond_to?(meth) || super
|
18
|
+
end
|
19
|
+
|
15
20
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Coloral::Color
|
2
|
+
|
3
|
+
include Coloral::RGBModel
|
4
|
+
|
5
|
+
attr_reader :hue, :saturation, :lightness
|
6
|
+
|
7
|
+
def initialize(hue, saturation, lightness)
|
8
|
+
@hue, @saturation, @lightness = hue, saturation, lightness
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.from_hex(hex_string)
|
12
|
+
new *hex_string.scan(/([0-9a-fA-F]{2})/).flatten
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def self.from_hsl(a_hue = nil, a_saturation = nil, a_lightness = nil, h: a_hue, s: a_saturation, l: a_lightness, hue: h, saturation: s, lightness: l)
|
17
|
+
raise ArgumentError if [hue, saturation, lightness].any?(&:nil?)
|
18
|
+
new hue, saturation, lightness
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Coloral
|
2
|
+
module RGBModel
|
3
|
+
extend Coloral::Model
|
4
|
+
|
5
|
+
module ConstructorMethod
|
6
|
+
def from_rgb(a_red = nil, a_green = nil, a_blue = nil, r: a_red, g: a_green, b: a_blue, red: r, green: g, blue: b)
|
7
|
+
raise ArgumentError if [red, blue, green].any?(&:nil?)
|
8
|
+
red /= 255
|
9
|
+
green /= 255
|
10
|
+
blue /= 255
|
11
|
+
min, max = [red, blue, green].minmax
|
12
|
+
delta = (max - min).to_f
|
13
|
+
lum = (max + min) / 2.0
|
14
|
+
|
15
|
+
if delta.zero? # close to 0.0, so it's a grey
|
16
|
+
hue = 0
|
17
|
+
sat = 0
|
18
|
+
else
|
19
|
+
if lum <= 0.5
|
20
|
+
sat = delta / (max + min).to_f
|
21
|
+
else
|
22
|
+
sat = delta / (2 - max - min).to_f
|
23
|
+
end
|
24
|
+
sixth = 1 / 6.0
|
25
|
+
if red == max # Color.near_zero_or_less?(@r - max)
|
26
|
+
hue = (sixth * ((green - blue) / delta))
|
27
|
+
hue += 1.0 if green < blue
|
28
|
+
elsif green == max # Color.near_zero_or_less(@g - max)
|
29
|
+
hue = (sixth * ((blue - red) / delta)) + (1.0 / 3.0)
|
30
|
+
elsif blue == max # Color.near_zero_or_less?(@b - max)
|
31
|
+
hue = (sixth * ((red - green) / delta)) + (2.0 / 3.0)
|
32
|
+
end
|
33
|
+
|
34
|
+
hue += 1 if hue < 0
|
35
|
+
hue -= 1 if hue > 1
|
36
|
+
end
|
37
|
+
new(hue, sat, lum)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/coloral/version.rb
CHANGED
data/spec/color_spec.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module Coloral
|
4
|
+
describe Color do
|
5
|
+
|
6
|
+
it "has hsl attributes" do
|
7
|
+
attributes = { hue: 220, saturation: 60, lightness: 55 }
|
8
|
+
expect(Color.from_hsl(attributes)).to have_attributes attributes
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "::from_hex" do
|
12
|
+
context "with no parameters" do
|
13
|
+
it "raises ArgumentError" do
|
14
|
+
expect{ Color.from_hex() }.to raise_error ArgumentError
|
15
|
+
end
|
16
|
+
end
|
17
|
+
context "when # is in front" do
|
18
|
+
it "returns a color" do
|
19
|
+
expect(Color.from_hex("#00FF00")).to be_a Color
|
20
|
+
end
|
21
|
+
end
|
22
|
+
context "when # is not in front" do
|
23
|
+
it "returns a color" do
|
24
|
+
expect(Color.from_hex("00FF00")).to be_a Color
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "::from_hsl" do
|
30
|
+
context "with no parameters" do
|
31
|
+
it "raises ArgumentError" do
|
32
|
+
expect{ Color.from_hsl() }.to raise_error ArgumentError
|
33
|
+
end
|
34
|
+
end
|
35
|
+
context "with no special syntax" do
|
36
|
+
it "returns a color" do
|
37
|
+
expect(Color.from_hsl(175, 50, 85)).to be_a Color
|
38
|
+
end
|
39
|
+
end
|
40
|
+
context "with hsl hash" do
|
41
|
+
it "returns a color" do
|
42
|
+
hash = { h: 175, s: 50, l: 85 }
|
43
|
+
expect(Color.from_hsl(hash)).to be_a Color
|
44
|
+
end
|
45
|
+
end
|
46
|
+
context "with hue saturation lightness hash" do
|
47
|
+
it "returns a color" do
|
48
|
+
hash = { hue: 175, saturation: 50, lightness: 85 }
|
49
|
+
expect(Color.from_hsl(hash)).to be_a Color
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "::from_rgb" do
|
55
|
+
context "with no parameters" do
|
56
|
+
it "raises ArgumentError" do
|
57
|
+
expect{ Color.from_hsl() }.to raise_error ArgumentError
|
58
|
+
end
|
59
|
+
end
|
60
|
+
context "with no special syntax" do
|
61
|
+
it "returns a color" do
|
62
|
+
expect(Color.from_rgb(200, 180, 90)).to be_a Color
|
63
|
+
end
|
64
|
+
end
|
65
|
+
context "with hsl hash" do
|
66
|
+
it "returns a color" do
|
67
|
+
hash = { r: 200, g: 180, b: 90 }
|
68
|
+
expect(Color.from_rgb(hash)).to be_a Color
|
69
|
+
end
|
70
|
+
end
|
71
|
+
context "with hue saturation lightness hash" do
|
72
|
+
it "returns a color" do
|
73
|
+
hash = { red: 200, green: 180, blue: 90 }
|
74
|
+
expect(Color.from_rgb(hash)).to be_a Color
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/spec/coloral_spec.rb
CHANGED
@@ -2,9 +2,11 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
module Coloral
|
4
4
|
describe Coloral do
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
|
6
|
+
context "with 'from_*' or 'for_*' method" do
|
7
|
+
it "delegates to Color" do
|
8
|
+
expect(Color).to receive(:from_hex)
|
9
|
+
Coloral.from_hex("#00FF00")
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coloral
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Metcalfe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10
|
11
|
+
date: 2014-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,7 +71,11 @@ files:
|
|
71
71
|
- Rakefile
|
72
72
|
- coloral.gemspec
|
73
73
|
- lib/coloral.rb
|
74
|
+
- lib/coloral/color.rb
|
75
|
+
- lib/coloral/model.rb
|
76
|
+
- lib/coloral/models/rgb_model.rb
|
74
77
|
- lib/coloral/version.rb
|
78
|
+
- spec/color_spec.rb
|
75
79
|
- spec/coloral_spec.rb
|
76
80
|
- spec/spec_helper.rb
|
77
81
|
homepage: ''
|
@@ -84,9 +88,9 @@ require_paths:
|
|
84
88
|
- lib
|
85
89
|
required_ruby_version: !ruby/object:Gem::Requirement
|
86
90
|
requirements:
|
87
|
-
- - "
|
91
|
+
- - "~>"
|
88
92
|
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
93
|
+
version: '2.0'
|
90
94
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
95
|
requirements:
|
92
96
|
- - ">="
|
@@ -100,6 +104,7 @@ specification_version: 4
|
|
100
104
|
summary: The best way to create, convert, or manipulate colors in Ruby using helpful
|
101
105
|
color functions.
|
102
106
|
test_files:
|
107
|
+
- spec/color_spec.rb
|
103
108
|
- spec/coloral_spec.rb
|
104
109
|
- spec/spec_helper.rb
|
105
110
|
has_rdoc:
|