rainbow_colors 0.3.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 678e6300d2667bcdc5f2489b9cf5e9773bf086c5
4
- data.tar.gz: d6acab94b85369acce7cfa2ab389ca82137c2e2b
3
+ metadata.gz: 97eb58ce4b49c9cc24e4388c64c5384dbaba54c9
4
+ data.tar.gz: 2c4962729f215682f31535a6585216c23dd269cb
5
5
  SHA512:
6
- metadata.gz: 631199957b99e4fb3976ee8fdcf414847e2102a065abd602ae66aa477bb4928fc94080d53553701e2fc4206bba09fa5f5266c081ac0a5eacd872a9a646b333c8
7
- data.tar.gz: 76efeeab5b3fd30487ba9a8f5a4cffed40bf17e5783ab89dbec80bd9f8536e5cdd9c12c001e71d0f383b335ede6ee93b28479761634c017ccd917f974b55596e
6
+ metadata.gz: 56d926650b9d6b5b7cb1316c175d6c627d095c1494ce563edd9653d1831b547efdb725c8ad907cf4d449c1836716c5d29c330c9a382a7ec445b1e5d7abd443bd
7
+ data.tar.gz: 1a0a77df341214e6bec280f11f9a753a4cd08140973a8e479bcd8528c0e9e5159cfe204f0b5535f784756c5cb96e1c8164b8864f7be1e4f9e0bb0b6e6c862209
@@ -1,7 +1,7 @@
1
1
  module RainbowColors
2
2
  class ImagePalette
3
3
  def initialize(image_path)
4
- @image_path = image_path
4
+ @image = Magick::ImageList.new(image_path).quantize(2048, Magick::RGBColorspace)
5
5
  end
6
6
 
7
7
  def scheme
@@ -45,24 +45,46 @@ module RainbowColors
45
45
  end
46
46
 
47
47
  def color_background
48
+ # Pick the most-used color in the image
48
49
  RainbowColors::Convertor.hex_from_rgb image_colors.first
49
50
  end
50
51
 
51
52
  def color_text
52
53
  background_rgb = RainbowColors::Convertor.rgb_from_hex color_background
54
+ # http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
53
55
  background_brightness = Math.sqrt(
54
56
  background_rgb[:red] ** 2 * 0.241 +
55
57
  background_rgb[:green] ** 2 * 0.691 +
56
58
  background_rgb[:blue] ** 2 * 0.068
57
59
  )
58
- background_brightness < 130 ? "#ffffff" : "#000000"
60
+ background_brightness < 145 ? "#E6E6E6" : "#1A1A1A"
61
+ end
62
+
63
+ def color_accent
64
+ bg = RainbowColors::Convertor.rgb_from_hex color_background
65
+ color_scheme = scheme.map { |hex| RainbowColors::Convertor.rgb_from_hex(hex) }
66
+
67
+ contrast_colors = []
68
+ color_scheme.each do |c|
69
+ delta_red = [bg[:red], c[:red]].max - [bg[:red], c[:red]].min
70
+ delta_green = [bg[:green], c[:green]].max - [bg[:green], c[:green]].min
71
+ delta_blue = [bg[:blue], c[:blue]].max - [bg[:blue], c[:blue]].min
72
+ color_difference = delta_red + delta_green + delta_blue
73
+
74
+ contrast_colors << RainbowColors::Convertor.hex_from_rgb(c) if color_difference > 225
75
+ end
76
+
77
+ # Return random contrasting color color, if available
78
+ contrast_colors.empty? ? color_text : contrast_colors.sample
59
79
  end
60
80
 
61
81
  private
62
82
 
63
83
  def image_colors
64
- image = Magick::ImageList.new(@image_path).quantize(2048, Magick::RGBColorspace)
65
- colors = image.color_histogram.sort { |a,b| b[1] <=> a[1] }
84
+ # Sort colors from most-used to least-used
85
+ colors = @image.color_histogram.sort { |a,b| b[1] <=> a[1] }
86
+
87
+ # Generate RGB values for colors
66
88
  colors.map! do |c|
67
89
  red = c.first.red / 256
68
90
  green = c.first.green / 256
@@ -1,3 +1,3 @@
1
1
  module RainbowColors
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "0.3.1".freeze
3
3
  end
data/server/server.rb CHANGED
@@ -51,6 +51,7 @@ get "/image" do
51
51
  colors = image.scheme
52
52
  pp image.color_background
53
53
  pp image.color_text
54
+ pp image.color_accent
54
55
 
55
56
  erb :image, locals: { colors: colors, image: params[:img] }
56
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rainbow_colors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ashish Kumar