Class: Cheers::ContrastingColorPicker

Inherits:
Object
  • Object
show all
Defined in:
lib/cheers/contrasting_color_picker.rb

Constant Summary collapse

MAX_RETRIES =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(palette, *colors) ⇒ ContrastingColorPicker



8
9
10
11
# File 'lib/cheers/contrasting_color_picker.rb', line 8

def initialize(palette, *colors)
  @palette = palette.map { |c| Color.new(c) }
  @colors  = colors.map  { |c| Color.new(c) }
end

Instance Attribute Details

#colorsObject (readonly)

Returns the value of attribute colors.



6
7
8
# File 'lib/cheers/contrasting_color_picker.rb', line 6

def colors
  @colors
end

#paletteObject (readonly)

Returns the value of attribute palette.



6
7
8
# File 'lib/cheers/contrasting_color_picker.rb', line 6

def palette
  @palette
end

Instance Method Details

#pick(randomizer = Random.new) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cheers/contrasting_color_picker.rb', line 13

def pick(randomizer = Random.new)
  pick = palette.sample(random: randomizer)
  
  try = 0
  while colors.any? { |c| c.similar?(pick) } && try <= MAX_RETRIES
    try += 1
    pick = palette.sample(random: randomizer)
  end
  
  pick.to_s
end