Class: Huespace::MedianCut

Inherits:
Object
  • Object
show all
Defined in:
lib/huespace/median_cut.rb

Defined Under Namespace

Classes: SplitInfo

Class Method Summary collapse

Class Method Details

.process(colors, count, hist) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/huespace/median_cut.rb', line 5

def self.process(colors, count, hist)
    colors = colors.uniq # Remove duplicate colors
    groups = [colors]
    limit = [count, colors.size].min
    
    loop do
        break if groups.size >= limit

        split_info = determine_split(groups)
        group1, group2 = split_group(groups[split_info.group_index], split_info)
        groups.delete_at(split_info.group_index) # Remove group that we split by
        groups << group1 unless group1.empty?
        groups << group2 unless group2.empty?
    end
    
    palette = []
    groups.sort_by! { |group| -calc_sort_score(group, hist) }
    groups.each do |group|
        palette << average_color(group)
    end
    
    palette[0...count]
end