Class: Miro::DominantColors

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_image_path, image_type = nil) ⇒ DominantColors

Returns a new instance of DominantColors.



7
8
9
10
# File 'lib/miro/dominant_colors.rb', line 7

def initialize(src_image_path, image_type = nil)
  @src_image_path = src_image_path
  @image_type = image_type
end

Instance Attribute Details

#src_image_pathObject

Returns the value of attribute src_image_path.



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

def src_image_path
  @src_image_path
end

Instance Method Details

#by_percentageObject



39
40
41
42
43
44
# File 'lib/miro/dominant_colors.rb', line 39

def by_percentage
  return nil if Miro.histogram?
  sorted_pixels
  pixel_count = @pixels.size
  sorted_pixels.collect { |pixel| @grouped_pixels[pixel].size / pixel_count.to_f }
end

#histogramObject



50
51
52
# File 'lib/miro/dominant_colors.rb', line 50

def histogram
  @histogram ||= downsample_and_histogram.sort_by { |item| item[0]  }.reverse
end

#sorted_pixelsObject



46
47
48
# File 'lib/miro/dominant_colors.rb', line 46

def sorted_pixels
  @sorted_pixels ||= extract_colors_from_image
end

#to_cmykObject



31
32
33
# File 'lib/miro/dominant_colors.rb', line 31

def to_cmyk
  histogram.map { |item| item[1].to_cmyk.to_a } if Miro.histogram?
end

#to_hexObject



12
13
14
15
# File 'lib/miro/dominant_colors.rb', line 12

def to_hex
  return histogram.map{ |item| item[1].html } if Miro.histogram?
  sorted_pixels.collect { |pixel| ChunkyPNG::Color.to_hex(pixel, false) }
end

#to_hslObject



27
28
29
# File 'lib/miro/dominant_colors.rb', line 27

def to_hsl
  histogram.map { |item| item[1].to_hsl.to_a } if Miro.histogram?
end

#to_rgbObject



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

def to_rgb
  return histogram.map { |item| item[1].to_rgb.to_a } if Miro.histogram?
  sorted_pixels.collect { |pixel| ChunkyPNG::Color.to_truecolor_bytes(pixel) }
end

#to_rgbaObject



22
23
24
25
# File 'lib/miro/dominant_colors.rb', line 22

def to_rgba
  return histogram.map { |item| item[1].css_rgba } if Miro.histogram?
  sorted_pixels.collect { |pixel| ChunkyPNG::Color.to_truecolor_alpha_bytes(pixel) }
end

#to_yiqObject



35
36
37
# File 'lib/miro/dominant_colors.rb', line 35

def to_yiq
  histogram.map { |item| item[1].to_yiq.to_a } if Miro.histogram?
end