carrierwave-color 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +1 -0
- data/README.md +111 -0
- data/Rakefile +1 -0
- data/carrierwave-color.gemspec +25 -0
- data/lib/carrierwave/color/detection.rb +26 -0
- data/lib/carrierwave/color/version.rb +5 -0
- data/lib/carrierwave/color.rb +15 -0
- data/spec/detection_spec.rb +23 -0
- data/spec/fixtures/red-rainbow.jpg +0 -0
- data/spec/spec_helper.rb +3 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b934eea908ecbe77e1131bec424d0697c2565c0a
|
4
|
+
data.tar.gz: 44874d042c751d49c88e06d35bf13ac47b371ef5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6509d6720f23b2704a953102bb93582f8c76f6300cd82cc09ce07b65e11d45e6852e65f026b0543f9cb56b88019fe0f2ebc5d3015dd726ec07d38365ec193fac
|
7
|
+
data.tar.gz: 4c17e7564c4cd70b9af6018beb0b7467d0fe0498be6c5dffe3635e469272fd87017cd2aa7cf87e4c0ad3f3483a08e2f76c1e34e7be6e4894587a52f0bb53350f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gemspec
|
data/README.md
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
CarrierWave Color
|
2
|
+
=================
|
3
|
+
|
4
|
+
Add the dominant color of an image to your database whenever you upload it
|
5
|
+
with the CarrierWave gem on your Rails app.
|
6
|
+
|
7
|
+
|
8
|
+
Installation
|
9
|
+
------------
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem "carrierwave-color"
|
15
|
+
```
|
16
|
+
|
17
|
+
And then call:
|
18
|
+
|
19
|
+
```sh
|
20
|
+
$ bundle
|
21
|
+
```
|
22
|
+
|
23
|
+
Add a processor to your uploader
|
24
|
+
--------------------------------
|
25
|
+
|
26
|
+
In your uploader, include the module and call the processor:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class PhotoUploader < CarrierWave::Uploader::Base
|
30
|
+
include include CarrierWave::Color
|
31
|
+
|
32
|
+
process :store_dominant_color
|
33
|
+
|
34
|
+
# …
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Add a dominant color attribute
|
39
|
+
------------------------------
|
40
|
+
|
41
|
+
To store the color, you need an attribute on the Rails model where you attach
|
42
|
+
your uploader. Call it `{uploader}_dominant_color`.
|
43
|
+
|
44
|
+
For example if you have the following model:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class Article
|
48
|
+
mount_uploader :photo, PhotoUploader
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
Then you would need to add a `photo_dominant_color` column to the `articles`
|
53
|
+
table. You could create a migration by executing:
|
54
|
+
|
55
|
+
```sh
|
56
|
+
$ rails g migration AddPhotoDominantColorToArticles photo_dominant_color
|
57
|
+
$ rake db:migrate
|
58
|
+
```
|
59
|
+
|
60
|
+
Using your dominant color
|
61
|
+
-------------------------
|
62
|
+
|
63
|
+
An easy way of using your dominant colors is to put them as a background to your
|
64
|
+
image tags:
|
65
|
+
|
66
|
+
```erb
|
67
|
+
<%= image_tag(article.photo.url(:thumb), style: "background: #{article.photo_dominant_color}") %>
|
68
|
+
```
|
69
|
+
|
70
|
+
For example, on [cults3d](http://cults3d.com):
|
71
|
+
|
72
|
+
![Cults 3D Animated Screenshot with dominant colors during image load](doc/example-cults3d.gif)
|
73
|
+
|
74
|
+
Resizing
|
75
|
+
--------
|
76
|
+
|
77
|
+
If you do any resizing or if you have several versions of your uploader,
|
78
|
+
store the color on the smallest so that processing goes faster. For example:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# …
|
82
|
+
version :medium do
|
83
|
+
process resize_to_fill: [200, 200]
|
84
|
+
end
|
85
|
+
|
86
|
+
version :thumb, from_version: :medium do
|
87
|
+
process resize_to_fill: [42, 42]
|
88
|
+
process :store_dominant_color
|
89
|
+
end
|
90
|
+
# …
|
91
|
+
```
|
92
|
+
|
93
|
+
Dominant color from a palette
|
94
|
+
-----------------------------
|
95
|
+
|
96
|
+
If you wish to find the dominant color from a selection of colors, you can use
|
97
|
+
the following processor with a `{uploader}_palette_color` field on your model:
|
98
|
+
|
99
|
+
```rb
|
100
|
+
process store_palette_color: ['ff0000', '00ff00', '0000ff']
|
101
|
+
```
|
102
|
+
|
103
|
+
|
104
|
+
Contributing
|
105
|
+
------------
|
106
|
+
|
107
|
+
1. Fork it ( https://github.com/sunny/carrierwave-color/fork )
|
108
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
109
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
110
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
111
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'carrierwave/color/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "carrierwave-color"
|
8
|
+
spec.version = CarrierWave::Color::VERSION
|
9
|
+
spec.authors = ["Sunny Ripert"]
|
10
|
+
spec.email = ["sunny@sunfox.org"]
|
11
|
+
spec.summary = "Store the dominant color of an image with CarrierWave"
|
12
|
+
spec.description = "Store the dominant color of the images when you " \
|
13
|
+
"upload them to your Rails application using the " \
|
14
|
+
"CarrierWave gem."
|
15
|
+
spec.homepage = "https://github.com/sunny/carrierwave-color"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_runtime_dependency "colorscore"
|
25
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "colorscore"
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Color
|
5
|
+
module Detection
|
6
|
+
class Error < StandardError
|
7
|
+
end
|
8
|
+
|
9
|
+
module_function
|
10
|
+
|
11
|
+
def dominant_html(path)
|
12
|
+
histogram = Colorscore::Histogram.new(path, 1)
|
13
|
+
color = histogram.colors.first
|
14
|
+
color && color.html
|
15
|
+
end
|
16
|
+
|
17
|
+
def dominant_from_palette_html(path, palette_hexes)
|
18
|
+
histogram = Colorscore::Histogram.new(path)
|
19
|
+
palette = Colorscore::Palette.from_hex(palette_hexes)
|
20
|
+
scores = palette.scores(histogram.scores, 1)
|
21
|
+
_score, color = scores.first
|
22
|
+
color.html
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "carrierwave/color/detection"
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Color
|
5
|
+
def store_dominant_color
|
6
|
+
color = Detection.dominant_html(current_path)
|
7
|
+
model.send "#{mounted_as}_dominant_color=", color
|
8
|
+
end
|
9
|
+
|
10
|
+
def store_palette_color(palette = Colorscore::Palette::DEFAULT)
|
11
|
+
color = Detection.dominant_from_palette_html(current_path, palette)
|
12
|
+
model.send "#{mounted_as}_palette_color=", color
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module CarrierWave
|
4
|
+
module Color
|
5
|
+
RSpec.describe Detection do
|
6
|
+
let(:path) { File.expand_path('../fixtures/red-rainbow.jpg', __FILE__) }
|
7
|
+
|
8
|
+
describe ".dominant_html" do
|
9
|
+
it "returns the correct hex code" do
|
10
|
+
expect(Detection.dominant_html(path)).to eq("#700503")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ".dominant_from_palette_html" do
|
15
|
+
it "returns the correct hex code" do
|
16
|
+
palette = %w(ff0000 00ff00 0000ff)
|
17
|
+
result = Detection.dominant_from_palette_html(path, palette)
|
18
|
+
expect(result).to eq("#ff0000")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
Binary file
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: carrierwave-color
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sunny Ripert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: colorscore
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Store the dominant color of the images when you upload them to your Rails
|
70
|
+
application using the CarrierWave gem.
|
71
|
+
email:
|
72
|
+
- sunny@sunfox.org
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- Gemfile
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- carrierwave-color.gemspec
|
82
|
+
- lib/carrierwave/color.rb
|
83
|
+
- lib/carrierwave/color/detection.rb
|
84
|
+
- lib/carrierwave/color/version.rb
|
85
|
+
- spec/detection_spec.rb
|
86
|
+
- spec/fixtures/red-rainbow.jpg
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: https://github.com/sunny/carrierwave-color
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.5
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Store the dominant color of an image with CarrierWave
|
112
|
+
test_files: []
|