Class: Graphics::Rainbow
- Inherits:
-
Object
- Object
- Graphics::Rainbow
- Defined in:
- lib/graphics/rainbows.rb
Overview
Creates a range of colors all at once. See #initialize_rainbow.
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#cache_colors ⇒ Object
:nodoc:.
-
#clamp(d, min, max) ⇒ Object
:nodoc:.
-
#color(d, min = 0, max = 360) ⇒ Object
:nodoc:.
-
#initialize ⇒ Rainbow
constructor
:nodoc:.
-
#scale(d, min, max) ⇒ Object
Takes a value and a range, and scales that range to 0-360.
Constructor Details
#initialize ⇒ Rainbow
:nodoc:
7 8 9 |
# File 'lib/graphics/rainbows.rb', line 7 def initialize # :nodoc: @cache = self.cache_colors end |
Instance Attribute Details
#cache ⇒ Object (readonly)
:nodoc:
5 6 7 |
# File 'lib/graphics/rainbows.rb', line 5 def cache @cache end |
Instance Method Details
#cache_colors ⇒ Object
:nodoc:
28 29 30 31 32 33 34 35 |
# File 'lib/graphics/rainbows.rb', line 28 def cache_colors # :nodoc: # Saves all the colors to a hash cache = {} (0..360).each do |degree| cache[degree] = _color degree end cache end |
#clamp(d, min, max) ⇒ Object
:nodoc:
11 12 13 |
# File 'lib/graphics/rainbows.rb', line 11 def clamp d, min, max # :nodoc: [[min, d].max, max].min end |
#color(d, min = 0, max = 360) ⇒ Object
:nodoc:
37 38 39 40 |
# File 'lib/graphics/rainbows.rb', line 37 def color d, min=0, max=360 # :nodoc: scaled = scale d, min, max @cache[scaled] end |
#scale(d, min, max) ⇒ Object
Takes a value and a range, and scales that range to 0-360
18 19 20 21 22 23 24 25 26 |
# File 'lib/graphics/rainbows.rb', line 18 def scale d, min, max range = max - min if range != 0 scaled = (d.to_f / range) * 360 return clamp(scaled, 0, 360).round else 0 end end |