rainbow_colors 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eef2a6e2c5b2657ba71ea5fb0df4e6e1561aa247
4
- data.tar.gz: ff5c14a4e78f696c016cef3a2569fccc555e7c9d
3
+ metadata.gz: 678e6300d2667bcdc5f2489b9cf5e9773bf086c5
4
+ data.tar.gz: d6acab94b85369acce7cfa2ab389ca82137c2e2b
5
5
  SHA512:
6
- metadata.gz: 234a9f0eb7191dbc793a46558aad9de4cdf731b2ba7cb31db90d4c04342876e26b324bba77361ae785bedaef7ba96935a57e9c9adac26416cda8b7d93d30eade
7
- data.tar.gz: 1a0d05cdc486988e1c05ae85f168034fb08084ca9b438d9fa2c9a571c516e447b9e3121e141b466a06933b76b2f7b793461113aaf37f2e9d70accc6f68297b02
6
+ metadata.gz: 631199957b99e4fb3976ee8fdcf414847e2102a065abd602ae66aa477bb4928fc94080d53553701e2fc4206bba09fa5f5266c081ac0a5eacd872a9a646b333c8
7
+ data.tar.gz: 76efeeab5b3fd30487ba9a8f5a4cffed40bf17e5783ab89dbec80bd9f8536e5cdd9c12c001e71d0f383b335ede6ee93b28479761634c017ccd917f974b55596e
@@ -0,0 +1,74 @@
1
+ module RainbowColors
2
+ class ImagePalette
3
+ def initialize(image_path)
4
+ @image_path = image_path
5
+ end
6
+
7
+ def scheme
8
+ colors = image_colors
9
+ colors.map! { |rgb| RainbowColors::Convertor.hsl_from_rgb(rgb) }
10
+
11
+ # Round off HSL values to nearest 10. We don't need to worry about precision
12
+ colors.map! { |color_hsl| color_hsl.each { |key, value| color_hsl[key] = value.round(-1).to_f } }
13
+
14
+ # Remove colors that are too dark or too light
15
+ colors = colors.delete_if { |color_hsl| color_hsl[:saturation] <= 20 || color_hsl[:luminance] <= 20 || color_hsl[:luminance] >= 70 }
16
+
17
+ average_colors = []
18
+ # Get average color from hue ranges
19
+ # 12 * 30 = 360, i.e. all possible hues
20
+ 12.times do |i|
21
+ next if i == 0
22
+
23
+ hue_floor = (i - 1) * 30
24
+ hue_ceil = i * 30
25
+ colors_for_range = colors.select { |color_hsl| color_hsl[:hue] >= hue_floor && color_hsl[:hue] < hue_ceil }
26
+
27
+ unless colors_for_range.empty?
28
+ hue_sum = saturation_sum = luminance_sum = 0
29
+
30
+ colors_for_range.each do |color_hsl|
31
+ hue_sum = hue_sum + color_hsl[:hue]
32
+ saturation_sum = saturation_sum + color_hsl[:saturation]
33
+ luminance_sum = luminance_sum + color_hsl[:luminance]
34
+ end
35
+
36
+ hue = hue_sum / colors_for_range.count
37
+ saturation = saturation_sum / colors_for_range.count
38
+ luminance = luminance_sum / colors_for_range.count
39
+
40
+ average_colors << { hue: hue.to_f, saturation: saturation.to_f, luminance: luminance.to_f }
41
+ end
42
+ end
43
+
44
+ average_colors.map! { |color_hsl| RainbowColors::Convertor.hex_from_hsl color_hsl }
45
+ end
46
+
47
+ def color_background
48
+ RainbowColors::Convertor.hex_from_rgb image_colors.first
49
+ end
50
+
51
+ def color_text
52
+ background_rgb = RainbowColors::Convertor.rgb_from_hex color_background
53
+ background_brightness = Math.sqrt(
54
+ background_rgb[:red] ** 2 * 0.241 +
55
+ background_rgb[:green] ** 2 * 0.691 +
56
+ background_rgb[:blue] ** 2 * 0.068
57
+ )
58
+ background_brightness < 130 ? "#ffffff" : "#000000"
59
+ end
60
+
61
+ private
62
+
63
+ 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] }
66
+ colors.map! do |c|
67
+ red = c.first.red / 256
68
+ green = c.first.green / 256
69
+ blue = c.first.blue / 256
70
+ { red: red, green: green, blue: blue }
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,9 +1,5 @@
1
1
  module RainbowColors
2
2
  class Palette
3
- def initialize(primary_color_hex)
4
- @primary_color = primary_color_hex
5
- end
6
-
7
3
  def self.random_color
8
4
  red = rand(255)
9
5
  green = rand(255)
@@ -12,131 +8,16 @@ module RainbowColors
12
8
  RainbowColors::Convertor.hex_from_rgb({ red: red, green: green, blue: blue })
13
9
  end
14
10
 
15
- def analogous
16
- secondary = color_with_variance 30
17
- tertiary = color_with_variance -30
18
-
19
- [@primary_color, secondary, tertiary]
20
- end
21
-
22
- def complementary
23
- secondary = color_with_variance 180
24
-
25
- [@primary_color, secondary]
26
- end
27
-
28
- def complementary_split
29
- secondary = color_with_variance 150
30
- tertiary = color_with_variance 210
31
-
32
- [@primary_color, secondary, tertiary]
33
- end
34
-
35
- def triad
36
- secondary = color_with_variance 120
37
- tertiary = color_with_variance -120
38
-
39
- [@primary_color, secondary, tertiary]
40
- end
41
-
42
- def tints
43
- shades_tints "#ffffff"
44
- end
45
-
46
- def shades
47
- shades_tints "#000000"
48
- end
49
-
50
- def self.image_palette(image_path)
51
- image = Magick::ImageList.new(image_path).quantize(256, Magick::RGBColorspace)
52
- colors = image.color_histogram.sort { |a, b| b[1] <=> a[1] }
53
-
11
+ def self.adjust_saturation(colors, variance)
54
12
  colors.map! do |c|
55
- red = c.first.red / 256
56
- green = c.first.green / 256
57
- blue = c.first.blue / 256
58
-
59
- RainbowColors::Convertor.hsl_from_rgb({ red: red, green: green, blue: blue })
60
- end
61
-
62
- # Round off HSL values to nearest 10. We don't need to worry about precision
63
- colors.map! { |color_hsl| color_hsl.each { |key, value| color_hsl[key] = value.round(-1).to_f } }
64
-
65
- # Remove colors that are too dark or too light
66
- colors = colors.delete_if { |color_hsl| color_hsl[:saturation] <= 20 || color_hsl[:luminance] <= 20 || color_hsl[:luminance] >= 70 }
67
-
68
- average_colors = []
69
- # Get average color from hue ranges
70
- # 12 * 30 = 360, i.e. all possible hues
71
- 12.times do |i|
72
- next if i == 0
73
-
74
- hue_floor = (i - 1) * 30
75
- hue_ceil = i * 30
76
- colors_for_range = colors.select { |color_hsl| color_hsl[:hue] >= hue_floor && color_hsl[:hue] <= hue_ceil }
77
-
78
- unless colors_for_range.empty?
79
- hue_sum = saturation_sum = luminance_sum = 0
80
-
81
- colors_for_range.each do |color_hsl|
82
- hue_sum = hue_sum + color_hsl[:hue]
83
- saturation_sum = saturation_sum + color_hsl[:saturation]
84
- luminance_sum = luminance_sum + color_hsl[:luminance]
85
- end
86
-
87
- hue = hue_sum / colors_for_range.count
88
- saturation = saturation_sum / colors_for_range.count
89
- luminance = luminance_sum / colors_for_range.count
90
-
91
- average_colors << { hue: hue.to_f, saturation: saturation.to_f, luminance: luminance.to_f }
13
+ hsl = RainbowColors::Convertor.hsl_from_hex c
14
+ if variance > 0
15
+ hsl[:saturation] = hsl[:saturation] + variance if hsl[:saturation] + variance <= 100
16
+ else
17
+ hsl[:saturation] = hsl[:saturation] + variance if hsl[:saturation] + variance >= 0
92
18
  end
19
+ RainbowColors::Convertor.hex_from_hsl hsl
93
20
  end
94
-
95
- average_colors.map! { |color_hsl| RainbowColors::Convertor.hex_from_hsl color_hsl }
96
- end
97
-
98
- private
99
-
100
- def color_with_variance(variance)
101
- rgb = RainbowColors::Convertor.rgb_from_hex @primary_color
102
- hsl = RainbowColors::Convertor.hsl_from_rgb rgb
103
-
104
- color = RainbowColors::Convertor.rgb_from_hsl({ hue: hsl[:hue] + variance, saturation: hsl[:saturation], luminance: hsl[:luminance] })
105
-
106
- RainbowColors::Convertor.hex_from_rgb color
107
- end
108
-
109
- # Algorithm shamelessly stolen from SASS:
110
- # https://github.com/sass/sass/blob/stable/lib/sass/script/functions.rb#L1313
111
- def mix(color1, color2, weight)
112
- color1 = RainbowColors::Convertor.rgb_from_hex color1
113
- color2 = RainbowColors::Convertor.rgb_from_hex color2
114
-
115
- p = weight / 100.0
116
- w = p * 2 - 1
117
-
118
- w1 = (w + 1) / 2
119
- w2 = 1 - w1
120
-
121
- r = (color1[:red] * w1 + color2[:red] * w2).round
122
- g = (color1[:green] * w1 + color2[:green] * w2).round
123
- b = (color1[:blue] * w1 + color2[:blue] * w2).round
124
-
125
- RainbowColors::Convertor.hex_from_rgb({ red: r, green: g, blue: b })
126
- end
127
-
128
- def shades_tints(mix_color)
129
- colors = [@primary_color]
130
-
131
- weight = 0
132
- increment = 15
133
-
134
- 4.times do
135
- weight = weight + increment
136
- colors.push mix(mix_color, @primary_color, weight)
137
- end
138
-
139
- colors
140
21
  end
141
22
  end
142
23
  end
@@ -0,0 +1,86 @@
1
+ module RainbowColors
2
+ class Scheme
3
+ def initialize(primary_color_hex)
4
+ @primary_color = primary_color_hex
5
+ end
6
+
7
+ def analogous
8
+ secondary = color_with_variance 30
9
+ tertiary = color_with_variance -30
10
+
11
+ [@primary_color, secondary, tertiary]
12
+ end
13
+
14
+ def complementary
15
+ secondary = color_with_variance 180
16
+
17
+ [@primary_color, secondary]
18
+ end
19
+
20
+ def complementary_split
21
+ secondary = color_with_variance 150
22
+ tertiary = color_with_variance 210
23
+
24
+ [@primary_color, secondary, tertiary]
25
+ end
26
+
27
+ def triad
28
+ secondary = color_with_variance 120
29
+ tertiary = color_with_variance -120
30
+
31
+ [@primary_color, secondary, tertiary]
32
+ end
33
+
34
+ def tints
35
+ shades_tints "#ffffff"
36
+ end
37
+
38
+ def shades
39
+ shades_tints "#000000"
40
+ end
41
+
42
+ private
43
+
44
+ def color_with_variance(variance)
45
+ rgb = RainbowColors::Convertor.rgb_from_hex @primary_color
46
+ hsl = RainbowColors::Convertor.hsl_from_rgb rgb
47
+
48
+ color = RainbowColors::Convertor.rgb_from_hsl({ hue: hsl[:hue] + variance, saturation: hsl[:saturation], luminance: hsl[:luminance] })
49
+
50
+ RainbowColors::Convertor.hex_from_rgb color
51
+ end
52
+
53
+ # Algorithm shamelessly stolen from SASS:
54
+ # https://github.com/sass/sass/blob/stable/lib/sass/script/functions.rb#L1313
55
+ def mix(color1, color2, weight)
56
+ color1 = RainbowColors::Convertor.rgb_from_hex color1
57
+ color2 = RainbowColors::Convertor.rgb_from_hex color2
58
+
59
+ p = weight / 100.0
60
+ w = p * 2 - 1
61
+
62
+ w1 = (w + 1) / 2
63
+ w2 = 1 - w1
64
+
65
+ r = (color1[:red] * w1 + color2[:red] * w2).round
66
+ g = (color1[:green] * w1 + color2[:green] * w2).round
67
+ b = (color1[:blue] * w1 + color2[:blue] * w2).round
68
+
69
+ RainbowColors::Convertor.hex_from_rgb({ red: r, green: g, blue: b })
70
+ end
71
+
72
+ def shades_tints(mix_color)
73
+ colors = [@primary_color]
74
+
75
+ weight = 0
76
+ increment = 15
77
+
78
+ 4.times do
79
+ weight = weight + increment
80
+ colors.push mix(mix_color, @primary_color, weight)
81
+ end
82
+
83
+ colors
84
+ end
85
+ end
86
+ end
@@ -1,3 +1,3 @@
1
1
  module RainbowColors
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.3.0".freeze
3
3
  end
@@ -2,3 +2,5 @@ require "rmagick"
2
2
  require "rainbow_colors/version"
3
3
  require "rainbow_colors/convertor"
4
4
  require "rainbow_colors/palette"
5
+ require "rainbow_colors/scheme"
6
+ require "rainbow_colors/image_palette"
@@ -0,0 +1,9 @@
1
+ exit
2
+ colors.sort_by { |hash| hash[:hue] }exit
3
+ colors.sort_by { |hash| hash[:hue] }
4
+ colors.sort { |a,b| a[:hue] <=> b[:hue] }
5
+ colors.sort_by { |hash| hash[:hue] }
6
+ colors_sort_by { |hash| hash[:hue] }
7
+ colors.sort_by(&:hue)
8
+ colors.first[:hue]
9
+ colors.first
data/server/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "sinatra"
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ rack (1.6.4)
5
+ rack-protection (1.5.3)
6
+ rack
7
+ sinatra (1.4.7)
8
+ rack (~> 1.5)
9
+ rack-protection (~> 1.4)
10
+ tilt (>= 1.3, < 3)
11
+ tilt (2.0.2)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ sinatra
18
+
19
+ BUNDLED WITH
20
+ 1.11.2
data/server/img1.jpg ADDED
Binary file
data/server/img10.jpg ADDED
Binary file
data/server/img2.jpg ADDED
Binary file
data/server/img3.jpg ADDED
Binary file
data/server/img4.jpg ADDED
Binary file
data/server/img5.jpg ADDED
Binary file
data/server/img6.jpg ADDED
Binary file
data/server/img7.jpg ADDED
Binary file
data/server/img8.jpg ADDED
Binary file
data/server/img9.png ADDED
Binary file
data/server/server.rb ADDED
@@ -0,0 +1,56 @@
1
+ require "sinatra"
2
+ require "byebug"
3
+ require "rainbow_colors"
4
+ require "pp"
5
+
6
+ set :public_folder, File.dirname(__FILE__)
7
+
8
+ get '/' do
9
+ @triad = []
10
+ @analogous = []
11
+ @complementary = []
12
+ @complementary_split = []
13
+ @tints = []
14
+ @shades = []
15
+ for i in 0..30
16
+ cp = RainbowColors::Scheme.new RainbowColors::Palette.random_color
17
+
18
+ @triad.push cp.triad
19
+ @analogous.push cp.analogous
20
+ @complementary.push cp.complementary
21
+ @complementary_split.push cp.complementary_split
22
+ @tints.push cp.tints
23
+ @shades.push cp.shades
24
+ end
25
+
26
+ erb :index
27
+ end
28
+
29
+ get '/hex/:hex' do
30
+ @triad = []
31
+ @analogous = []
32
+ @complementary = []
33
+ @complementary_split = []
34
+ @tints = []
35
+ @shades = []
36
+
37
+ cp = RainbowColors::Scheme.new "##{params[:hex]}"
38
+
39
+ @triad.push cp.triad
40
+ @analogous.push cp.analogous
41
+ @complementary.push cp.complementary
42
+ @complementary_split.push cp.complementary_split
43
+ @tints.push cp.tints
44
+ @shades.push cp.shades
45
+
46
+ erb :index
47
+ end
48
+
49
+ get "/image" do
50
+ image = RainbowColors::ImagePalette.new params[:img]
51
+ colors = image.scheme
52
+ pp image.color_background
53
+ pp image.color_text
54
+
55
+ erb :image, locals: { colors: colors, image: params[:img] }
56
+ end
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <meta charset="utf-8">
3
+ <title>Rainbow Colors</title>
4
+
5
+ <style>
6
+ .color {
7
+ width: 100px;
8
+ display: inline-block;
9
+ float: left;
10
+ text-align: center;
11
+ line-height: 50px;
12
+ }
13
+
14
+ img {
15
+ max-height: 400px;
16
+ margin-bottom: 1rem;
17
+ display: block;
18
+ }
19
+ </style>
20
+
21
+ <img src="<%= image %>">
22
+
23
+ <% colors.each do |c| %>
24
+ <span class="color" style="background-color:<%= c %>;color:<%= c %>"><%= c %></span>
25
+ <% end %>
@@ -0,0 +1,97 @@
1
+ <!DOCTYPE html>
2
+ <meta charset="utf-8">
3
+
4
+ <style>
5
+ h2 {
6
+ font-size: 14px;
7
+ font-family: Avenir Next;
8
+ }
9
+
10
+ .algorithm {
11
+ margin-right: 20px;
12
+ float: left;
13
+ }
14
+
15
+ .row {
16
+ margin-bottom: 2px;
17
+ }
18
+
19
+ .column {
20
+ font-size: 10px;
21
+ width: 50px;
22
+ height: 20px;
23
+ display: inline-block;
24
+ }
25
+ </style>
26
+
27
+ <div class="algorithm">
28
+ <h2>Analogous</h2>
29
+
30
+ <% @analogous.each do |s| %>
31
+ <div class="row">
32
+ <% s.each do |c| %>
33
+ <span class="column" style="background-color:<%= c %>;color:<%= c %>"><%= c %></span>
34
+ <% end %>
35
+ </div>
36
+ <% end %>
37
+ </div>
38
+
39
+ <div class="algorithm">
40
+ <h2>Triad</h2>
41
+
42
+ <% @triad.each do |s| %>
43
+ <div class="row">
44
+ <% s.each do |c| %>
45
+ <span class="column" style="background-color:<%= c %>;color:<%= c %>"><%= c %></span>
46
+ <% end %>
47
+ </div>
48
+ <% end %>
49
+ </div>
50
+
51
+ <div class="algorithm">
52
+ <h2>Split Complementary</h2>
53
+
54
+ <% @complementary_split.each do |s| %>
55
+ <div class="row">
56
+ <% s.each do |c| %>
57
+ <span class="column" style="background-color:<%= c %>;color:<%= c %>"><%= c %></span>
58
+ <% end %>
59
+ </div>
60
+ <% end %>
61
+ </div>
62
+
63
+ <div class="algorithm">
64
+ <h2>Complementary</h2>
65
+
66
+ <% @complementary.each do |s| %>
67
+ <div class="row">
68
+ <% s.each do |c| %>
69
+ <span class="column" style="background-color:<%= c %>;color:<%= c %>"><%= c %></span>
70
+ <% end %>
71
+ </div>
72
+ <% end %>
73
+ </div>
74
+
75
+ <div class="algorithm">
76
+ <h2>Tints</h2>
77
+
78
+ <% @tints.each do |s| %>
79
+ <div class="row">
80
+ <% s.each do |c| %>
81
+ <span class="column" style="background-color:<%= c %>;color:<%= c %>"><%= c %></span>
82
+ <% end %>
83
+ </div>
84
+ <% end %>
85
+ </div>
86
+
87
+ <div class="algorithm">
88
+ <h2>Shades</h2>
89
+
90
+ <% @shades.each do |s| %>
91
+ <div class="row">
92
+ <% s.each do |c| %>
93
+ <span class="column" style="background-color:<%= c %>;color:<%= c %>"><%= c %></span>
94
+ <% end %>
95
+ </div>
96
+ <% end %>
97
+ </div>
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.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ashish Kumar
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-03-01 00:00:00.000000000 Z
12
+ date: 2016-03-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rmagick
@@ -87,9 +87,27 @@ files:
87
87
  - config.ru
88
88
  - lib/rainbow_colors.rb
89
89
  - lib/rainbow_colors/convertor.rb
90
+ - lib/rainbow_colors/image_palette.rb
90
91
  - lib/rainbow_colors/palette.rb
92
+ - lib/rainbow_colors/scheme.rb
91
93
  - lib/rainbow_colors/version.rb
92
94
  - rainbow_colors.gemspec
95
+ - server/.byebug_history
96
+ - server/Gemfile
97
+ - server/Gemfile.lock
98
+ - server/img1.jpg
99
+ - server/img10.jpg
100
+ - server/img2.jpg
101
+ - server/img3.jpg
102
+ - server/img4.jpg
103
+ - server/img5.jpg
104
+ - server/img6.jpg
105
+ - server/img7.jpg
106
+ - server/img8.jpg
107
+ - server/img9.png
108
+ - server/server.rb
109
+ - server/views/image.erb
110
+ - server/views/index.erb
93
111
  homepage: https://github.com/ashishkumar/rainbow_colors
94
112
  licenses:
95
113
  - MIT