color_helpers 0.1.2 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 97e0b6581f86ddbf23b9b42ed549e35bef32ca24
4
- data.tar.gz: a2938e01e6f8a420ee4fd00ecfdfff26286cfd2b
3
+ metadata.gz: 96bf52dfd6e884df92c351adfda806a911ef2357
4
+ data.tar.gz: ad1d2be1ec83107a91e1afd732848994b9a40359
5
5
  SHA512:
6
- metadata.gz: b3ab42f984f88553b47f8130c948e2f1d30edb93192cbd68e9ff4d9b9e93868d5f9aae0cdda4004fc66e65c2a44916634daa66b227f5f7d21be72e593dc3c448
7
- data.tar.gz: 8710fe3f0a98ae311eb2d439c4f172546f018f755a8181d0eb3faa75c2e86b49dce259a8637b56b08790688b7bb7db56b0722e6f7ebdae39f52a211b58935c11
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
 
@@ -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(&:round)
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
- convert_to_brightness_value(color) > 382.5 ? darken_color(color) : lighten_color(color)
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
@@ -1,3 +1,3 @@
1
1
  module ColorHelpers
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/color_helpers.rb CHANGED
@@ -1,2 +1,3 @@
1
+ require 'action_view'
1
2
  require 'color_helpers/version'
2
3
  require 'color_helpers/helpers'
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.1.2
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-22 00:00:00.000000000 Z
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.4.6
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.