Class: Colorscore::Palette

Inherits:
Array
  • Object
show all
Defined in:
lib/colorscore/palette.rb

Constant Summary collapse

DEFAULT =
["660000", "990000", "cc0000", "cc3333", "ea4c88", "993399",
"663399", "333399", "0066cc", "0099cc", "66cccc", "77cc33",
"669900", "336600", "666600", "999900", "cccc33", "ffff00",
"ffcc33", "ff9900", "ff6600", "cc6633", "996633", "663300",
"000000", "999999", "cccccc", "ffffff"]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defaultObject



9
10
11
# File 'lib/colorscore/palette.rb', line 9

def self.default
  from_hex DEFAULT
end

.from_hex(hex_values) ⇒ Object



13
14
15
# File 'lib/colorscore/palette.rb', line 13

def self.from_hex(hex_values)
  new hex_values.map { |hex| Color::RGB.from_html(hex) }
end

Instance Method Details

#collect_scores(distance_threshold = 0.275, min_colors = 3, max_colors = 7) ⇒ Object

From a large palette, collects scores and reduces them based on the distance threshold (which can be varied to find a specific number of colors)



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/colorscore/palette.rb', line 42

def collect_scores(distance_threshold=0.275, min_colors=3, max_colors=7)
  def generate_scores(threshold)
    @res = [[1,self[0]]]
    @pivot = self[0]
    self.each do |color|
      if (Metrics.distance(@pivot, color)) > threshold
        @res << [1,color]
        @pivot = color
      else
        @res.last[0] +=1
      end
    end
    @res
  end

  @results = generate_scores(distance_threshold)

  if @results.length <= min_colors && distance_threshold > 0.05
    self.collect_scores((distance_threshold-0.05))
  elsif @results.length > max_colors && distance_threshold < 0.5
    self.collect_scores((distance_threshold+0.05))
  else
    return @results
  end
end

#generate_scores(threshold) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/colorscore/palette.rb', line 43

def generate_scores(threshold)
  @res = [[1,self[0]]]
  @pivot = self[0]
  self.each do |color|
    if (Metrics.distance(@pivot, color)) > threshold
      @res << [1,color]
      @pivot = color
    else
      @res.last[0] +=1
    end
  end
  @res
end

#scores(histogram_scores, distance_threshold = 0.275) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/colorscore/palette.rb', line 17

def scores(histogram_scores, distance_threshold=0.275)
  scores = map do |palette_color|
    score = 0

    histogram_scores.each_with_index do |item, index|
      color_score, color = *item

      color = color.to_hsl.tap { |c| c.s = 0.05 + c.s * (4 - c.l * 2.5) }.to_rgb

      if (distance = Metrics.distance(palette_color, color)) < distance_threshold
        distance_penalty = (1 - distance) ** 4
        score += color_score * distance_penalty
      end
    end

    [score, palette_color]
  end

  scores.reject { |score, color| score <= 0.05 }.
         sort_by { |score, color| score }.
         reverse
end