Class: ClusterCalculator::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/quadtone/cluster_calculator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(center) ⇒ Cluster

Returns a new instance of Cluster.



13
14
15
16
17
# File 'lib/quadtone/cluster_calculator.rb', line 13

def initialize(center)
  @center = center
  @samples = []
  @moved = true
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



9
10
11
# File 'lib/quadtone/cluster_calculator.rb', line 9

def center
  @center
end

#movedObject

Returns the value of attribute moved.



11
12
13
# File 'lib/quadtone/cluster_calculator.rb', line 11

def moved
  @moved
end

#samplesObject

Returns the value of attribute samples.



10
11
12
# File 'lib/quadtone/cluster_calculator.rb', line 10

def samples
  @samples
end

Instance Method Details

#add_sample(sample) ⇒ Object



19
20
21
# File 'lib/quadtone/cluster_calculator.rb', line 19

def add_sample(sample)
  @samples << sample
end

#clear_samplesObject



23
24
25
# File 'lib/quadtone/cluster_calculator.rb', line 23

def clear_samples
  @samples = []
end

#distance_to(sample) ⇒ Object



27
28
29
# File 'lib/quadtone/cluster_calculator.rb', line 27

def distance_to(sample)
  @center.delta_e(sample.output)
end

#sizeObject



40
41
42
# File 'lib/quadtone/cluster_calculator.rb', line 40

def size
  @samples.length
end

#update_center(delta = 0.001) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/quadtone/cluster_calculator.rb', line 31

def update_center(delta=0.001)
  @moved = false
  average, error = Color::Lab.average(@samples.map(&:output))
  unless average.delta_e(@center) < delta
    @center = average
    @moved = true
  end
end