Class: Cheers::ContrastingColorPicker
- Inherits:
-
Object
- Object
- Cheers::ContrastingColorPicker
- Defined in:
- lib/cheers/contrasting_color_picker.rb
Constant Summary collapse
- MAX_RETRIES =
10
Instance Attribute Summary collapse
-
#colors ⇒ Object
readonly
Returns the value of attribute colors.
-
#palette ⇒ Object
readonly
Returns the value of attribute palette.
Instance Method Summary collapse
-
#initialize(palette, *colors) ⇒ ContrastingColorPicker
constructor
A new instance of ContrastingColorPicker.
- #pick(randomizer = Random.new) ⇒ Object
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
#colors ⇒ Object (readonly)
Returns the value of attribute colors.
6 7 8 |
# File 'lib/cheers/contrasting_color_picker.rb', line 6 def colors @colors end |
#palette ⇒ Object (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 |