class Rainbow::OpacityRanges

Attributes

current_x[R]
gradient[R]
ranges[R]

Public Class Methods

new(opacity_ranges, gradient) click to toggle source
# File lib/rainbow/opacity_ranges.rb, line 8
def initialize(opacity_ranges, gradient)
  @ranges   = opacity_ranges
  @gradient = gradient

  @ranges.each { |range| range.gradient = gradient }
end

Public Instance Methods

current_x=(x) click to toggle source
# File lib/rainbow/opacity_ranges.rb, line 15
def current_x=(x)
  @current_range = get_range_by_x(x)
  @current_range.current_x = x
end

Private Instance Methods

get_range_by_x(x) click to toggle source
# File lib/rainbow/opacity_ranges.rb, line 22
def get_range_by_x(x)
  # since x is started at 0
  x += 1

  range = ranges.find { |r| r.included?(x) }
  return range if range

  first_range = ranges.first
  last_range  = ranges.last

  if first_range.from_opacity.location > 0 && first_range.from_location_in_pixel > x
    ranges.first
  elsif last_range.to_opacity.location < 100 && x > last_range.to_location_in_pixel
    ranges.last
  else
    ranges.last
    # err_msg = "(#{x}) (#{first_range.from_location_in_pixel} - #{last_range.to_location_in_pixel})"
    # raise "Cannot find current opacity range! #{err_msg}"
  end
end