Class: Graphics::Rainbow

Inherits:
Object
  • Object
show all
Defined in:
lib/graphics/rainbows.rb

Overview

Creates a range of colors all at once. See #initialize_rainbow.

Direct Known Subclasses

Cubehelix, Greyscale, Hue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRainbow

:nodoc:



7
8
9
# File 'lib/graphics/rainbows.rb', line 7

def initialize # :nodoc:
  @cache = self.cache_colors
end

Instance Attribute Details

#cacheObject (readonly)

:nodoc:



5
6
7
# File 'lib/graphics/rainbows.rb', line 5

def cache
  @cache
end

Instance Method Details

#cache_colorsObject

: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