summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/stylesheet/gradient_fill.rb
blob: 6cd0a0520e77fe291c0866bdc9b463467eb1e161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# frozen_string_literal: true

module Axlsx
  # A GradientFill defines the color and positioning for gradiant cell fill.
  # @see Open Office XML Part 1 §18.8.24
  class GradientFill
    include Axlsx::OptionsParser
    include Axlsx::SerializedAttributes

    # Creates a new GradientFill object
    # @option options [Symbol] type
    # @option options [Float] degree
    # @option options [Float] left
    # @option options [Float] right
    # @option options [Float] top
    # @option options [Float] bottom
    def initialize(options = {})
      options[:type] ||= :linear
      parse_options options
      @stop = SimpleTypedList.new GradientStop
    end

    serializable_attributes :type, :degree, :left, :right, :top, :bottom

    # The type of gradient.
    # @note
    #  valid options are
    #   :linear
    #   :path
    # @return [Symbol]
    attr_reader :type

    # Angle of the linear gradient
    # @return [Float]
    attr_reader :degree

    # Percentage format left
    # @return [Float]
    attr_reader :left

    # Percentage format right
    # @return [Float]
    attr_reader :right

    # Percentage format top
    # @return [Float]
    attr_reader :top

    # Percentage format bottom
    # @return [Float]
    attr_reader :bottom

    # Collection of stop objects
    # @return [SimpleTypedList]
    attr_reader :stop

    # @see type
    def type=(v) Axlsx::validate_gradient_type v; @type = v end

    # @see degree
    def degree=(v) Axlsx::validate_float v; @degree = v end

    # @see left
    def left=(v)
      validate_format_percentage "GradientFill.left", v
      @left = v
    end

    # @see right
    def right=(v)
      validate_format_percentage "GradientFill.right", v
      @right = v
    end

    # @see top
    def top=(v)
      validate_format_percentage "GradientFill.top", v
      @top = v
    end

    # @see bottom
    def bottom=(v)
      validate_format_percentage "GradientFill.bottom", v
      @bottom = v
    end

    # validates that the value provided is between 0.0 and 1.0
    def validate_format_percentage(name, value)
      DataTypeValidator.validate name, Float, value, lambda { |arg| arg >= 0.0 && arg <= 1.0 }
    end

    # Serializes the object
    # @param [String] str
    # @return [String]
    def to_xml_string(str = +'')
      str << '<gradientFill '
      serialized_attributes str
      str << '>'
      @stop.each { |s| s.to_xml_string(str) }
      str << '</gradientFill>'
    end
  end
end