Class: Falu::DitheredPalette
- Inherits:
-
ImagePalette
- Object
- Palette
- ImagePalette
- Falu::DitheredPalette
- Defined in:
- lib/falu/dithered_palette.rb
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
-
#initialize(image, swatches = nil, **opts) ⇒ DitheredPalette
constructor
A new instance of DitheredPalette.
- #swatches ⇒ Object
Methods inherited from ImagePalette
Methods inherited from Palette
#<<, #accent, #accent=, #blues, #darkest, #dominant, dump, #each, #greens, #lightest, load, #map, #primary, #primary=, #reds, #reverse!, #secondary, #secondary=, #sort!, #sort_by!, #sum
Constructor Details
#initialize(image, swatches = nil, **opts) ⇒ DitheredPalette
Returns a new instance of DitheredPalette.
6 7 8 9 |
# File 'lib/falu/dithered_palette.rb', line 6 def initialize(image, swatches=nil, **opts) configuration.configure({colors: 20}) super(image, swatches, **opts) end |
Instance Method Details
#as_json(options = {}) ⇒ Object
33 34 35 36 37 |
# File 'lib/falu/dithered_palette.rb', line 33 def as_json(={}) super().merge({ colors: colors }) end |
#swatches ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/falu/dithered_palette.rb', line 11 def swatches if @swatches.empty? @swatches = image.dither(colors, scale: (colors*10), unique: true).sample(0, 0, size: 10, sample: true).map { |swatch| Falu::Swatch.new(swatch[0], 0) } image.dither(colors, scale: scale).sample(0, 0, size: size, sample: sample).each do |clr| color = Falu::Color.new(clr[0]) exact = @swatches.index { |s| s.color.hex.to_s == color.hex.to_s } unless exact.nil? @swatches[exact] += clr[1] next end @swatches.sort { |a,b| b.count <=> a.count }.each_with_index do |swatch, s| if [:red, :green, :blue].all? { |c| (swatch.color.rgb.send(c) - color.rgb.send(c)).abs <= 10 } @swatches[s] += clr[1] break end end end end @swatches end |