Class: Gradient::PointMerger

Inherits:
Object
  • Object
show all
Defined in:
lib/gradient/point_merger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color_points = [], opacity_points = []) ⇒ PointMerger

Returns a new instance of PointMerger.



6
7
8
9
10
11
12
13
14
# File 'lib/gradient/point_merger.rb', line 6

def initialize(color_points=[], opacity_points=[])
  color_points << Gradient::ColorPoint.new(0, Color::RGB.new(255, 255, 255)) if color_points.empty?
  opacity_points << Gradient::OpacityPoint.new(0, 1) if opacity_points.empty?

  @color_points = sort_points(Array(color_points))
  @opacity_points = sort_points(Array(opacity_points))
  @all_points = sort_points(@color_points + @opacity_points)
  @locations = @all_points.map { |point| point.location }.uniq
end

Instance Attribute Details

#color_pointsObject

Returns the value of attribute color_points.



4
5
6
# File 'lib/gradient/point_merger.rb', line 4

def color_points
  @color_points
end

#opacity_pointsObject

Returns the value of attribute opacity_points.



4
5
6
# File 'lib/gradient/point_merger.rb', line 4

def opacity_points
  @opacity_points
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gradient/point_merger.rb', line 16

def call
  @locations.map do |location|
    selected_points = @all_points.select { |point| point.location == location }
    colored, opaque = selected_points.group_by(&:class).values_at(ColorPoint, OpacityPoint)
    rgba = if colored && opaque
      [colored.first.color, opaque.first.opacity]
    elsif colored
      point = colored.first
      a, b = adjacent_points(@opacity_points, point)
      fraction = location_fraction(a, b, point)
      [point.color, opacity_difference(fraction, a, b)]
    elsif opaque
      point = opaque.first
      a, b = adjacent_points(@color_points, point)
      fraction = location_fraction(a, b, point)
      [color_difference(fraction, a, b), point.opacity]
    end

    Gradient::Point.new(location, *rgba)
  end
end