-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_scale3.go
108 lines (94 loc) · 2.42 KB
/
color_scale3.go
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
104
105
106
107
108
// Copyright (c) 2017 Andrey Gayvoronsky <plandem@gmail.com>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package rule
import (
"fmt"
"github.com/roboninc/xlsx/internal/color"
"github.com/roboninc/xlsx/internal/ml"
"github.com/roboninc/xlsx/internal/ml/primitives"
)
type colorScale3Rule struct {
baseRule
}
// ColorScale3 is helper object to set specific options for rule
var ColorScale3 colorScale3Rule
func (x colorScale3Rule) initIfRequired(r *Info) {
if !r.initialized {
r.initialized = true
r.validator = ColorScale3
r.rule = &ml.ConditionalRule{
Type: primitives.ConditionTypeColorScale,
ColorScale: &ml.ColorScale{
Values: []*ml.ConditionValue{
{
Type: ValueTypeLowest,
},
{
Type: ValueTypePercentile,
Value: "50",
},
{
Type: ValueTypeHighest,
},
},
Colors: []*ml.Color{
color.New("#F8696B"),
color.New("#FFEB84"),
color.New("#63BE7B"),
},
},
}
}
}
func (x colorScale3Rule) Default(r *Info) {
x.initIfRequired(r)
}
func (x colorScale3Rule) setValue(r *Info, idx int, value string, settings []interface{}) {
x.initIfRequired(r)
r.rule.ColorScale.Values[idx].Value = value
for _, p := range settings {
switch pv := p.(type) {
case string:
r.rule.ColorScale.Colors[idx] = color.New(pv)
case primitives.ConditionValueType:
r.rule.ColorScale.Values[idx].Type = pv
}
}
}
func (x colorScale3Rule) Min(min string, settings ...interface{}) Option {
return func(r *Info) {
x.setValue(r, 0, min, settings)
}
}
func (x colorScale3Rule) Mid(mid string, settings ...interface{}) Option {
return func(r *Info) {
x.setValue(r, 1, mid, settings)
}
}
func (x colorScale3Rule) Max(max string, settings ...interface{}) Option {
return func(r *Info) {
x.setValue(r, 2, max, settings)
}
}
func (x colorScale3Rule) Validate(r *Info) error {
if !r.rule.ColorScale.Values[0].Type.IsAllowed(
ValueTypeLowest,
ValueTypeNumber,
ValueTypePercent,
ValueTypeFormula,
ValueTypePercentile,
) {
return fmt.Errorf("colorScale3: Not allowed type '%s' for min value", r.rule.ColorScale.Values[0].Type)
}
if !r.rule.ColorScale.Values[2].Type.IsAllowed(
ValueTypeNumber,
ValueTypePercent,
ValueTypeFormula,
ValueTypePercentile,
ValueTypeHighest,
) {
return fmt.Errorf("colorScale3: Not allowed type '%s' for max value", r.rule.ColorScale.Values[2].Type)
}
return nil
}