Class: RailsDataExplorer::Utils::ColorScale

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-data-explorer/utils/color_scale.rb

Instance Method Summary collapse

Constructor Details

#initializeColorScale

Returns a new instance of ColorScale.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rails-data-explorer/utils/color_scale.rb', line 5

def initialize
  @points = {
    -1 => Color::RGB::Red,
    -0.1 =>  Color::RGB::Black,
    0.1 =>  Color::RGB::Black,
    1 => Color::RGB::Green,
  }
  @gradient = Interpolate::Points.new(@points)
  @gradient.blend_with {|color, other, balance|
    other.mix_with(color, balance * 100.0)
  }
end

Instance Method Details

#compute(input) ⇒ Object



18
19
20
# File 'lib/rails-data-explorer/utils/color_scale.rb', line 18

def compute(input)
  @gradient.at(input).html
end

#demoObject



22
23
24
25
26
27
28
29
# File 'lib/rails-data-explorer/utils/color_scale.rb', line 22

def demo
  "<ul>".html_safe +
  (-1).step(1, 0.1).map { |e|
    color = compute(e)
    %(<li style="color: #{ color }">#{ e } (#{ color })</li>)
  }.join.html_safe +
  "</ul>".html_safe
end