coloruby 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/Gemfile +3 -0
- data/Gemfile.lock +31 -0
- data/README.md +59 -0
- data/Rakefile +7 -0
- data/coloruby.gemspec +19 -0
- data/lib/coloruby.rb +48 -0
- data/spec/coloruby_spec.rb +36 -0
- data/spec/spec_helper.rb +1 -0
- metadata +81 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 7f46540692142996ecac7093def726a1245fb990
|
|
4
|
+
data.tar.gz: 1be384d8e0aa8a14404a6cd61ca6e87a9aaece08
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: fb1f7ec363291066e2cf92ad870dc2e1c68d54edab7e824f153f9bae652db59d1d8d318fd1e96a2d2aab7eff3721abd391b139583d63e3e83aede4c33b27647e
|
|
7
|
+
data.tar.gz: b1a446adeedbd75ddab2cb0f6e78c6a84b299b448330c91997c36497e34769aec1338bd29e5479fd8cac9d956297bf66d00ce6161e63c87bbbd8767f120f0df9
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
coloruby (0.0.1)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: http://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
diff-lcs (1.2.5)
|
|
10
|
+
rake (10.4.2)
|
|
11
|
+
rspec (3.2.0)
|
|
12
|
+
rspec-core (~> 3.2.0)
|
|
13
|
+
rspec-expectations (~> 3.2.0)
|
|
14
|
+
rspec-mocks (~> 3.2.0)
|
|
15
|
+
rspec-core (3.2.3)
|
|
16
|
+
rspec-support (~> 3.2.0)
|
|
17
|
+
rspec-expectations (3.2.1)
|
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
19
|
+
rspec-support (~> 3.2.0)
|
|
20
|
+
rspec-mocks (3.2.1)
|
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
22
|
+
rspec-support (~> 3.2.0)
|
|
23
|
+
rspec-support (3.2.2)
|
|
24
|
+
|
|
25
|
+
PLATFORMS
|
|
26
|
+
ruby
|
|
27
|
+
|
|
28
|
+
DEPENDENCIES
|
|
29
|
+
coloruby!
|
|
30
|
+
rake
|
|
31
|
+
rspec
|
data/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Coloruby
|
|
2
|
+
|
|
3
|
+
A set of color helpers for ruby.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
gem "coloruby"
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install coloruby
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
To lighten a color;
|
|
22
|
+
|
|
23
|
+
Coloruby.lighten("#000000")
|
|
24
|
+
=> "#4d4d4d"
|
|
25
|
+
|
|
26
|
+
To darken a color;
|
|
27
|
+
|
|
28
|
+
Coloruby.darken("#990000")
|
|
29
|
+
=> "#2e0000"
|
|
30
|
+
|
|
31
|
+
By default it will lighten or darken the color by 30%. You can pass in a different amount if you wish. This can be any number between 0.0 and 1.
|
|
32
|
+
|
|
33
|
+
Coloruby.lighten("#000000", 1)
|
|
34
|
+
=> "#ffffff"
|
|
35
|
+
|
|
36
|
+
There is also a helper to check if a color is a light or a dark shade.
|
|
37
|
+
|
|
38
|
+
Coloruby.light_or_dark?("#000000")
|
|
39
|
+
=> "dark"
|
|
40
|
+
|
|
41
|
+
Coloruby.light?("#000000")
|
|
42
|
+
=> false
|
|
43
|
+
|
|
44
|
+
Coloruby.dark?("#000000")
|
|
45
|
+
=> true
|
|
46
|
+
|
|
47
|
+
There is also a couple of converter helpers;
|
|
48
|
+
|
|
49
|
+
Coloruby.hex_to_rgb("#ffffff")
|
|
50
|
+
=> [255, 255, 255]
|
|
51
|
+
|
|
52
|
+
Coloruby.hex_to_rgb("#990000")
|
|
53
|
+
=> [153, 0, 0]
|
|
54
|
+
|
|
55
|
+
Coloruby.rgb_to_hex([255, 255, 255])
|
|
56
|
+
=> "ffffff"
|
|
57
|
+
|
|
58
|
+
Coloruby.rgb_to_hex([153, 0, 0])
|
|
59
|
+
=> "990000"
|
data/Rakefile
ADDED
data/coloruby.gemspec
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |gem|
|
|
4
|
+
gem.authors = ["Craig Sheen"]
|
|
5
|
+
gem.email = ["craig_sheen@hotmail.com"]
|
|
6
|
+
gem.description = %q{Colour helpers for ruby}
|
|
7
|
+
gem.summary = %q{Colour helpers for ruby}
|
|
8
|
+
gem.homepage = ""
|
|
9
|
+
gem.files = `git ls-files`.split($\)
|
|
10
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
11
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
12
|
+
gem.name = "coloruby"
|
|
13
|
+
gem.require_paths = ["lib"]
|
|
14
|
+
gem.version = "0.0.1"
|
|
15
|
+
|
|
16
|
+
gem.add_development_dependency 'rake'
|
|
17
|
+
gem.add_development_dependency 'rspec'
|
|
18
|
+
|
|
19
|
+
end
|
data/lib/coloruby.rb
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
class Coloruby
|
|
2
|
+
|
|
3
|
+
def self.lighten(hex_color, amount=0.3)
|
|
4
|
+
hex_color = hex_color.gsub('#','')
|
|
5
|
+
rgb = hex_color.scan(/../).map { |color| color.hex }
|
|
6
|
+
rgb[0] = [(rgb[0].to_i + 255 * amount).round, 255].min
|
|
7
|
+
rgb[1] = [(rgb[1].to_i + 255 * amount).round, 255].min
|
|
8
|
+
rgb[2] = [(rgb[2].to_i + 255 * amount).round, 255].min
|
|
9
|
+
"#%02x%02x%02x" % rgb
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.darken(hex_color, amount=0.3)
|
|
13
|
+
hex_color = hex_color.gsub('#','')
|
|
14
|
+
rgb = hex_color.scan(/../).map { |color| color.hex }
|
|
15
|
+
rgb[0] = (rgb[0].to_i * amount).round
|
|
16
|
+
rgb[1] = (rgb[1].to_i * amount).round
|
|
17
|
+
rgb[2] = (rgb[2].to_i * amount).round
|
|
18
|
+
"#%02x%02x%02x" % rgb
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.hex_to_rgb(hex_color)
|
|
22
|
+
hex_color = hex_color.gsub('#','')
|
|
23
|
+
hex_color.scan(/../).map { |color| color.hex }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.rgb_to_hex(rgb_color)
|
|
27
|
+
'#'+rgb_color.map { |color| color < 10 ? "0#{color.to_s(16)}" : color.to_s(16) }.join("")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.light_or_dark?(hex, dark_limit=0.5)
|
|
31
|
+
rgb = hex.match /#(..)(..)(..)/
|
|
32
|
+
brightness = Math.sqrt(0.241 * rgb[1].hex**2 + 0.691 * rgb[2].hex**2 + 0.068 * rgb[3].hex**2) / 255
|
|
33
|
+
if brightness > dark_limit
|
|
34
|
+
"light"
|
|
35
|
+
else
|
|
36
|
+
"dark"
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.dark?(hex)
|
|
41
|
+
light_or_dark?(hex) == "dark"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.light?(hex)
|
|
45
|
+
light_or_dark?(hex) == "light"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Coloruby do
|
|
4
|
+
|
|
5
|
+
it '#lighten' do
|
|
6
|
+
expect(Coloruby.lighten('#000000')).to eql('#4d4d4d')
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it '#darken' do
|
|
10
|
+
expect(Coloruby.darken('#990000')).to eql('#2e0000')
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it '#hex_to_rgb' do
|
|
14
|
+
expect(Coloruby.hex_to_rgb('#ffffff')).to eql([255, 255, 255])
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it '#rgb_to_hex' do
|
|
18
|
+
expect(Coloruby.rgb_to_hex([255, 255, 255])).to eql('#ffffff')
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it '#light_or_dark?' do
|
|
22
|
+
expect(Coloruby.light_or_dark?('#000000')).to eql('dark')
|
|
23
|
+
expect(Coloruby.light_or_dark?('#ffffff')).to eql('light')
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it '#dark?' do
|
|
27
|
+
expect(Coloruby.dark?('#000000')).to eql(true)
|
|
28
|
+
expect(Coloruby.dark?('#ffffff')).to eql(false)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it '#light?' do
|
|
32
|
+
expect(Coloruby.light?('#ffffff')).to eql(true)
|
|
33
|
+
expect(Coloruby.light?('#000000')).to eql(false)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'coloruby'
|
metadata
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: coloruby
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Craig Sheen
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-06-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rake
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
description: Colour helpers for ruby
|
|
42
|
+
email:
|
|
43
|
+
- craig_sheen@hotmail.com
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- Gemfile
|
|
49
|
+
- Gemfile.lock
|
|
50
|
+
- README.md
|
|
51
|
+
- Rakefile
|
|
52
|
+
- coloruby.gemspec
|
|
53
|
+
- lib/coloruby.rb
|
|
54
|
+
- spec/coloruby_spec.rb
|
|
55
|
+
- spec/spec_helper.rb
|
|
56
|
+
homepage: ''
|
|
57
|
+
licenses: []
|
|
58
|
+
metadata: {}
|
|
59
|
+
post_install_message:
|
|
60
|
+
rdoc_options: []
|
|
61
|
+
require_paths:
|
|
62
|
+
- lib
|
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - ">="
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '0'
|
|
73
|
+
requirements: []
|
|
74
|
+
rubyforge_project:
|
|
75
|
+
rubygems_version: 2.4.5
|
|
76
|
+
signing_key:
|
|
77
|
+
specification_version: 4
|
|
78
|
+
summary: Colour helpers for ruby
|
|
79
|
+
test_files:
|
|
80
|
+
- spec/coloruby_spec.rb
|
|
81
|
+
- spec/spec_helper.rb
|