color_helpers 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -5
- data/color_helpers.gemspec +2 -0
- data/lib/color_helpers/helpers.rb +29 -3
- data/lib/color_helpers/version.rb +1 -1
- data/lib/color_helpers.rb +1 -0
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96bf52dfd6e884df92c351adfda806a911ef2357
|
4
|
+
data.tar.gz: ad1d2be1ec83107a91e1afd732848994b9a40359
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf7ebcb123dcd996e326a26c5b10bf37192807474771db0a9801ec9bf9998a60fa012b58bc6e72c44f91ae9d94171c1b7da999236ed1fc67735c33c8a1347d24
|
7
|
+
data.tar.gz: 54b8177114c3c1a0c0b184fa40c1f78d36e80ad4984055a5b40b0c7b457c27d9971f07051d249dbfb71e225fdf26654a98656ea290148ad215737990c437107a
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# ColorHelpers
|
2
|
+
[<img src="https://badge.fury.io/rb/color_helpers.svg" alt="Gem Version" />](https://rubygems.org/gems/color_helpers)
|
2
3
|
|
3
4
|
## Installation
|
4
5
|
|
@@ -17,12 +18,29 @@ Or install it yourself as:
|
|
17
18
|
$ gem install color_helpers
|
18
19
|
|
19
20
|
## Usage
|
20
|
-
_hex_color argument accepts an input string e.g. "#ff0000"_
|
21
|
-
|
22
|
-
* **darken_color(hex_color, amount)** _amount 0-1, default: 0.4_
|
23
|
-
* **lighten_color(hex_color, amount)** _amount 0-1, default: 0.6_
|
24
|
-
* **contrasting_text_color(hex_color)**
|
25
21
|
|
22
|
+
```ruby
|
23
|
+
<%= random_hex_color %> // prints a random hex color e.g. "#ff0000"
|
24
|
+
```
|
25
|
+
```ruby
|
26
|
+
<%= hex_to_rgb(hex_color) %> // converts hex color string to rgb color string
|
27
|
+
```
|
28
|
+
```ruby
|
29
|
+
<%= rgb_to_hex(rgb_color) %> // converts rgb color string to hex color string
|
30
|
+
```
|
31
|
+
_hex_color argument accepts an input string e.g. "#ff0000"_
|
32
|
+
```ruby
|
33
|
+
<%= darken_color(hex_color, amount) %> // amount: 0 to 1, default: 0.4
|
34
|
+
```
|
35
|
+
```ruby
|
36
|
+
<%= lighten_color(hex_color, amount) %> // amount: 0 to 1, default: 0.6
|
37
|
+
```
|
38
|
+
```ruby
|
39
|
+
<%= contrasting_text_color(hex_color) %> // Options: :monochrome, default: false
|
40
|
+
```
|
41
|
+
```ruby
|
42
|
+
<%= color_to_grayscale(hex_color) %> // converts color to greyscale
|
43
|
+
```
|
26
44
|
|
27
45
|
## Development
|
28
46
|
|
data/color_helpers.gemspec
CHANGED
@@ -22,6 +22,8 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
23
|
spec.require_paths = ["lib"]
|
24
24
|
|
25
|
+
spec.add_dependency "actionview", '~> 4.2'
|
26
|
+
|
25
27
|
spec.add_development_dependency "bundler", "~> 1.8"
|
26
28
|
spec.add_development_dependency "rake", "~> 10.0"
|
27
29
|
spec.add_development_dependency "rspec"
|
@@ -1,5 +1,18 @@
|
|
1
1
|
module ColorHelpers
|
2
2
|
module ActionViewExtension
|
3
|
+
def random_hex_color
|
4
|
+
"#"+SecureRandom.hex(3)
|
5
|
+
end
|
6
|
+
|
7
|
+
def hex_to_rgb(hex_color)
|
8
|
+
color = hex_color.gsub('#','')
|
9
|
+
"rgb("+color.scan(/../).map {|color| color.to_i(16)}.join(",").to_s+")"
|
10
|
+
end
|
11
|
+
|
12
|
+
def rgb_to_hex(rgb_color)
|
13
|
+
color = rgb_color.delete("rgb() ").split(",")
|
14
|
+
"#%02x%02x%02x" % color
|
15
|
+
end
|
3
16
|
|
4
17
|
def darken_color(hex_color, amount=0.4)
|
5
18
|
hex_color = hex_color.gsub('#','')
|
@@ -9,18 +22,31 @@ module ColorHelpers
|
|
9
22
|
|
10
23
|
def lighten_color(hex_color, amount=0.6)
|
11
24
|
hex_color = hex_color.gsub('#','')
|
12
|
-
rgb = hex_color.scan(/../).map(&:hex).map{|color| (color + 255) * amount}.map
|
25
|
+
rgb = hex_color.scan(/../).map(&:hex).map{|color| (color + 255) * amount}.map{|color| [color.round, 255].min}
|
13
26
|
"#%02x%02x%02x" % rgb
|
14
27
|
end
|
15
28
|
|
16
|
-
def contrasting_text_color(hex_color)
|
29
|
+
def contrasting_text_color(hex_color, options = {})
|
17
30
|
color = hex_color.gsub('#','')
|
18
|
-
|
31
|
+
if options[:monochrome] == true
|
32
|
+
convert_to_brightness_value(color) > 382.5 ? "#000000" : "#ffffff"
|
33
|
+
else
|
34
|
+
convert_to_brightness_value(color) > 382.5 ? darken_color(color) : lighten_color(color)
|
35
|
+
end
|
19
36
|
end
|
20
37
|
|
21
38
|
def convert_to_brightness_value(hex_color)
|
22
39
|
(hex_color.scan(/../).map {|color| color.hex}).sum
|
23
40
|
end
|
41
|
+
|
42
|
+
def color_to_grayscale(hex_color)
|
43
|
+
hex_color = hex_color.gsub('#','')
|
44
|
+
rgb = hex_color.scan(/../).map(&:hex)
|
45
|
+
grayscale_factors = {r: 0.21, g: 0.72, b: 0.07}
|
46
|
+
rgb[0] = (rgb[0].to_i * grayscale_factors[:r] + rgb[1].to_i * grayscale_factors[:g] + rgb[2].to_i * grayscale_factors[:b]).round
|
47
|
+
rgb[1] = rgb[2] = rgb[0]
|
48
|
+
"#%02x%02x%02x" % rgb
|
49
|
+
end
|
24
50
|
end
|
25
51
|
end
|
26
52
|
ActionView::Base.send :include, ColorHelpers::ActionViewExtension
|
data/lib/color_helpers.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: color_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Schneider
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionview
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -93,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
107
|
version: '0'
|
94
108
|
requirements: []
|
95
109
|
rubyforge_project:
|
96
|
-
rubygems_version: 2.
|
110
|
+
rubygems_version: 2.6.3
|
97
111
|
signing_key:
|
98
112
|
specification_version: 4
|
99
113
|
summary: View helpers for hex color brightness adjustments.
|