summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/stylesheet/pattern_fill.rb
blob: 32d54d83caea2b0d08d9abf82c93c87aa9c768cb (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
# encoding: UTF-8
module Axlsx
  # A PatternFill is the pattern and solid fill styling for a cell.
  # @note The recommended way to manage styles is with Styles#add_style
  # @see Style#add_style
  class PatternFill

    # The color to use for the the background in solid fills.
    # @return [Color]
    attr_reader :fgColor

    # The color to use for the background of the fill when the type is not solid.
    # @return [Color]
    attr_reader :bgColor

    # The pattern type to use
    # @note
    #  patternType must be one of
    #   :none
    #   :solid
    #   :mediumGray
    #   :darkGray
    #   :lightGray
    #   :darkHorizontal
    #   :darkVertical
    #   :darkDown
    #   :darkUp
    #   :darkGrid
    #   :darkTrellis
    #   :lightHorizontal
    #   :lightVertical
    #   :lightDown
    #   :lightUp
    #   :lightGrid
    #   :lightTrellis
    #   :gray125
    #   :gray0625
    # @see Office Open XML Part 1 18.18.55
    attr_reader :patternType

    # Creates a new PatternFill Object
    # @option options [Symbol] patternType
    # @option options [Color] fgColor
    # @option options [Color] bgColor
    def initialize(options={})
      @patternType = :none
      options.each do |o|
        self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
      end
    end
    # @see fgColor
    def fgColor=(v) DataTypeValidator.validate "PatternFill.fgColor", Color, v; @fgColor=v end
    # @see bgColor
    def bgColor=(v) DataTypeValidator.validate "PatternFill.bgColor", Color, v; @bgColor=v end
    # @see patternType
    def patternType=(v) Axlsx::validate_pattern_type v; @patternType = v end

    def to_xml_string(str = '')
      str << '<patternFill patternType="' << patternType.to_s << '">'
      if fgColor.is_a?(Color)
        str << "<fgColor "
        fgColor.instance_values.each do |key, value|
          str << key.to_s << '="' << value.to_s << '" '
        end
        str << "/>"
      end

      if bgColor.is_a?(Color)
        str << "<bgColor "
        bgColor.instance_values.each do |key, value|
          str << key.to_s << '="' << value.to_s << '" '
        end
        str << "/>"
      end
      str << '</patternFill>'
    end

    # Serializes the pattern fill
    # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
    # @return [String]
    def to_xml(xml)
      xml.patternFill(:patternType => self.patternType) {
        self.instance_values.reject { |k,v| k.to_sym == :patternType }.each { |k,v| xml.send(k, v.instance_values) }
      }
    end
  end
end