Sha256: 71f78c3e1fae024137dd9d654d9e96f2c8f98b7ab3fe74bcac48ffde35823cdb

Contents?: true

Size: 727 Bytes

Versions: 10

Compression:

Stored size: 727 Bytes

Contents

require 'zlib'
module PulseMeter
  module DygraphsVisualize
    class SeriesColor
      def initialize(options = {})
        @forced_color = options[:color]
        @name = options[:name] || rand.to_s
      end

      def color
        @color ||= calculate_color 
      end

      private

      def calculate_color
        @forced_color || calculate_color_by_name
      end

      def calculate_color_by_name
        '#' + hashed_color_bytes.map{|b| '%02x' % darken(b)}.join
      end

      def hashed_color_bytes
        n = Zlib::crc32(@name)
        [
          n & 0xFF,
          (n & 0xFF00) >> 8,
          (n & 0xFF0000) >> 16
        ]
      end

      def darken(byte)
        byte / 2
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pulse_meter-dygraphs_visualizer-0.4.25 lib/pulse_meter/dygraphs_visualize/series_color.rb
pulse_meter-dygraphs_visualizer-0.4.23 lib/pulse_meter/dygraphs_visualize/series_color.rb
pulse_meter-dygraphs_visualizer-0.4.22 lib/pulse_meter/dygraphs_visualize/series_color.rb
pulse_meter-dygraphs_visualizer-0.4.21 lib/pulse_meter/dygraphs_visualize/series_color.rb
pulse_meter-dygraphs_visualizer-0.4.20 lib/pulse_meter/dygraphs_visualize/series_color.rb
pulse_meter-dygraphs_visualizer-0.4.19 lib/pulse_meter/dygraphs_visualize/series_color.rb
pulse_meter-dygraphs_visualizer-0.4.18 lib/pulse_meter/dygraphs_visualize/series_color.rb
pulse_meter-dygraphs_visualizer-0.4.17 lib/pulse_meter/dygraphs_visualize/series_color.rb
pulse_meter-dygraphs_visualizer-0.4.16 lib/pulse_meter/dygraphs_visualize/series_color.rb
pulse_meter-dygraphs_visualizer-0.4.15 lib/pulse_meter/dygraphs_visualize/series_color.rb