Class: Kolors::DominantColors

Inherits:
Object
  • Object
show all
Defined in:
lib/kolors/dominant_colors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_image_path) ⇒ DominantColors

Returns a new instance of DominantColors.



5
6
7
# File 'lib/kolors/dominant_colors.rb', line 5

def initialize(src_image_path)
  @src_image_path = src_image_path
end

Instance Attribute Details

#src_image_pathObject

Returns the value of attribute src_image_path.



3
4
5
# File 'lib/kolors/dominant_colors.rb', line 3

def src_image_path
  @src_image_path
end

Instance Method Details

#kmeans_resultObject



21
22
23
# File 'lib/kolors/dominant_colors.rb', line 21

def kmeans_result
  @kmeans_result ||= extract_colors_from_image
end

#to_facetsObject



9
10
11
12
13
14
15
# File 'lib/kolors/dominant_colors.rb', line 9

def to_facets
  # Map each centroid to key_colors
  facets = to_lab.each_with_index.collect do |color,index| 
    { key_colors[key_colors.keys.sort_by {|c| dist(color, c) }.first] => kmeans_result.clusters[index].data_items.size}
  end
  percentize(group_hashes_sum_values(facets))
end

#to_labObject



17
18
19
# File 'lib/kolors/dominant_colors.rb', line 17

def to_lab
  kmeans_result.centroids.collect{|r,g,b| Kolors::Rgb.new(r,g,b).to_lab}
end