gauguin 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +9 -0
  5. data/Gemfile +4 -0
  6. data/Guardfile +6 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +119 -0
  9. data/Rakefile +13 -0
  10. data/gauguin.gemspec +30 -0
  11. data/lib/gauguin/color.rb +76 -0
  12. data/lib/gauguin/color_space/lab_vector.rb +6 -0
  13. data/lib/gauguin/color_space/rgb_vector.rb +36 -0
  14. data/lib/gauguin/color_space/xyz_vector.rb +33 -0
  15. data/lib/gauguin/color_space.rb +4 -0
  16. data/lib/gauguin/colors_clusterer.rb +63 -0
  17. data/lib/gauguin/colors_limiter.rb +14 -0
  18. data/lib/gauguin/colors_retriever.rb +33 -0
  19. data/lib/gauguin/image.rb +55 -0
  20. data/lib/gauguin/image_recolorer.rb +29 -0
  21. data/lib/gauguin/image_repository.rb +7 -0
  22. data/lib/gauguin/noise_reducer.rb +26 -0
  23. data/lib/gauguin/painting.rb +29 -0
  24. data/lib/gauguin/palette_serializer.rb +22 -0
  25. data/lib/gauguin/version.rb +3 -0
  26. data/lib/gauguin.rb +43 -0
  27. data/spec/integration/painting_spec.rb +79 -0
  28. data/spec/integration/samples_spec.rb +43 -0
  29. data/spec/lib/gauguin/color_space/rgb_vector_spec.rb +15 -0
  30. data/spec/lib/gauguin/color_space/xyz_vector_spec.rb +15 -0
  31. data/spec/lib/gauguin/color_spec.rb +125 -0
  32. data/spec/lib/gauguin/colors_clusterer_spec.rb +158 -0
  33. data/spec/lib/gauguin/colors_limiter_spec.rb +27 -0
  34. data/spec/lib/gauguin/colors_retriever_spec.rb +85 -0
  35. data/spec/lib/gauguin/image_recolorer_spec.rb +94 -0
  36. data/spec/lib/gauguin/image_repository_spec.rb +15 -0
  37. data/spec/lib/gauguin/image_spec.rb +90 -0
  38. data/spec/lib/gauguin/noise_reducer_spec.rb +51 -0
  39. data/spec/lib/gauguin/painting_spec.rb +55 -0
  40. data/spec/lib/gauguin/palette_serializer_spec.rb +24 -0
  41. data/spec/spec_helper.rb +60 -0
  42. data/spec/support/pictures/10_colors.png +0 -0
  43. data/spec/support/pictures/12_colors.png +0 -0
  44. data/spec/support/pictures/gauguin.png +0 -0
  45. data/spec/support/pictures/gray_and_black.png +0 -0
  46. data/spec/support/pictures/not_unique_colors.png +0 -0
  47. data/spec/support/pictures/samples/sample1.png +0 -0
  48. data/spec/support/pictures/samples/sample10.png +0 -0
  49. data/spec/support/pictures/samples/sample11.png +0 -0
  50. data/spec/support/pictures/samples/sample2.png +0 -0
  51. data/spec/support/pictures/samples/sample3.png +0 -0
  52. data/spec/support/pictures/samples/sample4.png +0 -0
  53. data/spec/support/pictures/samples/sample5.png +0 -0
  54. data/spec/support/pictures/samples/sample6.png +0 -0
  55. data/spec/support/pictures/samples/sample7.png +0 -0
  56. data/spec/support/pictures/samples/sample8.png +0 -0
  57. data/spec/support/pictures/samples/sample9.png +0 -0
  58. data/spec/support/pictures/too_many_colors.png +0 -0
  59. data/spec/support/pictures/transparent_background.png +0 -0
  60. data/spec/support/pictures/unique_colors.png +0 -0
  61. metadata +251 -0
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ module Gauguin
4
+ describe ImageRepository do
5
+ let(:repository) { ImageRepository.new }
6
+ let(:path) { "path" }
7
+
8
+ describe "#get" do
9
+ it "returns image" do
10
+ expect(Gauguin::Image).to receive(:new).with(path)
11
+ repository.get(path)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ module Gauguin
4
+ describe Image do
5
+ let(:path) do
6
+ File.join("spec", "support", "pictures", "gray_and_black.png")
7
+ end
8
+
9
+ let(:image) { Image.new(path) }
10
+ let(:magic_pixel) { double }
11
+
12
+ describe "initialize" do
13
+ context "path given" do
14
+ subject { Image.new(path) }
15
+
16
+ it "returns Image with magick image present" do
17
+ expect(subject.image).not_to be nil
18
+ end
19
+ end
20
+
21
+ context "empty constructor" do
22
+ subject { Image.new }
23
+
24
+ it "returns Image without magick image" do
25
+ expect(subject.image).to be nil
26
+ end
27
+ end
28
+ end
29
+
30
+ describe ".blank" do
31
+ let(:rows) { 10 }
32
+ let(:columns) { 20 }
33
+
34
+
35
+ it "returns blank image with transparent background" do
36
+ blank_image = Image.blank(rows, columns)
37
+
38
+ pixels = columns.times.map do |column|
39
+ rows.times.map do |row|
40
+ blank_image.pixel_color(column, row)
41
+ end
42
+ end.flatten
43
+
44
+ expect(pixels.all? { |p| p.transparent? }).to be true
45
+ end
46
+ end
47
+
48
+ describe "#pixel" do
49
+ it "returns new Image::Pixel" do
50
+ expect(Image::Pixel).to receive(:new)
51
+ image.pixel(magic_pixel)
52
+ end
53
+ end
54
+
55
+ describe "#pixel_color" do
56
+ subject { image.pixel_color(0, 0) }
57
+
58
+ it "returns Image::Pixel for given row and column" do
59
+ expect(subject.to_rgb).to eq([204, 204, 204])
60
+ end
61
+ end
62
+
63
+ describe Image::Pixel do
64
+ let(:pixel) { Image::Pixel.new(magic_pixel) }
65
+
66
+ describe "#transparent?" do
67
+ let(:magic_pixel) { double(opacity: opacity) }
68
+ let(:opacity) { 0 }
69
+
70
+ subject { pixel.transparent? }
71
+
72
+ it { expect(subject).to be false }
73
+
74
+ context "opacity equals MAX_TRANSPARENCY" do
75
+ let(:opacity) { Image::Pixel::MAX_TRANSPARENCY }
76
+
77
+ it { expect(subject).to be true }
78
+ end
79
+ end
80
+
81
+ describe "#to_rgb" do
82
+ let(:magic_pixel) { double(red: 65535, green: 0, blue: 0) }
83
+
84
+ subject { pixel.to_rgb }
85
+
86
+ it { expect(subject).to eq([255, 0, 0]) }
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ module Gauguin
4
+ describe NoiseReducer do
5
+ let(:reducer) { NoiseReducer.new }
6
+
7
+ describe "#reduce" do
8
+ subject { reducer.call(colors).keys }
9
+
10
+ let(:white) { Color.new(255, 255, 255, 0.01) }
11
+ let(:red) { Color.new(255, 0, 0, 0.02) }
12
+ let(:black) { Color.new(0, 0, 0, 0.97) }
13
+
14
+ let(:colors) do
15
+ {
16
+ black => [black],
17
+ red => [red],
18
+ white => [white]
19
+ }
20
+ end
21
+
22
+ configure(:min_percentage_sum, 0.96)
23
+
24
+ it "returns only relevant colors" do
25
+ expect(subject).to eq([black])
26
+ end
27
+
28
+ context "no sum greater than min_percentage_sum" do
29
+ let(:white) { Color.new(255, 255, 255, 0.02) }
30
+ let(:red) { Color.new(255, 0, 0, 0.01) }
31
+ let(:black) { Color.new(0, 0, 0, 0.90) }
32
+
33
+ it "returns all colors" do
34
+ expect(subject).to eq([black, red, white])
35
+ end
36
+ end
37
+
38
+ context "transparent color" do
39
+ configure(:min_percentage_sum, 0.98)
40
+
41
+ before do
42
+ white.transparent = true
43
+ end
44
+
45
+ it "returns all colors except white" do
46
+ expect(subject).to eq([black, red])
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ module Gauguin
4
+ describe Painting do
5
+ let(:colors) { [] }
6
+
7
+ let(:image_repository) { double(get: double('image')) }
8
+ let(:colors_retriever) { double }
9
+ let(:colors_limiter) { double }
10
+ let(:noise_reducer) { double }
11
+ let(:colors_clusterer) { double }
12
+ let(:image_recolorer) { double }
13
+
14
+ let(:clusters) { {} }
15
+
16
+ let(:painting) do
17
+ Painting.new("path", image_repository, colors_retriever,
18
+ colors_limiter, noise_reducer,
19
+ colors_clusterer, image_recolorer)
20
+ end
21
+
22
+ describe "#palette" do
23
+ it "returns hash with main colors of the image" do
24
+ expect(colors_retriever).to receive(:colors).
25
+ and_return(colors)
26
+ expect(colors_limiter).to receive(:call).with(colors).
27
+ and_return(colors)
28
+ expect(colors_clusterer).to receive(:clusters).with(colors).
29
+ and_return(clusters)
30
+ expect(noise_reducer).to receive(:call).with(colors).
31
+ and_return(colors)
32
+
33
+ painting.palette
34
+ end
35
+ end
36
+
37
+ describe "#recolor" do
38
+ let(:palette) { {} }
39
+ let(:path) { 'path' }
40
+ let(:image) { double(write: nil) }
41
+ let(:new_colors) { {} }
42
+
43
+ it "recolors and writes the image to given path" do
44
+ expect(colors_clusterer).to receive(:reversed_clusters).
45
+ with(palette).and_return(new_colors)
46
+ expect(image_recolorer).to receive(:recolor).
47
+ with(new_colors).and_return(image)
48
+ expect(image).to receive(:write).with(path)
49
+
50
+ painting.recolor(palette, path)
51
+ end
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ module Gauguin
4
+ describe PaletteSerializer do
5
+ let(:palette) do
6
+ {
7
+ Color.new(255, 255, 255, 0.7, true) => [Color.new(255, 0, 0, 0.7, true)]
8
+ }
9
+ end
10
+
11
+ it "serializes palette" do
12
+ dumped = PaletteSerializer.dump(palette)
13
+ loaded = PaletteSerializer.load(dumped)
14
+
15
+ key = loaded.keys.first
16
+ value = loaded.values.first.first
17
+
18
+ expect(key.class).to eq(Color)
19
+ expect(value.class).to eq(Color)
20
+ expect(key.to_a).to eq([255, 255, 255, 0.7, true])
21
+ expect(value.to_a).to eq([255, 0, 0, 0.7, true])
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,60 @@
1
+ if ENV['CODECLIMATE_REPO_TOKEN']
2
+ require "codeclimate-test-reporter"
3
+ CodeClimate::TestReporter.start
4
+ else
5
+ require 'simplecov'
6
+ SimpleCov.start
7
+ end
8
+
9
+ require 'bundler/setup'
10
+ require './lib/gauguin'
11
+ require 'pry'
12
+
13
+ Bundler.setup
14
+
15
+ RSpec.configure do |config|
16
+ end
17
+
18
+ def configure(config_option, value)
19
+ old_value = Gauguin.configuration.send(config_option)
20
+
21
+ before do
22
+ Gauguin.configuration.send("#{config_option}=", value)
23
+ end
24
+
25
+ after do
26
+ Gauguin.configuration.send("#{config_option}=", old_value)
27
+ end
28
+ end
29
+
30
+ class FakeImage
31
+ attr_accessor :magic_black_pixel, :magic_red_pixel,
32
+ :magic_white_pixel, :magic_red_little_transparent_pixel,
33
+ :pixels_repository, :color_histogram, :rows, :columns,
34
+ :pixels, :colors_to_pixels
35
+
36
+ def pixel(magic_pixel)
37
+ pixels_repository[magic_pixel]
38
+ end
39
+
40
+ def pixel_color(row, column, new_color = nil)
41
+ if new_color
42
+ new_pixel = self.colors_to_pixels[new_color]
43
+ pixels[row][column] = new_pixel
44
+ end
45
+ pixels[row][column]
46
+ end
47
+
48
+ class Pixel < Gauguin::Image::Pixel
49
+ attr_accessor :magic_pixel
50
+
51
+ def initialize(magic_pixel)
52
+ self.magic_pixel = magic_pixel
53
+ end
54
+
55
+ def to_rgb
56
+ magic_pixel.rgb
57
+ end
58
+ end
59
+ end
60
+
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,251 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gauguin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ania Slimak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-19 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
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
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: codeclimate-test-reporter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Retrieves palette of main colors, merging similar colors using Lab color
126
+ space.
127
+ email:
128
+ - anna.slimak@lunarlogic.io
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - Guardfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - gauguin.gemspec
142
+ - lib/gauguin.rb
143
+ - lib/gauguin/color.rb
144
+ - lib/gauguin/color_space.rb
145
+ - lib/gauguin/color_space/lab_vector.rb
146
+ - lib/gauguin/color_space/rgb_vector.rb
147
+ - lib/gauguin/color_space/xyz_vector.rb
148
+ - lib/gauguin/colors_clusterer.rb
149
+ - lib/gauguin/colors_limiter.rb
150
+ - lib/gauguin/colors_retriever.rb
151
+ - lib/gauguin/image.rb
152
+ - lib/gauguin/image_recolorer.rb
153
+ - lib/gauguin/image_repository.rb
154
+ - lib/gauguin/noise_reducer.rb
155
+ - lib/gauguin/painting.rb
156
+ - lib/gauguin/palette_serializer.rb
157
+ - lib/gauguin/version.rb
158
+ - spec/integration/painting_spec.rb
159
+ - spec/integration/samples_spec.rb
160
+ - spec/lib/gauguin/color_space/rgb_vector_spec.rb
161
+ - spec/lib/gauguin/color_space/xyz_vector_spec.rb
162
+ - spec/lib/gauguin/color_spec.rb
163
+ - spec/lib/gauguin/colors_clusterer_spec.rb
164
+ - spec/lib/gauguin/colors_limiter_spec.rb
165
+ - spec/lib/gauguin/colors_retriever_spec.rb
166
+ - spec/lib/gauguin/image_recolorer_spec.rb
167
+ - spec/lib/gauguin/image_repository_spec.rb
168
+ - spec/lib/gauguin/image_spec.rb
169
+ - spec/lib/gauguin/noise_reducer_spec.rb
170
+ - spec/lib/gauguin/painting_spec.rb
171
+ - spec/lib/gauguin/palette_serializer_spec.rb
172
+ - spec/spec_helper.rb
173
+ - spec/support/pictures/10_colors.png
174
+ - spec/support/pictures/12_colors.png
175
+ - spec/support/pictures/gauguin.png
176
+ - spec/support/pictures/gray_and_black.png
177
+ - spec/support/pictures/not_unique_colors.png
178
+ - spec/support/pictures/samples/sample1.png
179
+ - spec/support/pictures/samples/sample10.png
180
+ - spec/support/pictures/samples/sample11.png
181
+ - spec/support/pictures/samples/sample2.png
182
+ - spec/support/pictures/samples/sample3.png
183
+ - spec/support/pictures/samples/sample4.png
184
+ - spec/support/pictures/samples/sample5.png
185
+ - spec/support/pictures/samples/sample6.png
186
+ - spec/support/pictures/samples/sample7.png
187
+ - spec/support/pictures/samples/sample8.png
188
+ - spec/support/pictures/samples/sample9.png
189
+ - spec/support/pictures/too_many_colors.png
190
+ - spec/support/pictures/transparent_background.png
191
+ - spec/support/pictures/unique_colors.png
192
+ homepage: https://github.com/LunarLogic/gauguin
193
+ licenses:
194
+ - MIT
195
+ metadata: {}
196
+ post_install_message:
197
+ rdoc_options: []
198
+ require_paths:
199
+ - lib
200
+ required_ruby_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - ">="
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
210
+ requirements: []
211
+ rubyforge_project:
212
+ rubygems_version: 2.2.2
213
+ signing_key:
214
+ specification_version: 4
215
+ summary: Tool for retrieving main colors from the image.
216
+ test_files:
217
+ - spec/integration/painting_spec.rb
218
+ - spec/integration/samples_spec.rb
219
+ - spec/lib/gauguin/color_space/rgb_vector_spec.rb
220
+ - spec/lib/gauguin/color_space/xyz_vector_spec.rb
221
+ - spec/lib/gauguin/color_spec.rb
222
+ - spec/lib/gauguin/colors_clusterer_spec.rb
223
+ - spec/lib/gauguin/colors_limiter_spec.rb
224
+ - spec/lib/gauguin/colors_retriever_spec.rb
225
+ - spec/lib/gauguin/image_recolorer_spec.rb
226
+ - spec/lib/gauguin/image_repository_spec.rb
227
+ - spec/lib/gauguin/image_spec.rb
228
+ - spec/lib/gauguin/noise_reducer_spec.rb
229
+ - spec/lib/gauguin/painting_spec.rb
230
+ - spec/lib/gauguin/palette_serializer_spec.rb
231
+ - spec/spec_helper.rb
232
+ - spec/support/pictures/10_colors.png
233
+ - spec/support/pictures/12_colors.png
234
+ - spec/support/pictures/gauguin.png
235
+ - spec/support/pictures/gray_and_black.png
236
+ - spec/support/pictures/not_unique_colors.png
237
+ - spec/support/pictures/samples/sample1.png
238
+ - spec/support/pictures/samples/sample10.png
239
+ - spec/support/pictures/samples/sample11.png
240
+ - spec/support/pictures/samples/sample2.png
241
+ - spec/support/pictures/samples/sample3.png
242
+ - spec/support/pictures/samples/sample4.png
243
+ - spec/support/pictures/samples/sample5.png
244
+ - spec/support/pictures/samples/sample6.png
245
+ - spec/support/pictures/samples/sample7.png
246
+ - spec/support/pictures/samples/sample8.png
247
+ - spec/support/pictures/samples/sample9.png
248
+ - spec/support/pictures/too_many_colors.png
249
+ - spec/support/pictures/transparent_background.png
250
+ - spec/support/pictures/unique_colors.png
251
+ has_rdoc: