Class: Gradient::Map

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*points) ⇒ Map

Returns a new instance of Map.



12
13
14
15
16
# File 'lib/gradient/map.rb', line 12

def initialize(*points)
  # Sort points using a stable sort algorithm
  @points = points.flatten.sort_by.with_index { |x, idx| [x, idx] }
  @locations = @points.map { |point| point.location }
end

Instance Attribute Details

#locationsObject (readonly)

Returns the value of attribute locations.



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

def locations
  @locations
end

#pointsObject (readonly)

Returns the value of attribute points.



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

def points
  @points
end

Class Method Details

.deserialize(points = []) ⇒ Object



7
8
9
# File 'lib/gradient/map.rb', line 7

def deserialize(points=[])
  new(points.map { |point| Gradient::Point.deserialize(*point) })
end

Instance Method Details

#as_json(json = {}) ⇒ Object



51
52
53
# File 'lib/gradient/map.rb', line 51

def as_json(json={})
  serialize
end

#at(location, opts = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/gradient/map.rb', line 32

def at(location, opts = {})
  if range.include? location then
    if 0 == (i = points.find_index { |p| p.location >= location }) then
      points[0].dup
    else
      interpolate_points(points[i-1], points[i], location)
    end
  end
end

#inspectObject



18
19
20
# File 'lib/gradient/map.rb', line 18

def inspect
  "#<Gradient Map #{points.map(&:inspect).join(" ")}>"
end

#rangeObject



22
23
24
25
26
27
28
29
30
# File 'lib/gradient/map.rb', line 22

def range
  @range ||=
    begin
      ends = [:first, :last]
        .map { |method| points.send(method) }
        .map(&:location)
      Range.new(*ends)
    end
end

#serializeObject



47
48
49
# File 'lib/gradient/map.rb', line 47

def serialize
  @points.map(&:serialize)
end

#to_css(**args) ⇒ Object



42
43
44
45
# File 'lib/gradient/map.rb', line 42

def to_css(**args)
  @css_printer ||= Gradient::CSSPrinter.new(self)
  @css_printer.css(**args)
end