Class: ColorExtract::Analytics

Inherits:
Object
  • Object
show all
Includes:
ColorUtil
Defined in:
lib/color_extract/analytics.rb

Overview

Public: 分析图片的色彩信息,相似的颜色会合并在一起

Example:

img = "http://distilleryimage2...jpg"
colors = ColorExtract::Analytics.new( img ).valuable_colors
colors.each do |percent, color|
  puts "#{color.html} : #{percent}"
end

Constant Summary

Constants included from ColorUtil

ColorUtil::K, ColorUtil::X, ColorUtil::Y, ColorUtil::Z

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ColorUtil

#darken, #darken!, #dither, #hue_similarity, #lighten, #lighten!, #pure, #readable_textcolor, #reverse_color, #rgb2lab, #rgb_xyz, #similarity, #xyz_lab

Constructor Details

#initialize(img) ⇒ Analytics

Returns a new instance of Analytics.



22
23
24
# File 'lib/color_extract/analytics.rb', line 22

def initialize( img )
  @img = img
end

Instance Attribute Details

#merge_factorObject (readonly)

Returns the value of attribute merge_factor.



20
21
22
# File 'lib/color_extract/analytics.rb', line 20

def merge_factor
  @merge_factor
end

Instance Method Details

#valuable_colors(merge_factor: 5) ⇒ Object

Public: 获取颜色数组

  • merge_factor: 颜色合并系数,数字越大,相似的颜色越容易被合并在一起

Returns 颜色数组,数组的每个元素都是 [percent, color] 结构



31
32
33
34
# File 'lib/color_extract/analytics.rb', line 31

def valuable_colors( merge_factor: 5 )
  @merge_factor = merge_factor
  @colors ||= calc_valuable_colors
end

#visible_colorsObject

Public: 获取可见的颜色,这里的颜色是没有经过相似合并的

Returns 可见颜色数组,数组每个元素都是 [percent, color] 结构



65
66
67
68
69
70
71
# File 'lib/color_extract/analytics.rb', line 65

def visible_colors
  Colorscore::Histogram.new( @img, MAX_VISIBLE_COLORS).scores.reject do |per, color|
    # 由于不知道的原因(待查),colorscore 返回的队列中
    # 有些颜色是 nil, 这些应该去除,以免影响后续计算。
    color.nil?
  end
end