Class: FoggyMirror::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/foggy-mirror/processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image_path, resolution: DEFAULT_RESOLUTION, overlap: 0.5, distribution: nil, random_offset: 0.5, random: Random.new, adapter: default_adapter, adapter_options: {}) ⇒ Processor

Returns a new instance of Processor.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/foggy-mirror/processor.rb', line 7

def initialize(image_path, resolution: DEFAULT_RESOLUTION, overlap: 0.5, distribution: nil, random_offset: 0.5, random: Random.new, adapter: default_adapter, adapter_options: {})
  @image_path = image_path

  @resolution = resolution
  @overlap = overlap
  @distribution = distribution
  @random_offset = random_offset

  @random = random
  @adapter = resolve_adapter(adapter).new(image_path, **adapter_options)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



5
6
7
# File 'lib/foggy-mirror/processor.rb', line 5

def adapter
  @adapter
end

#image_pathObject (readonly)

Returns the value of attribute image_path.



5
6
7
# File 'lib/foggy-mirror/processor.rb', line 5

def image_path
  @image_path
end

#overlapObject (readonly)

Returns the value of attribute overlap.



5
6
7
# File 'lib/foggy-mirror/processor.rb', line 5

def overlap
  @overlap
end

#randomObject (readonly)

Returns the value of attribute random.



5
6
7
# File 'lib/foggy-mirror/processor.rb', line 5

def random
  @random
end

#random_offsetObject (readonly)

Returns the value of attribute random_offset.



5
6
7
# File 'lib/foggy-mirror/processor.rb', line 5

def random_offset
  @random_offset
end

#resolutionObject (readonly)

Returns the value of attribute resolution.



5
6
7
# File 'lib/foggy-mirror/processor.rb', line 5

def resolution
  @resolution
end

Instance Method Details

#base_colorObject



19
20
21
# File 'lib/foggy-mirror/processor.rb', line 19

def base_color
  @base_color ||= Color.new(adapter.dominant_color)
end

#blobsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/foggy-mirror/processor.rb', line 31

def blobs
  samples = color_samples

  increment = 1.0 / (resolution - 1)

  blobs = resolution.times.with_object([]) do |y, blobs|
    resolution.times do |x|
      xp = x * increment + increment * random_offset * (random.rand - 0.5)
      yp = y * increment + increment * random_offset * (random.rand - 0.5)
      r = increment * (1 + overlap)
      color = samples[y * resolution + x]

      blobs << Blob.new(x: xp, y: yp, r: r, color: color)
    end
  end

  case @distribution.to_s.downcase
  when 'shuffle'
    blobs.shuffle(random: random)
  when 'spiral_in'
    spiral_in(blobs)
  when 'spiral_out'
    spiral_in(blobs).reverse
  when 'scan_reverse'
    blobs.reverse
  when 'brightness'
    blobs.sort_by { |b| b.color.brightness }
  when 'brightness_reverse'
    blobs.sort_by { |b| 255 - b.color.brightness }
  else
    blobs
  end
end

#color_samplesObject



69
70
71
# File 'lib/foggy-mirror/processor.rb', line 69

def color_samples
  @color_samples ||= adapter.color_samples(resolution)
end

#data_uriObject



65
66
67
# File 'lib/foggy-mirror/processor.rb', line 65

def data_uri
  @data_uri ||= adapter.data_uri(resolution)
end

#to_css(hash: false) ⇒ Object



23
24
25
# File 'lib/foggy-mirror/processor.rb', line 23

def to_css(hash: false)
  CSS.new(self).render(hash: hash)
end

#to_svg(strategy: nil) ⇒ Object



27
28
29
# File 'lib/foggy-mirror/processor.rb', line 27

def to_svg(strategy: nil)
  SVG.new(self).render
end