falu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ module Falu
2
+ class ImagePalette < Palette
3
+ delegate :sample, :scale, :size, to: :configuration
4
+
5
+ def initialize(image, swatches=nil, **opts)
6
+ configuration.configure({sample: nil, scale: 500, size: 10})
7
+ super(swatches, **opts)
8
+ @image = image
9
+ end
10
+
11
+ def image
12
+ @image = Falu::Image.new(@image) unless @image.is_a?(Falu::Image)
13
+ @image
14
+ end
15
+
16
+ def swatches
17
+ @swatches = image.scale(scale).sample(0, 0, size: size, sample: sample).map { |swatch| Falu::Swatch.new(*swatch) } if @swatches.empty?
18
+ @swatches
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,145 @@
1
+ module Falu
2
+ class Palette
3
+ include Canfig::Instance
4
+ include Enumerable
5
+
6
+ attr_reader :swatches
7
+
8
+ class << self
9
+ def dump(palette)
10
+ palette.as_json
11
+ end
12
+
13
+ def load(json)
14
+ return if json.nil?
15
+ json.symbolize_keys!
16
+ new(json.delete(:swatches), **json)
17
+ end
18
+ end
19
+
20
+ def initialize(swatches=nil, **opts)
21
+ @swatches = (swatches || []).map { |swatch| swatch.is_a?(Falu::Swatch) ? swatch : Falu::Swatch.new(*swatch) }
22
+
23
+ configuration.configure(opts.slice!(:primary, :secondary, :accent))
24
+
25
+ self.primary = opts[:primary]
26
+ self.secondary = opts[:secondary]
27
+ self.accent = opts[:accent]
28
+ end
29
+
30
+ def each(&block)
31
+ swatches.each(&block)
32
+ end
33
+
34
+ def map(&block)
35
+ swatches.map(&block)
36
+ end
37
+
38
+ def sum(*args, &block)
39
+ swatches.sum(*args, &block)
40
+ end
41
+
42
+ def <<(swatch)
43
+ swatches << (swatch.is_a?(Falu::Swatch) ? swatch : Falu::Swatch.new(*swatch))
44
+ end
45
+
46
+ def reverse!
47
+ swatches.reverse!
48
+ self
49
+ end
50
+
51
+ def sort!(*args, &block)
52
+ if args.empty? && !block_given?
53
+ swatches.sort_by!(&:count).reverse!
54
+ else
55
+ swatches.sort!(*args, &block)
56
+ end
57
+ self
58
+ end
59
+
60
+ def sort_by!(*args, &block)
61
+ if args.empty? && !block_given?
62
+ swatches.sort_by!(&:count).reverse!
63
+ else
64
+ swatches.sort_by!(*args, &block)
65
+ end
66
+ self
67
+ end
68
+
69
+ def lightest(*args)
70
+ swatches.sort_by { |swatch| swatch.color.hsl.lightness }.reverse.first(*args)
71
+ end
72
+
73
+ def darkest(*args)
74
+ swatches.sort_by { |swatch| swatch.color.hsl.lightness }.first(*args)
75
+ end
76
+
77
+ def reds(*args)
78
+ #swatches.sort_by { |swatch| swatch.color.rgb.red }.reverse.first(*args)
79
+ swatches.sort do |a,b|
80
+ ared = a.color.rgb.red - (a.color.rgb.blue + a.color.rgb.green)
81
+ bred = b.color.rgb.red - (b.color.rgb.blue + b.color.rgb.green)
82
+
83
+ ared <=> bred
84
+ end.first(*args)
85
+ end
86
+
87
+ def greens(*args)
88
+ #swatches.sort_by { |swatch| swatch.color.rgb.green }.reverse.first(*args)
89
+ swatches.sort do |a,b|
90
+ agrn = a.color.rgb.green - (a.color.rgb.blue + a.color.rgb.red)
91
+ bgrn = b.color.rgb.green - (b.color.rgb.blue + b.color.rgb.red)
92
+
93
+ agrn <=> bgrn
94
+ end.first(*args)
95
+ end
96
+
97
+ def blues(*args)
98
+ #swatches.sort_by { |swatch| swatch.color.rgb.blue }.reverse.first(*args)
99
+ swatches.sort do |a,b|
100
+ ablu = a.color.rgb.blue - (a.color.rgb.red + a.color.rgb.green)
101
+ bblu = b.color.rgb.blue - (b.color.rgb.red + b.color.rgb.green)
102
+
103
+ ablu <=> bblu
104
+ end.first(*args)
105
+ end
106
+
107
+ def dominant(*args)
108
+ swatches.sort_by(&:count).reverse.first(*args)
109
+ end
110
+
111
+ def primary=(pri)
112
+ # TODO check that the color exists in the palette
113
+ @primary = pri
114
+ end
115
+
116
+ def primary
117
+ @primary ||= dominant
118
+ end
119
+
120
+ def secondary=(sec)
121
+ # TODO check that the color exists in the palette
122
+ @secondary = sec
123
+ end
124
+
125
+ def secondary
126
+ @secondary ||= dominant(2).last
127
+ end
128
+
129
+ def accent=(acc)
130
+ # TODO check that the color exists in the palette
131
+ @accent = acc
132
+ end
133
+
134
+ def accent
135
+ @accent ||= dominant(3).last
136
+ end
137
+
138
+ def as_json(options={})
139
+ { primary: primary.to_s,
140
+ secondary: secondary.to_s,
141
+ accent: accent.to_s,
142
+ swatches: swatches.as_json }
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,64 @@
1
+ module Falu
2
+ class Swatch
3
+ attr_reader :color, :count
4
+
5
+ delegate :name, to: :color
6
+
7
+ class << self
8
+ def dump(swatch)
9
+ swatch.as_json
10
+ end
11
+
12
+ def load(json)
13
+ json.symbolize_keys!
14
+ new(json[:color].values.first, json[:count])
15
+ end
16
+ end
17
+
18
+ def initialize(color, count=0)
19
+ @color = color.is_a?(Falu::Color) ? color : Falu::Color.new(color)
20
+ @count = count.to_i
21
+ end
22
+
23
+ def <=>(other)
24
+ if count == other.count
25
+ color <=> color
26
+ else
27
+ count <=> other.count
28
+ end
29
+ end
30
+
31
+ def ==(other)
32
+ to_s == other.to_s
33
+ end
34
+
35
+ def +(cnt)
36
+ self.class.new(color, (count + cnt.to_i))
37
+ end
38
+
39
+ def -(cnt)
40
+ self.class.new(color, (count - cnt.to_i))
41
+ end
42
+
43
+ def to_i
44
+ count
45
+ end
46
+
47
+ def to_s
48
+ color.to_s
49
+ end
50
+
51
+ def to_a
52
+ [to_s, to_i]
53
+ end
54
+
55
+ def to_h
56
+ {color: to_s, count: to_i}
57
+ end
58
+
59
+ def as_json(options={})
60
+ #{color: color.as_json, count: count}
61
+ to_a
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,14 @@
1
+ module Falu
2
+ module Version
3
+ MAJOR = '0'
4
+ MINOR = '0'
5
+ PATCH = '1'
6
+ VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+
8
+ class << self
9
+ def inspect
10
+ VERSION
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,71 @@
1
+ require 'falu'
2
+ require 'rspec'
3
+ require 'coveralls'
4
+ Coveralls.wear!
5
+
6
+
7
+ RSpec.configure do |config|
8
+ # rspec-expectations config goes here. You can use an alternate
9
+ # assertion/expectation library such as wrong or the stdlib/minitest
10
+ # assertions if you prefer.
11
+ config.expect_with :rspec do |expectations|
12
+ # This option will default to `true` in RSpec 4. It makes the `description`
13
+ # and `failure_message` of custom matchers include text for helper methods
14
+ # defined using `chain`, e.g.:
15
+ # be_bigger_than(2).and_smaller_than(4).description
16
+ # # => "be bigger than 2 and smaller than 4"
17
+ # ...rather than:
18
+ # # => "be bigger than 2"
19
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
20
+ end
21
+
22
+ # rspec-mocks config goes here. You can use an alternate test double
23
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
24
+ config.mock_with :rspec do |mocks|
25
+ # Prevents you from mocking or stubbing a method that does not exist on
26
+ # a real object. This is generally recommended, and will default to
27
+ # `true` in RSpec 4.
28
+ mocks.verify_partial_doubles = true
29
+ end
30
+
31
+ # Many RSpec users commonly either run the entire suite or an individual
32
+ # file, and it's useful to allow more verbose output when running an
33
+ # individual spec file.
34
+ if config.files_to_run.one?
35
+ # Use the documentation formatter for detailed output,
36
+ # unless a formatter has already been configured
37
+ # (e.g. via a command-line flag).
38
+ config.default_formatter = 'doc'
39
+ end
40
+
41
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
42
+ # For more details, see:
43
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
44
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
45
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
46
+ config.disable_monkey_patching!
47
+
48
+ # Run specs in random order to surface order dependencies. If you find an
49
+ # order dependency and want to debug it, you can fix the order by providing
50
+ # the seed, which is printed after each run.
51
+ # --seed 1234
52
+ config.order = :random
53
+
54
+ # Seed global randomization in this process using the `--seed` CLI option.
55
+ # Setting this allows you to use `--seed` to deterministically reproduce
56
+ # test failures related to randomization by passing the same `--seed` value
57
+ # as the one that triggered the failure.
58
+ Kernel.srand config.seed
59
+
60
+ # Print the 10 slowest examples and example groups at the
61
+ # end of the spec run, to help surface which specs are running
62
+ # particularly slow.
63
+ #config.profile_examples = 10
64
+
65
+ # These two settings work together to allow you to limit a spec run
66
+ # to individual examples or groups you care about by tagging them with
67
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
68
+ # get run.
69
+ #config.filter_run :focus
70
+ #config.run_all_when_everything_filtered = true
71
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: falu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mark Rebec
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: canfig
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: mini_magick
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
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: rspec
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
+ description: Extract color swatches and full palettes from images with imagemagick
84
+ email:
85
+ - mark@markrebec.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - colors.txt
91
+ - lib/falu.rb
92
+ - lib/falu/color.rb
93
+ - lib/falu/colors/base.rb
94
+ - lib/falu/colors/hex.rb
95
+ - lib/falu/colors/hsl.rb
96
+ - lib/falu/colors/rgb.rb
97
+ - lib/falu/dithered_palette.rb
98
+ - lib/falu/image.rb
99
+ - lib/falu/image_palette.rb
100
+ - lib/falu/palette.rb
101
+ - lib/falu/swatch.rb
102
+ - lib/falu/version.rb
103
+ - spec/spec_helper.rb
104
+ homepage: http://github.com/markrebec/falu
105
+ licenses: []
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.8
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Extract color swatches from images with imagemagick
127
+ test_files:
128
+ - spec/spec_helper.rb
129
+ has_rdoc: