Sha256: 2690d64b05e1ee17eb4732bab6e7a6336fc89c5e8f1a11aa0a12b01cae20f958

Contents?: true

Size: 887 Bytes

Versions: 5

Compression:

Stored size: 887 Bytes

Contents

# -*- coding: utf-8 -*-

class RailsDataExplorer
  module Utils

    # Responsibilities:
    #  * Map an input value between -1 and 1 to a color on a color scale.
    #
    class ColorScale

      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

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

      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

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails-data-explorer-1.0.4 lib/rails_data_explorer/utils/color_scale.rb
rails-data-explorer-1.0.3 lib/rails_data_explorer/utils/color_scale.rb
rails-data-explorer-1.0.2 lib/rails_data_explorer/utils/color_scale.rb
rails-data-explorer-1.0.1 lib/rails_data_explorer/utils/color_scale.rb
rails-data-explorer-1.0.0 lib/rails_data_explorer/utils/color_scale.rb