Class: Gauguin::ColorsClusterer

Inherits:
Object
  • Object
show all
Defined in:
lib/gauguin/colors_clusterer.rb

Instance Method Summary collapse

Instance Method Details

#call(colors) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gauguin/colors_clusterer.rb', line 3

def call(colors)
  clusters = {}

  while !colors.empty?
    pivot = colors.shift
    group = [pivot]

    colors, pivot, group = find_all_similar(colors, pivot, group)

    clusters[pivot] = group
  end

  update_pivots_percentages(clusters)

  clusters
end

#clusters(colors) ⇒ Object



20
21
22
23
24
# File 'lib/gauguin/colors_clusterer.rb', line 20

def clusters(colors)
  clusters = self.call(colors)
  clusters = clusters.sort_by { |color, _| color.percentage }.reverse
  Hash[clusters[0...Gauguin.configuration.max_colors_count]]
end

#reversed_clusters(clusters) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gauguin/colors_clusterer.rb', line 26

def reversed_clusters(clusters)
  reversed_clusters = {}

  clusters.each do |pivot, group|
    group.each do |color|
      reversed_clusters[color] = pivot
    end
  end

  reversed_clusters
end