Module: Writexlsx::Gradient

Included in:
Chart, Chart::Chartline, Chart::Series, ChartArea
Defined in:
lib/write_xlsx/gradient.rb

Instance Method Summary collapse

Instance Method Details

#gradient_properties(args) ⇒ Object



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
# File 'lib/write_xlsx/gradient.rb', line 5

def gradient_properties(args)
  return unless ptrue?(args)

  gradient = {}

  types    = {
    'linear'      => 'linear',
    'radial'      => 'circle',
    'rectangular' => 'rect',
    'path'        => 'shape'
  }

  # Check the colors array exists and is valid.
  raise "Gradient must include colors array" unless ptrue?(args[:colors])
  # Check the colors array has the right number of entries.
  raise "Gradient colors array must include at least 2 values" if args[:colors].size < 2

  gradient[:colors] = args[:colors]

  if ptrue?(args[:positions])
    # Check the positions array has the right number of entries.
    raise "Gradient positions not equal to numbers of colors" unless args[:positions].size == args[:colors].size

    # Check the positions are in the correct range.
    args[:positions].each do |pos|
      raise "Gradient position '#{pos} must be in range 0 <= pos <= 100" if pos < 0 || pos > 100
    end
    gradient[:positions] = args[:positions]
  else
    # Use the default gradient positions.
    case args[:colors].size
    when 2
      gradient[:positions] = [0, 100]
    when 3
      gradient[:positions] = [0, 50, 100]
    when 4
      gradient[:positions] = [0, 33, 66, 100]
    else
      raise "Must specify gradient positions"
    end
  end

  # Set the gradient angle.
  if args[:angle]
    angle = args[:angle]

    raise "Gradient angle '#{angle} must be in range 0 <= pos < 360" if angle < 0 || angle > 359.9

    gradient[:angle] = angle
  else
    gradient[:angle] = 90
  end

  # Set the gradient type.
  if args[:type]
    type = args[:type]

    raise "Unknow gradient type '#{type}'" unless types[type]

    gradient[:type] = types[type]
  else
    gradient[:type] = 'linear'
  end

  gradient
end