forked from signintech/gopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transparency.go
136 lines (114 loc) · 3.02 KB
/
transparency.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package gopdf
import (
"fmt"
"sync"
"github.com/pkg/errors"
)
type BlendModeType string
const (
Hue BlendModeType = "/Hue"
Color BlendModeType = "/Color"
NormalBlendMode BlendModeType = "/Normal"
Darken BlendModeType = "/Darken"
Screen BlendModeType = "/Screen"
Overlay BlendModeType = "/Overlay"
Lighten BlendModeType = "/Lighten"
Multiply BlendModeType = "/Multiply"
Exclusion BlendModeType = "/Exclusion"
ColorBurn BlendModeType = "/ColorBurn"
HardLight BlendModeType = "/HardLight"
SoftLight BlendModeType = "/SoftLight"
Difference BlendModeType = "/Difference"
Saturation BlendModeType = "/Saturation"
Luminosity BlendModeType = "/Luminosity"
ColorDodge BlendModeType = "/ColorDodge"
)
const DefaultAplhaValue = 1
// Transparency defines an object alpha.
type Transparency struct {
extGStateIndex int
Alpha float64
BlendModeType BlendModeType
}
func NewTransparency(alpha float64, blendModeType string) (Transparency, error) {
if alpha < 0.0 || alpha > 1.0 {
return Transparency{}, errors.Errorf("alpha value is out of range (0.0 - 1.0): %.3f", alpha)
}
bmtType, err := defineBlendModeType(blendModeType)
if err != nil {
return Transparency{}, err
}
return Transparency{
Alpha: alpha,
BlendModeType: bmtType,
}, nil
}
func (t Transparency) GetId() string {
keyStr := fmt.Sprintf("%.3f_%s", t.Alpha, t.BlendModeType)
return keyStr
}
type TransparencyMap struct {
syncer sync.Mutex
table map[string]Transparency
}
func NewTransparencyMap() TransparencyMap {
return TransparencyMap{
syncer: sync.Mutex{},
table: make(map[string]Transparency),
}
}
func (tm *TransparencyMap) Find(transparency Transparency) (Transparency, bool) {
key := transparency.GetId()
tm.syncer.Lock()
defer tm.syncer.Unlock()
t, ok := tm.table[key]
if !ok {
return Transparency{}, false
}
return t, ok
}
func (tm *TransparencyMap) Save(transparency Transparency) Transparency {
tm.syncer.Lock()
defer tm.syncer.Unlock()
key := transparency.GetId()
tm.table[key] = transparency
return transparency
}
func defineBlendModeType(bmType string) (BlendModeType, error) {
switch bmType {
case string(Hue):
return Hue, nil
case string(Color):
return Color, nil
case "", string(NormalBlendMode):
return NormalBlendMode, nil
case string(Darken):
return Darken, nil
case string(Screen):
return Screen, nil
case string(Overlay):
return Overlay, nil
case string(Lighten):
return Lighten, nil
case string(Multiply):
return Multiply, nil
case string(Exclusion):
return Exclusion, nil
case string(ColorBurn):
return ColorBurn, nil
case string(HardLight):
return HardLight, nil
case string(SoftLight):
return SoftLight, nil
case string(Difference):
return Difference, nil
case string(Saturation):
return Saturation, nil
case string(Luminosity):
return Luminosity, nil
case string(ColorDodge):
return ColorDodge, nil
default:
return "", errors.New("blend mode is unknown")
}
}