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 +4 -4
- data/README.md +82 -1
- data/lib/swatchman/palette.rb +17 -12
- data/lib/swatchman/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c18c4e66eee494aceafd3883bdcc598592b21c92017b434249721cf5bf34f8f
|
4
|
+
data.tar.gz: 95954764b4bafc08349f528010ef300cbd75d0c819323e1cd078c1584bcfbb2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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/
|
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
|
data/lib/swatchman/palette.rb
CHANGED
@@ -2,7 +2,7 @@ require 'swatchman/swatch'
|
|
2
2
|
|
3
3
|
module Swatchman
|
4
4
|
class Palette
|
5
|
-
|
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
|
-
|
14
|
-
|
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
|
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
|
data/lib/swatchman/version.rb
CHANGED