shrine-color 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a7d2d6bf761232e62fdc34b0e1378dfa8288aab5
4
+ data.tar.gz: a15371b69e211694cb0c642bf95ef8664c2d482a
5
+ SHA512:
6
+ metadata.gz: cd31b407cef673b5c6b454bbdb404c70aaec79433ed7f1b34a08e826a344f819daf53836675c49bf81c5d7a82cb9c4a43d4e23d39bbed82d9d6b60a53aabccd0
7
+ data.tar.gz: 8836c647e54b2bc93e4cab88e30b6e524bdabdb623abbf9d986990183464deaae60cdcf625406c4fa9173b0d63b5f0ec028c91c469836df8d0c00d63d0e19cfa
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ vendor/
2
+ t.rb
3
+ public/
4
+ .bundle/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ shrine-color (0.1)
5
+ jnylen-colorscore (>= 0.1.1)
6
+ shrine (>= 2.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ color (1.8)
12
+ diff-lcs (1.2.5)
13
+ down (2.3.6)
14
+ jnylen-colorscore (0.1.1)
15
+ color
16
+ rake (11.2.2)
17
+ rspec (3.5.0)
18
+ rspec-core (~> 3.5.0)
19
+ rspec-expectations (~> 3.5.0)
20
+ rspec-mocks (~> 3.5.0)
21
+ rspec-core (3.5.2)
22
+ rspec-support (~> 3.5.0)
23
+ rspec-expectations (3.5.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.5.0)
26
+ rspec-mocks (3.5.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.5.0)
29
+ rspec-support (3.5.0)
30
+ shrine (2.2.0)
31
+ down (>= 2.3.5)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ bundler (~> 1.12)
38
+ rake (~> 11.1)
39
+ rspec (~> 3.0)
40
+ shrine-color!
41
+
42
+ BUNDLED WITH
43
+ 1.12.5
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Joakim Nylén
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ Shrine Color
2
+ =================
3
+
4
+ Get the dominant color of an image using colorscore (updated version by me). This can be used as a background color while an image is loading or search by color.
5
+
6
+ This is based on carrierwave-color by Sunny Ripert (sunny).
7
+
8
+ Requirements
9
+ ------------
10
+
11
+ This gem requires imagemagick (convert) and grabs colors via a command line.
12
+ So mini_magick, etc. isn't required.
13
+
14
+
15
+ Installation
16
+ ------------
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem "shrine-color"
22
+ ```
23
+
24
+ And then call:
25
+
26
+ ```sh
27
+ $ bundle
28
+ ```
29
+
30
+ Add the plugin to your uploader
31
+ --------------------------------
32
+
33
+ In your uploader, include the module and call the processor:
34
+
35
+ ```ruby
36
+ class ImageUploader < Shrine
37
+ # plugin add_metadata should be loaded automagically.
38
+ # otherwise add it here
39
+ plugin :color
40
+
41
+ # dominant color
42
+ add_metadata :dominant_color do |io, context|
43
+ dominant_color(io.path)
44
+ end
45
+
46
+ # palette color with version and own array of colors.
47
+ add_metadata :palette_color do |io, context|
48
+ if context[:version] == :small
49
+ palette_color(io.path, ['ff0000', '00ff00', '0000ff'])
50
+ end
51
+ end
52
+ end
53
+ ```
54
+
55
+ You can now use the code below to get a color:
56
+ ```ruby
57
+ ## Dominant color
58
+ photo.image[:small].metadata["dominant_color"]
59
+ # or
60
+ photo.image[:small].dominant_color
61
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,44 @@
1
+ require "colorscore"
2
+
3
+ class Shrine
4
+ module Plugins
5
+ # text text text
6
+ module Color
7
+ def self.load_dependencies(uploader, *)
8
+ uploader.plugin :add_metadata
9
+ end
10
+
11
+ module InstanceMethods
12
+ DEFAULT_PALETTE_HEXES = ["660000", "990000", "cc0000", "cc3333", "ea4c88", "993399",
13
+ "663399", "333399", "0066cc", "0099cc", "66cccc", "77cc33",
14
+ "669900", "336600", "666600", "999900", "cccc33", "ffff00",
15
+ "ffcc33", "ff9900", "ff6600", "cc6633", "996633", "663300",
16
+ "000000", "999999", "cccccc", "ffffff"]
17
+
18
+ # Returns the most dominant color in HEX in an image.
19
+ def dominant_color(io_path)
20
+ histogram = Colorscore::Histogram.new(io_path, :colors => 1)
21
+ color = histogram.colors.first
22
+ color && color.html # html = hex
23
+ end
24
+
25
+ # Matches the dominant color to the closest color in the array provided
26
+ def palette_color(io_path, palette_hexes = DEFAULT_PALETTE_HEXES)
27
+ histogram = Colorscore::Histogram.new(io_path)
28
+ palette = Colorscore::Palette.from_hex(palette_hexes)
29
+
30
+ histogram_scores = histogram.scores
31
+ return nil if histogram_scores.first.last.nil?
32
+
33
+ scores = palette.scores(histogram_scores, 1)
34
+ _score, color = scores.first
35
+ color.html
36
+ end
37
+
38
+ end
39
+
40
+ end
41
+
42
+ register_plugin(:color, Color)
43
+ end
44
+ end
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "shrine-color"
6
+ spec.version = "0.2.0"
7
+ spec.authors = ["Joakim Nylen"]
8
+ spec.email = ["me@jnylen.nu"]
9
+ spec.summary = "Return the dominant color of an image in Shrine."
10
+ spec.description = "Return the dominant color of an image in Shrine."
11
+ spec.homepage = "https://github.com/jnylen/shrine-color"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_dependency "shrine", ">= 2.0"
18
+ spec.add_dependency "jnylen-colorscore", ">= 0.1.2"
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.12"
21
+ spec.add_development_dependency "rake", "~> 11.1"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ end
Binary file
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shrine::Plugins::Color do
4
+ before do
5
+ @uploader = uploader { plugin :color }
6
+ end
7
+
8
+ describe ":dominant_color" do
9
+ it "extracts dominant_color from files via add_metadata" do
10
+ @uploader.class.add_metadata(:dominant_color) { |io, context| dominant_color(io.path) }
11
+ uploaded_file = @uploader.upload(File.open("spec/fixtures/bora.jpg"))
12
+ expect(uploaded_file.metadata.fetch("dominant_color")).to eq("#48839f")
13
+ end
14
+ end
15
+
16
+ describe ":palette_color" do
17
+ it "extracts palette_color from files with default hexes via add_metadata" do
18
+ @uploader.class.add_metadata(:palette_color) { |io, context| palette_color(io.path) }
19
+ uploaded_file = @uploader.upload(File.open("spec/fixtures/bora.jpg"))
20
+ expect(uploaded_file.metadata.fetch("palette_color")).to eq("#333399")
21
+ end
22
+
23
+ it "extracts palette_color from files with custom hexes via add_metadata" do
24
+ @uploader.class.add_metadata(:palette_color) { |io, context| palette_color(io.path, ['ff0000', '00ff00', '0000ff']) }
25
+ uploaded_file = @uploader.upload(File.open("spec/fixtures/bora.jpg"))
26
+ expect(uploaded_file.metadata.fetch("palette_color")).to eq("#0000ff")
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'shrine'
3
+ require 'shrine/plugins/add_metadata'
4
+ require 'shrine/plugins/color'
5
+ require 'shrine/storage/file_system'
6
+
7
+ def uploader(storage_key = :store, &block)
8
+ uploader_class = Class.new(Shrine)
9
+ uploader_class.storages[:cache] = Shrine::Storage::FileSystem.new("/tmp", prefix: "uploads")
10
+ uploader_class.storages[:store] = Shrine::Storage::FileSystem.new("/tmp", prefix: "uploads")
11
+ uploader_class.class_eval(&block) if block
12
+ uploader_class.new(storage_key)
13
+ end
14
+
15
+ def attacher(*args, &block)
16
+ uploader = uploader(*args, &block)
17
+ Object.send(:remove_const, "User") if defined?(User) # for warnings
18
+ user_class = Object.const_set("User", Struct.new(:avatar_data, :id))
19
+ user_class.include uploader.class[:avatar]
20
+ user_class.new.avatar_attacher
21
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shrine-color
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Joakim Nylen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shrine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jnylen-colorscore
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '11.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '11.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Return the dominant color of an image in Shrine.
84
+ email:
85
+ - me@jnylen.nu
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE
94
+ - README.md
95
+ - Rakefile
96
+ - lib/shrine/plugins/color.rb
97
+ - shrine-color.gemspec
98
+ - spec/fixtures/bora.jpg
99
+ - spec/shrine/color_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: https://github.com/jnylen/shrine-color
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.5.1
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Return the dominant color of an image in Shrine.
125
+ test_files: []