swatchman 0.1.0 → 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
  SHA256:
3
- metadata.gz: 56354a93a4c024f57eb519c087816d098235a1cb6bee53d2c088d287856efffc
4
- data.tar.gz: f6325a28b81d059e537cb0da7c3bf7f38b3227fad401bb69784694a56277336d
3
+ metadata.gz: 5c18c4e66eee494aceafd3883bdcc598592b21c92017b434249721cf5bf34f8f
4
+ data.tar.gz: 95954764b4bafc08349f528010ef300cbd75d0c819323e1cd078c1584bcfbb2f
5
5
  SHA512:
6
- metadata.gz: bafe1fcc02e4425e9ac51c48d8e3b596dd94196cc8aac345f20557335c98c56f87a69bbe9814da3e774a45e9c8ef2f824c75c421db8ed95cef6a7ebf9664f8ad
7
- data.tar.gz: 25c0be6e5f884ade52348caf19916a77fb481bfe5922b830ebf27a993bac1b4b2894d7f88d015c4fc75738f09e036832a9b0e0aef904ce64f644f7231c04354a
6
+ metadata.gz: 718c4f337fb4b9d4d4433d6f0f631078809db1f7f1eca172e04588a1deadc6a77bfe6e7e4dcca27f4c49c71082e284e2fe4a044efa4173ee7a04ee44517af2eb
7
+ data.tar.gz: f7e1a23bcb6a65fd75bf99a4c49036473aef5e41e46c5a7691cae94c62de5ebe2d7e8a249c25352973a382281833d61e2986a179ab6ff53a48027c07d46bd0d7
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Simple gem for extracting swatches from an image with MiniMagick. Allows you to specify the number of swatches to extract.
4
4
 
5
+ ## Dependencies
6
+
7
+ This gem makes use of the `mini_magick` gem, and therefore requires an installation of ImageMagick.
8
+
5
9
  ## Installation
6
10
 
7
11
  Add this line to your application's Gemfile:
@@ -40,6 +44,83 @@ Swatchman::Image.new('/some/file/path.png').palette.swatches
40
44
  Swatchman::Image.new('/some/file/path.png').palette(size: 5).swatches
41
45
  ```
42
46
 
47
+ ## API
48
+
49
+ ### `Swatchman::Image`
50
+
51
+ #### `new(path_or_url)`
52
+
53
+ ##### Parameters
54
+
55
+ * `path_or_url` (String) — A local file system path, or URL.
56
+
57
+ ##### Returns
58
+
59
+ * `Swatchman::Image` — Image instance.
60
+
61
+ #### `palette(size: 3)`
62
+
63
+ ##### Parameters
64
+
65
+ * `size` (Integer) — The amount of swatches this palette should contain. Defaults to `3`.
66
+
67
+ ##### Returns
68
+
69
+ * `Swatchman::Palette` — Palette instance.
70
+
71
+
72
+ ### `Swatchman::Palette`
73
+
74
+ #### `new(histogram, size)`
75
+
76
+ ##### Parameters
77
+
78
+ * `histogram` (String) — An ImageMagick histogram string.
79
+ * `size` (String) — The amount of swatches in the palette.
80
+
81
+ ##### Returns
82
+
83
+ * `Swatchman::Palette` — Palette instance.
84
+
85
+ #### `size`
86
+
87
+ ##### Returns
88
+
89
+ * `Integer` — The number of swatches in the palette.
90
+
91
+ #### `swatches`
92
+
93
+ ##### Returns
94
+
95
+ * `Array` — Array of `Swatchman::Swatch` instances.
96
+
97
+
98
+ ### `Swatchman::Swatch`
99
+
100
+ #### `new(color, frequency)`
101
+
102
+ ##### Parameters
103
+
104
+ * `color` — The hex color value.
105
+ * `frequency` — The frequency it appears within the image.
106
+
107
+ ##### Returns
108
+
109
+ * `Swatchman::Swatch` — Swatch instance.
110
+
111
+ #### `color`
112
+
113
+ ##### Returns
114
+
115
+ * `String` — Hex color value.
116
+
117
+ #### `frequency`
118
+
119
+ ##### Returns
120
+
121
+ * `Integer` — The frequency it appears.
122
+
123
+
43
124
  ## Development
44
125
 
45
126
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -48,7 +129,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
48
129
 
49
130
  ## Contributing
50
131
 
51
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/swatchman. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/swatchman/blob/master/CODE_OF_CONDUCT.md).
132
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/swatchman. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/livelink/swatchman/blob/master/CODE_OF_CONDUCT.md).
52
133
 
53
134
 
54
135
  ## License
@@ -2,7 +2,7 @@ require 'swatchman/swatch'
2
2
 
3
3
  module Swatchman
4
4
  class Palette
5
- MATCH = /([0-9]+)\:.*(\#[0-9ABCDEF]{6})/
5
+ attr_reader :size
6
6
 
7
7
  def initialize(histogram, size)
8
8
  @histogram = histogram
@@ -10,20 +10,25 @@ module Swatchman
10
10
  end
11
11
 
12
12
  def swatches
13
- swatches = histogram.scan(MATCH).map do |frequency, color|
14
- Swatch.new(color, frequency.to_i)
15
- end
16
-
17
- total = swatches.map(&:frequency).reduce(:+).to_f
18
-
19
- swatches
20
- .sort_by { |swatch| swatch.frequency / total }
21
- .reverse
22
- .slice(0, size)
13
+ histogram_swatches.sort_by do |swatch|
14
+ swatch.frequency / total
15
+ end.reverse.slice(0, size)
23
16
  end
24
17
 
25
18
  private
26
19
 
27
- attr_reader :histogram, :size
20
+ attr_reader :histogram
21
+
22
+ def histogram_swatches
23
+ @histogram_swatches ||= histogram.scan(
24
+ /([0-9]+)\:.*(\#[0-9ABCDEF]{6})/
25
+ ).map do |frequency, color|
26
+ Swatch.new(color, frequency.to_i)
27
+ end
28
+ end
29
+
30
+ def total
31
+ @total ||= histogram_swatches.map(&:frequency).reduce(:+).to_f
32
+ end
28
33
  end
29
34
  end
@@ -1,3 +1,3 @@
1
1
  module Swatchman
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swatchman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamie Hill