falu 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/falu/dithered_palette.rb +25 -1
- data/lib/falu/image_palette.rb +20 -0
- data/lib/falu/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f08793b49c9cd82d6a486fd2903dc1041a1a2f73
|
4
|
+
data.tar.gz: 309a1a89a69629163ad945f854c1577dd6607d34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2ed04569779b47dddf8782a6bc08aba512300bb13b111abbf5ab890844d0bc906ee3b8e2db9537381a3d80d0594a04c0548b7ab4068d5e40487af9f2e203db1
|
7
|
+
data.tar.gz: 704decaa5d473ee1c5516a332b70d211896ec8af94396c7f9bdd7a77de207491853b4e0ad610a532ccdf99069d37be63bfa95c563bc43c68a7209090dc0ddb94
|
@@ -1,5 +1,6 @@
|
|
1
1
|
module Falu
|
2
2
|
class DitheredPalette < ImagePalette
|
3
|
+
|
3
4
|
delegate :colors, to: :configuration
|
4
5
|
|
5
6
|
def initialize(image, swatches=nil, **opts)
|
@@ -8,8 +9,31 @@ module Falu
|
|
8
9
|
end
|
9
10
|
|
10
11
|
def swatches
|
11
|
-
|
12
|
+
if @swatches.empty?
|
13
|
+
@swatches = image.dither(colors, scale: (colors*10), unique: true).sample(0, 0, size: 10, sample: true).map { |swatch| Falu::Swatch.new(swatch[0], 0) }
|
14
|
+
image.dither(colors, scale: scale).sample(0, 0, size: size, sample: sample).each do |clr|
|
15
|
+
color = Falu::Color.new(clr[0])
|
16
|
+
exact = @swatches.index { |s| s.color.hex.to_s == color.hex.to_s }
|
17
|
+
unless exact.nil?
|
18
|
+
@swatches[exact] += clr[1]
|
19
|
+
next
|
20
|
+
end
|
21
|
+
|
22
|
+
@swatches.sort { |a,b| b.count <=> a.count }.each_with_index do |swatch, s|
|
23
|
+
if [:red, :green, :blue].all? { |c| (swatch.color.rgb.send(c) - color.rgb.send(c)).abs <= 10 }
|
24
|
+
@swatches[s] += clr[1]
|
25
|
+
break
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
12
30
|
@swatches
|
13
31
|
end
|
32
|
+
|
33
|
+
def as_json(options={})
|
34
|
+
super(options).merge({
|
35
|
+
colors: colors
|
36
|
+
})
|
37
|
+
end
|
14
38
|
end
|
15
39
|
end
|
data/lib/falu/image_palette.rb
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
module Falu
|
2
2
|
class ImagePalette < Palette
|
3
|
+
class << self
|
4
|
+
def dump(palette)
|
5
|
+
palette.as_json
|
6
|
+
end
|
7
|
+
|
8
|
+
def load(json)
|
9
|
+
return if json.nil?
|
10
|
+
json.symbolize_keys!
|
11
|
+
new(json.delete(:image), json.delete(:swatches), **json)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
3
15
|
delegate :sample, :scale, :size, to: :configuration
|
4
16
|
|
5
17
|
def initialize(image, swatches=nil, **opts)
|
@@ -17,5 +29,13 @@ module Falu
|
|
17
29
|
@swatches = image.scale(scale).sample(0, 0, size: size, sample: sample).map { |swatch| Falu::Swatch.new(*swatch) } if @swatches.empty?
|
18
30
|
@swatches
|
19
31
|
end
|
32
|
+
|
33
|
+
def as_json(options={})
|
34
|
+
super(options).merge({
|
35
|
+
sample: sample,
|
36
|
+
scale: scale,
|
37
|
+
size: size
|
38
|
+
})
|
39
|
+
end
|
20
40
|
end
|
21
41
|
end
|
data/lib/falu/version.rb
CHANGED