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
  new DEFAULT.map { |hex| Color::RGB.from_html(hex) }
end

Instance Method Details

#scores(histogram_scores) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/colorscore/palette.rb', line 13

def scores(histogram_scores)
  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)) < 0.275
        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