rainbow_colors 0.1.3 → 0.2.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: de6ab96343026e27e1c825f076038ad0513e0df5
4
- data.tar.gz: 7d56af9840b74569bb66b630b2a535e03b864827
3
+ metadata.gz: eef2a6e2c5b2657ba71ea5fb0df4e6e1561aa247
4
+ data.tar.gz: ff5c14a4e78f696c016cef3a2569fccc555e7c9d
5
5
  SHA512:
6
- metadata.gz: 5020257c347ff805bb728baff9768c8bcd1a88d15364293da57d17cb3ff4012d45f0a41449b46d7affddbf3432a53d286ee7ebc3659f6a689b793c6d6a9c6b33
7
- data.tar.gz: f776e51c75b7a0fc4e00e81be893336924827c97f65e23ffc7e573bdeaba3c1ab7969b9b8dc794e47d442c7018cc14b8d07edbedcdd6c8f31180bdcff9d23d01
6
+ metadata.gz: 234a9f0eb7191dbc793a46558aad9de4cdf731b2ba7cb31db90d4c04342876e26b324bba77361ae785bedaef7ba96935a57e9c9adac26416cda8b7d93d30eade
7
+ data.tar.gz: 1a0d05cdc486988e1c05ae85f168034fb08084ca9b438d9fa2c9a571c516e447b9e3121e141b466a06933b76b2f7b793461113aaf37f2e9d70accc6f68297b02
data/config.ru ADDED
@@ -0,0 +1,2 @@
1
+ require './server/server'
2
+ run Sinatra::Application
@@ -82,6 +82,16 @@ module RainbowColors
82
82
  { red: red.round, green: green.round, blue: blue.round }
83
83
  end
84
84
 
85
+ def self.hsl_from_hex(color_hex)
86
+ rgb = rgb_from_hex color_hex
87
+ hsl = hsl_from_rgb rgb
88
+ end
89
+
90
+ def self.hex_from_hsl(color_hsl)
91
+ rgb = rgb_from_hsl color_hsl
92
+ hex = hex_from_rgb rgb
93
+ end
94
+
85
95
  private
86
96
 
87
97
  def self.rgb_from_hue(v1, v2, vH)
@@ -16,27 +16,83 @@ module RainbowColors
16
16
  secondary = color_with_variance 30
17
17
  tertiary = color_with_variance -30
18
18
 
19
- { primary: @primary_color, secondary: secondary, tertiary: tertiary }
19
+ [@primary_color, secondary, tertiary]
20
20
  end
21
21
 
22
22
  def complementary
23
23
  secondary = color_with_variance 180
24
24
 
25
- { primary: @primary_color, secondary: secondary }
25
+ [@primary_color, secondary]
26
26
  end
27
27
 
28
28
  def complementary_split
29
29
  secondary = color_with_variance 150
30
30
  tertiary = color_with_variance 210
31
31
 
32
- { primary: @primary_color, secondary: secondary, tertiary: tertiary }
32
+ [@primary_color, secondary, tertiary]
33
33
  end
34
34
 
35
35
  def triad
36
36
  secondary = color_with_variance 120
37
37
  tertiary = color_with_variance -120
38
38
 
39
- { primary: @primary_color, secondary: secondary, tertiary: tertiary }
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
+
54
+ 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 }
92
+ end
93
+ end
94
+
95
+ average_colors.map! { |color_hsl| RainbowColors::Convertor.hex_from_hsl color_hsl }
40
96
  end
41
97
 
42
98
  private
@@ -49,5 +105,38 @@ module RainbowColors
49
105
 
50
106
  RainbowColors::Convertor.hex_from_rgb color
51
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
+ end
52
141
  end
53
142
  end
@@ -1,3 +1,3 @@
1
1
  module RainbowColors
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0".freeze
3
3
  end
@@ -1,3 +1,4 @@
1
+ require "rmagick"
1
2
  require "rainbow_colors/version"
2
3
  require "rainbow_colors/convertor"
3
4
  require "rainbow_colors/palette"
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.bindir = "exe"
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rmagick"
20
22
 
21
23
  spec.add_development_dependency "bundler", "~> 1.11"
22
24
  spec.add_development_dependency "rake", "~> 10.0"
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.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ashish Kumar
@@ -9,8 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-02-16 00:00:00.000000000 Z
12
+ date: 2016-03-01 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rmagick
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: bundler
16
30
  requirement: !ruby/object:Gem::Requirement
@@ -70,6 +84,7 @@ files:
70
84
  - Rakefile
71
85
  - bin/console
72
86
  - bin/setup
87
+ - config.ru
73
88
  - lib/rainbow_colors.rb
74
89
  - lib/rainbow_colors/convertor.rb
75
90
  - lib/rainbow_colors/palette.rb