Class: RailsDataExplorer::Utils::ColorScale

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_data_explorer/utils/color_scale.rb

Overview

Responsibilities:

* Map an input value between -1 and 1 to a color on a color scale.

Instance Method Summary collapse

Constructor Details

#initializeColorScale

Returns a new instance of ColorScale.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rails_data_explorer/utils/color_scale.rb', line 11

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



24
25
26
# File 'lib/rails_data_explorer/utils/color_scale.rb', line 24

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

#demoObject



28
29
30
31
32
33
34
35
# File 'lib/rails_data_explorer/utils/color_scale.rb', line 28

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