summaryrefslogtreecommitdiffhomepage
path: root/lib/axlsx/stylesheet/color.rb
blob: cfe5de9a77774105babbf34df1b9864a75fbfad0 (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
# frozen_string_literal: true

module Axlsx
  # The color class represents a color used for borders, fills an fonts
  class Color
    include Axlsx::OptionsParser
    include Axlsx::SerializedAttributes

    # Creates a new Color object
    # @option options [Boolean] auto
    # @option options [String] rgb
    # @option options [Float] tint
    def initialize(options = {})
      @rgb = "FF000000"
      parse_options options
    end

    serializable_attributes :auto, :rgb, :tint

    # Determines if the color is system color dependant
    # @return [Boolean]
    attr_reader :auto

    # The color as defined in rgb terms.
    # @note
    #  rgb colors need to conform to ST_UnsignedIntHex. That basically means put 'FF' before you color
    # When assigning the rgb value the behavior is much like CSS selectors and can use shorthand versions as follows:
    # If you provide a two character value it will be repeated for each r, g, b assignment
    # If you provide data that is not 2 characters in length, and is less than 8 characters it will be padded with "F"
    # @example
    #        Color.new :rgb => "FF000000"
    #          => #<Axlsx::Color:0x102106b68 @rgb="FF000000">
    #        Color.new :rgb => "0A"
    #          => #<Axlsx::Color:0x102106b68 @rgb="FF0A0A0A">
    #        Color.new :rgb => "00BB"
    #          => #<Axlsx::Color:0x102106b68 @rgb="FFFF00BB">
    # @return [String]
    attr_reader :rgb

    # no support for theme just yet
    # @return [Integer]
    # attr_reader :theme

    # The tint value.
    # @note valid values are between -1.0 and 1.0
    # @return [Float]
    attr_reader :tint

    # @see auto
    def auto=(v) Axlsx.validate_boolean v; @auto = v end

    # @see color
    def rgb=(v)
      Axlsx.validate_string(v)
      v = v.upcase
      v *= 3 if v.size == 2
      v = v.rjust(8, 'FF')
      raise ArgumentError, "Invalid color rgb value: #{v}." unless /[0-9A-F]{8}/.match?(v)

      @rgb = v
    end

    # @see tint
    def tint=(v) Axlsx.validate_float v; @tint = v end

    # This version does not support themes
    # def theme=(v) Axlsx::validate_unsigned_integer v; @theme = v end

    # Indexed colors are for backward compatability which I am choosing not to support
    # def indexed=(v) Axlsx::validate_unsigned_integer v; @indexed = v end

    # Serializes the object
    # @param [String] str
    # @return [String]
    def to_xml_string(str = +'', tag_name = 'color')
      serialized_tag(tag_name.to_s, str)
    end
  end
end