vibrant 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/examples/.gitignore +17 -0
- data/examples/Gemfile +18 -0
- data/examples/Gemfile.lock +135 -0
- data/examples/README.rdoc +28 -0
- data/examples/Rakefile +6 -0
- data/examples/app/assets/images/.keep +0 -0
- data/examples/app/assets/javascripts/application.coffee +1 -0
- data/examples/app/assets/javascripts/home.coffee +0 -0
- data/examples/app/assets/stylesheets/application.css +15 -0
- data/examples/app/assets/stylesheets/home.scss +3 -0
- data/examples/app/controllers/application_controller.rb +3 -0
- data/examples/app/controllers/concerns/.keep +0 -0
- data/examples/app/controllers/home_controller.rb +20 -0
- data/examples/app/helpers/application_helper.rb +2 -0
- data/examples/app/helpers/home_helper.rb +2 -0
- data/examples/app/mailers/.keep +0 -0
- data/examples/app/models/.keep +0 -0
- data/examples/app/models/concerns/.keep +0 -0
- data/examples/app/uploaders/photo_uploader.rb +51 -0
- data/examples/app/views/home/index.html.erb +26 -0
- data/examples/app/views/layouts/application.html.erb +14 -0
- data/examples/bin/bundle +3 -0
- data/examples/bin/rails +8 -0
- data/examples/bin/rake +8 -0
- data/examples/bin/setup +29 -0
- data/examples/bin/spring +15 -0
- data/examples/config.ru +4 -0
- data/examples/config/application.rb +29 -0
- data/examples/config/boot.rb +3 -0
- data/examples/config/database.yml +25 -0
- data/examples/config/environment.rb +5 -0
- data/examples/config/environments/development.rb +41 -0
- data/examples/config/environments/production.rb +79 -0
- data/examples/config/environments/test.rb +42 -0
- data/examples/config/initializers/assets.rb +11 -0
- data/examples/config/initializers/backtrace_silencers.rb +7 -0
- data/examples/config/initializers/cookies_serializer.rb +3 -0
- data/examples/config/initializers/filter_parameter_logging.rb +4 -0
- data/examples/config/initializers/inflections.rb +16 -0
- data/examples/config/initializers/mime_types.rb +4 -0
- data/examples/config/initializers/session_store.rb +3 -0
- data/examples/config/initializers/wrap_parameters.rb +14 -0
- data/examples/config/locales/en.yml +23 -0
- data/examples/config/routes.rb +6 -0
- data/examples/config/secrets.yml +22 -0
- data/examples/db/seeds.rb +7 -0
- data/examples/lib/assets/.keep +0 -0
- data/examples/lib/tasks/.keep +0 -0
- data/examples/log/.keep +0 -0
- data/examples/public/1.jpg +0 -0
- data/examples/public/2.jpg +0 -0
- data/examples/public/3.jpg +0 -0
- data/examples/public/4.jpg +0 -0
- data/examples/public/404.html +67 -0
- data/examples/public/422.html +67 -0
- data/examples/public/500.html +66 -0
- data/examples/public/favicon.ico +0 -0
- data/examples/public/octocat.png +0 -0
- data/examples/public/robots.txt +5 -0
- data/examples/test/controllers/.keep +0 -0
- data/examples/test/controllers/home_controller_test.rb +9 -0
- data/examples/test/fixtures/.keep +0 -0
- data/examples/test/helpers/.keep +0 -0
- data/examples/test/integration/.keep +0 -0
- data/examples/test/mailers/.keep +0 -0
- data/examples/test/models/.keep +0 -0
- data/examples/test/test_helper.rb +10 -0
- data/examples/vendor/assets/javascripts/.keep +0 -0
- data/examples/vendor/assets/stylesheets/.keep +0 -0
- data/images/1.jpg +0 -0
- data/images/2.jpg +0 -0
- data/images/3.jpg +0 -0
- data/images/4.jpg +0 -0
- data/images/octocat.png +0 -0
- data/lib/vibrant.rb +15 -0
- data/lib/vibrant/swatch.rb +55 -0
- data/lib/vibrant/version.rb +4 -0
- data/lib/vibrant/vibrant.rb +177 -0
- data/vibrant-rb.gemspec +25 -0
- metadata +174 -0
@@ -0,0 +1,177 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Vibrant
|
3
|
+
|
4
|
+
class Vibrant
|
5
|
+
|
6
|
+
TARGET_DARK_LUNA = 0.26
|
7
|
+
MAX_DARK_LUNA = 0.45
|
8
|
+
MIN_LIGHT_LUNA = 0.55
|
9
|
+
TARGET_LIGHT_LUNA = 0.74
|
10
|
+
|
11
|
+
MIN_NORMAL_LUNA = 0.3
|
12
|
+
TARGET_NORMAL_LUNA = 0.5
|
13
|
+
MAX_NORMAL_LUNA = 0.7
|
14
|
+
|
15
|
+
TARGET_MUTED_SATURATION = 0.3
|
16
|
+
MAX_MUTED_SATURATION = 0.4
|
17
|
+
|
18
|
+
TARGET_VIBRANT_SATURATION = 1
|
19
|
+
MIN_VIBRANT_SATURATION = 0.35
|
20
|
+
|
21
|
+
NORMAL_LUNA = {
|
22
|
+
target: TARGET_NORMAL_LUNA,
|
23
|
+
range: MIN_NORMAL_LUNA..MAX_NORMAL_LUNA
|
24
|
+
}
|
25
|
+
LIGHT_LUNA = {
|
26
|
+
target: TARGET_LIGHT_LUNA,
|
27
|
+
range: MIN_LIGHT_LUNA..1
|
28
|
+
}
|
29
|
+
DARK_LUNA = {
|
30
|
+
target: TARGET_DARK_LUNA,
|
31
|
+
range: 0..MAX_DARK_LUNA
|
32
|
+
}
|
33
|
+
|
34
|
+
VIBRANT_SATURATION = {
|
35
|
+
target: TARGET_VIBRANT_SATURATION,
|
36
|
+
range: MIN_VIBRANT_SATURATION..1
|
37
|
+
}
|
38
|
+
MUTED_SATURATION = {
|
39
|
+
target: TARGET_MUTED_SATURATION,
|
40
|
+
range: 0..MAX_MUTED_SATURATION
|
41
|
+
}
|
42
|
+
|
43
|
+
WEIGHT_SATURATION = 3
|
44
|
+
WEIGHT_LUNA = 6
|
45
|
+
WEIGHT_POPULATION = 1
|
46
|
+
|
47
|
+
@@highestPopulation = 0
|
48
|
+
|
49
|
+
# def initialize(sourceImage, color_count: 64, quality: 5)
|
50
|
+
# @img = Magick::Image.from_blob(sourceImage.read).file.quantize(@color_count)
|
51
|
+
# @process
|
52
|
+
# end
|
53
|
+
|
54
|
+
def initialize(sourceImage, color_count: 64, quality: 5)
|
55
|
+
@color_count = color_count
|
56
|
+
@quality = quality
|
57
|
+
@image = Magick::Image.read(sourceImage).first.quantize(@color_count)
|
58
|
+
process
|
59
|
+
end
|
60
|
+
|
61
|
+
def process
|
62
|
+
|
63
|
+
@_swatches = []
|
64
|
+
|
65
|
+
# histogram
|
66
|
+
histogram = @image.color_histogram.inject({}) { |h, pair|
|
67
|
+
hex = pair[0].to_color(Magick::AllCompliance, false, 8, true)
|
68
|
+
hsla = pair[0].to_hsla
|
69
|
+
count = pair[1]
|
70
|
+
|
71
|
+
h[hex] ||= {
|
72
|
+
hex: hex,
|
73
|
+
hsla: hsla,
|
74
|
+
count: 0
|
75
|
+
}
|
76
|
+
h[hex][:count] += count
|
77
|
+
h
|
78
|
+
}.each_pair { |key, val|
|
79
|
+
@_swatches.push(Swatch.new(val[:hex], val[:hsla], val[:count]))
|
80
|
+
}
|
81
|
+
|
82
|
+
@maxPopulation = @_swatches.collect(&:population).max
|
83
|
+
|
84
|
+
@vibrantSwatch = find_color_variation(NORMAL_LUNA, VIBRANT_SATURATION)
|
85
|
+
@lightVibrantSwatch = find_color_variation(LIGHT_LUNA, VIBRANT_SATURATION)
|
86
|
+
@darkVibrantSwatch = find_color_variation(DARK_LUNA, VIBRANT_SATURATION)
|
87
|
+
@mutedSwatch = find_color_variation(NORMAL_LUNA, MUTED_SATURATION)
|
88
|
+
@lightMutedSwatch = find_color_variation(LIGHT_LUNA, MUTED_SATURATION)
|
89
|
+
@darkMutedSwatch = find_color_variation(DARK_LUNA, MUTED_SATURATION)
|
90
|
+
|
91
|
+
if @vibrantSwatch.nil? && !@darkVibrantSwatch.nil?
|
92
|
+
hsla = @DarkVibrantSwatch.hsla.dup
|
93
|
+
hsla[2] = TARGET_NORMAL_LUNA
|
94
|
+
@vibrantSwatch = Swatch.new(@DarkVibrantSwatch.hex, hsla, 0)
|
95
|
+
end
|
96
|
+
|
97
|
+
if @darkVibrantSwatch.nil? && !@vibrantSwatch.nil?
|
98
|
+
hsla = @vibrantSwatch.hsla.dup
|
99
|
+
hsla[2] = TARGET_DARK_LUNA
|
100
|
+
@darkVibrantSwatch = Swatch.new(@vibrantSwatch.hex, hsla, 0)
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
def find_color_variation(luna, saturation)
|
106
|
+
max = nil
|
107
|
+
maxValue = 0
|
108
|
+
|
109
|
+
@_swatches.each do |swatch|
|
110
|
+
#h = swatch.hsla[0] / 360
|
111
|
+
s = swatch.hsla[1] / 255
|
112
|
+
l = swatch.hsla[2] / 255
|
113
|
+
|
114
|
+
if luna[:range].include?(l) && saturation[:range].include?(s) && !already_selected?(swatch)
|
115
|
+
|
116
|
+
value = create_comparison_value(s, saturation[:target], l, luna[:target], swatch.population, @maxPopulation)
|
117
|
+
if max.nil? || value > maxValue
|
118
|
+
max = swatch
|
119
|
+
maxValue = value
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
p max
|
124
|
+
|
125
|
+
max
|
126
|
+
end
|
127
|
+
|
128
|
+
def create_comparison_value(saturation, targetSaturation, luna, target_luna, population, max_population)
|
129
|
+
self.class.weightedMean([
|
130
|
+
[self.class.invert_diff(saturation, targetSaturation), WEIGHT_SATURATION],
|
131
|
+
[self.class.invert_diff(luna, target_luna), WEIGHT_LUNA],
|
132
|
+
[(population / max_population), WEIGHT_POPULATION]
|
133
|
+
])
|
134
|
+
end
|
135
|
+
|
136
|
+
|
137
|
+
def swatches
|
138
|
+
Swatches.new(
|
139
|
+
vibrant: @vibrantSwatch,
|
140
|
+
muted: @mutedSwatch,
|
141
|
+
dark_vibrant: @darkVibrantSwatch,
|
142
|
+
dark_muted: @darkMutedSwatch,
|
143
|
+
light_vibrant: @lightVibrantSwatch,
|
144
|
+
light_muted: @lightMutedSwatch
|
145
|
+
)
|
146
|
+
end
|
147
|
+
|
148
|
+
def already_selected?(swatch)
|
149
|
+
return @vibrantSwatch == swatch ||
|
150
|
+
@darkVibrantSwatch == swatch ||
|
151
|
+
@lightVibrantSwatch == swatch ||
|
152
|
+
@mutedSwatch == swatch ||
|
153
|
+
@darkMutedSwatch == swatch ||
|
154
|
+
@lightMutedSwatch == swatch
|
155
|
+
end
|
156
|
+
|
157
|
+
class << self
|
158
|
+
|
159
|
+
def invert_diff(value, targetValue)
|
160
|
+
1 - (value - targetValue).abs
|
161
|
+
end
|
162
|
+
|
163
|
+
def weightedMean(values)
|
164
|
+
sum = 0
|
165
|
+
sumWeight = 0
|
166
|
+
values.each do |v|
|
167
|
+
value = v[0]
|
168
|
+
weight = v[1]
|
169
|
+
sum += value * weight
|
170
|
+
sumWeight += weight
|
171
|
+
end
|
172
|
+
sum / sumWeight
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
177
|
+
end
|
data/vibrant-rb.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'vibrant/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "vibrant"
|
8
|
+
spec.version = Vibrant::VERSION
|
9
|
+
spec.authors = ["dondoco7"]
|
10
|
+
spec.email = ["dondoco7@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Get color variations from an image.}
|
13
|
+
spec.description = %q{Get color variations from an image. Original is 'http://jariz.github.io/vibrant.js'}
|
14
|
+
spec.homepage = "https://github.com/dondoco7/vibrant-rb/"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "rmagick", ">= 2.14"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vibrant
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- dondoco7
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rmagick
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.14'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: Get color variations from an image. Original is 'http://jariz.github.io/vibrant.js'
|
56
|
+
email:
|
57
|
+
- dondoco7@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- examples/.gitignore
|
72
|
+
- examples/Gemfile
|
73
|
+
- examples/Gemfile.lock
|
74
|
+
- examples/README.rdoc
|
75
|
+
- examples/Rakefile
|
76
|
+
- examples/app/assets/images/.keep
|
77
|
+
- examples/app/assets/javascripts/application.coffee
|
78
|
+
- examples/app/assets/javascripts/home.coffee
|
79
|
+
- examples/app/assets/stylesheets/application.css
|
80
|
+
- examples/app/assets/stylesheets/home.scss
|
81
|
+
- examples/app/controllers/application_controller.rb
|
82
|
+
- examples/app/controllers/concerns/.keep
|
83
|
+
- examples/app/controllers/home_controller.rb
|
84
|
+
- examples/app/helpers/application_helper.rb
|
85
|
+
- examples/app/helpers/home_helper.rb
|
86
|
+
- examples/app/mailers/.keep
|
87
|
+
- examples/app/models/.keep
|
88
|
+
- examples/app/models/concerns/.keep
|
89
|
+
- examples/app/uploaders/photo_uploader.rb
|
90
|
+
- examples/app/views/home/index.html.erb
|
91
|
+
- examples/app/views/layouts/application.html.erb
|
92
|
+
- examples/bin/bundle
|
93
|
+
- examples/bin/rails
|
94
|
+
- examples/bin/rake
|
95
|
+
- examples/bin/setup
|
96
|
+
- examples/bin/spring
|
97
|
+
- examples/config.ru
|
98
|
+
- examples/config/application.rb
|
99
|
+
- examples/config/boot.rb
|
100
|
+
- examples/config/database.yml
|
101
|
+
- examples/config/environment.rb
|
102
|
+
- examples/config/environments/development.rb
|
103
|
+
- examples/config/environments/production.rb
|
104
|
+
- examples/config/environments/test.rb
|
105
|
+
- examples/config/initializers/assets.rb
|
106
|
+
- examples/config/initializers/backtrace_silencers.rb
|
107
|
+
- examples/config/initializers/cookies_serializer.rb
|
108
|
+
- examples/config/initializers/filter_parameter_logging.rb
|
109
|
+
- examples/config/initializers/inflections.rb
|
110
|
+
- examples/config/initializers/mime_types.rb
|
111
|
+
- examples/config/initializers/session_store.rb
|
112
|
+
- examples/config/initializers/wrap_parameters.rb
|
113
|
+
- examples/config/locales/en.yml
|
114
|
+
- examples/config/routes.rb
|
115
|
+
- examples/config/secrets.yml
|
116
|
+
- examples/db/seeds.rb
|
117
|
+
- examples/lib/assets/.keep
|
118
|
+
- examples/lib/tasks/.keep
|
119
|
+
- examples/log/.keep
|
120
|
+
- examples/public/1.jpg
|
121
|
+
- examples/public/2.jpg
|
122
|
+
- examples/public/3.jpg
|
123
|
+
- examples/public/4.jpg
|
124
|
+
- examples/public/404.html
|
125
|
+
- examples/public/422.html
|
126
|
+
- examples/public/500.html
|
127
|
+
- examples/public/favicon.ico
|
128
|
+
- examples/public/octocat.png
|
129
|
+
- examples/public/robots.txt
|
130
|
+
- examples/test/controllers/.keep
|
131
|
+
- examples/test/controllers/home_controller_test.rb
|
132
|
+
- examples/test/fixtures/.keep
|
133
|
+
- examples/test/helpers/.keep
|
134
|
+
- examples/test/integration/.keep
|
135
|
+
- examples/test/mailers/.keep
|
136
|
+
- examples/test/models/.keep
|
137
|
+
- examples/test/test_helper.rb
|
138
|
+
- examples/vendor/assets/javascripts/.keep
|
139
|
+
- examples/vendor/assets/stylesheets/.keep
|
140
|
+
- images/1.jpg
|
141
|
+
- images/2.jpg
|
142
|
+
- images/3.jpg
|
143
|
+
- images/4.jpg
|
144
|
+
- images/octocat.png
|
145
|
+
- lib/vibrant.rb
|
146
|
+
- lib/vibrant/swatch.rb
|
147
|
+
- lib/vibrant/version.rb
|
148
|
+
- lib/vibrant/vibrant.rb
|
149
|
+
- vibrant-rb.gemspec
|
150
|
+
homepage: https://github.com/dondoco7/vibrant-rb/
|
151
|
+
licenses:
|
152
|
+
- MIT
|
153
|
+
metadata: {}
|
154
|
+
post_install_message:
|
155
|
+
rdoc_options: []
|
156
|
+
require_paths:
|
157
|
+
- lib
|
158
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - ">="
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
requirements: []
|
169
|
+
rubyforge_project:
|
170
|
+
rubygems_version: 2.4.5
|
171
|
+
signing_key:
|
172
|
+
specification_version: 4
|
173
|
+
summary: Get color variations from an image.
|
174
|
+
test_files: []
|