Class: Falu::ImagePalette

Inherits:
Palette
  • Object
show all
Defined in:
lib/falu/image_palette.rb

Direct Known Subclasses

DitheredPalette

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Palette

#<<, #accent, #accent=, #blues, #darkest, #dominant, #each, #greens, #lightest, #map, #primary, #primary=, #reds, #reverse!, #secondary, #secondary=, #sort!, #sort_by!, #sum

Constructor Details

#initialize(image, swatches = nil, **opts) ⇒ ImagePalette

Returns a new instance of ImagePalette.



17
18
19
20
21
# File 'lib/falu/image_palette.rb', line 17

def initialize(image, swatches=nil, **opts)
  configuration.configure({sample: nil, scale: 500, size: 10, miro: false})
  super(swatches, **opts)
  @image = image
end

Class Method Details

.dump(palette) ⇒ Object



4
5
6
# File 'lib/falu/image_palette.rb', line 4

def dump(palette)
  palette.as_json
end

.load(json) ⇒ Object



8
9
10
11
12
# File 'lib/falu/image_palette.rb', line 8

def load(json)
  return if json.nil?
  json.symbolize_keys!
  new(json.delete(:image), json.delete(:swatches), **json)
end

Instance Method Details

#as_json(options = {}) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/falu/image_palette.rb', line 37

def as_json(options={})
  super(options).merge({
    sample: sample,
    scale: scale,
    size: size,
    miro: miro
  })
end

#imageObject



23
24
25
26
# File 'lib/falu/image_palette.rb', line 23

def image
  @image = Falu::Image.new(@image) unless @image.is_a?(Falu::Image)
  @image
end

#swatchesObject



28
29
30
31
32
33
34
35
# File 'lib/falu/image_palette.rb', line 28

def swatches
  if miro
    @swatches = image.miro.map { |swatch| Falu::Swatch.new(*swatch) } if @swatches.empty?
  else
    @swatches = image.scale(scale).sample(0, 0, size: size, sample: sample).map { |swatch| Falu::Swatch.new(*swatch) } if @swatches.empty?
  end
  @swatches
end