-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathsass_number.go
188 lines (176 loc) · 3.55 KB
/
sass_number.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package libs
import (
"math"
"strconv"
)
func (s SassNumber) String() string {
return strconv.FormatFloat(s.Value, 'f', -1, 64) + s.Unit
}
var sassUnitConversions = map[string]map[string]float64{
"in": {
"in": 1,
"cm": 2.54,
"pc": 6,
"mm": 25.4,
"pt": 72,
"px": 96,
"deg": 1,
"grad": 1,
"rad": 1,
"turn": 1,
},
"cm": {
"in": 1.0 / 2.54,
"cm": 1,
"pc": 6.0 / 2.54,
"mm": 10,
"pt": 72.0 / 2.54,
"px": 96.0 / 2.54,
"deg": 1,
"grad": 1,
"rad": 1,
"turn": 1,
},
"pc": {
"in": 1.0 / 6.0,
"cm": 2.54 / 6.0,
"pc": 1,
"mm": 25.4 / 6.0,
"pt": 72.0 / 6.0,
"px": 96.0 / 6.0,
"deg": 1,
"grad": 1,
"rad": 1,
"turn": 1,
},
"mm": {
"in": 1.0 / 25.4,
"cm": 1.0 / 10.0,
"pc": 6.0 / 25.4,
"mm": 1,
"pt": 72.0 / 25.4,
"px": 96.0 / 25.4,
"deg": 1,
"grad": 1,
"rad": 1,
"turn": 1,
},
"pt": {
"in": 1.0 / 72.0,
"cm": 2.54 / 72.0,
"pc": 6.0 / 72.0,
"mm": 25.4 / 72.0,
"pt": 1,
"px": 96.0 / 72.0,
"deg": 1,
"grad": 1,
"rad": 1,
"turn": 1,
},
"px": {
"in": 1.0 / 96.0,
"cm": 2.54 / 96.0,
"pc": 6.0 / 96.0,
"mm": 25.4 / 96.0,
"pt": 72.0 / 96.0,
"px": 1,
"deg": 1,
"grad": 1,
"rad": 1,
"turn": 1,
},
"deg": {
"in": 1,
"cm": 1,
"pc": 1,
"mm": 1,
"pt": 1,
"px": 1,
"deg": 1,
"grad": 40.0 / 36.0,
"rad": math.Pi / 180.0,
"turn": 1.0 / 360.0,
},
"grad": {
"in": 1,
"cm": 1,
"pc": 1,
"mm": 1,
"pt": 1,
"px": 1,
"deg": 36.0 / 40.0,
"grad": 1,
"rad": math.Pi / 200.0,
"turn": 1.0 / 400.0,
},
"rad": {
"in": 1,
"cm": 1,
"pc": 1,
"mm": 1,
"pt": 1,
"px": 1,
"deg": 180.0 / math.Pi,
"grad": 200.0 / math.Pi,
"rad": 1,
"turn": math.Pi / 2.0,
},
"turn": {
"in": 1,
"cm": 1,
"pc": 1,
"mm": 1,
"pt": 1,
"px": 1,
"deg": 360.0,
"grad": 400.0,
"rad": 2.0 * math.Pi,
"turn": 1,
},
}
var SassUnitConversions map[string]map[string]float64 = sassUnitConversions
var sassUnitTypes = map[string]string{
"in": "distance",
"cm": "distance",
"pc": "distance",
"mm": "distance",
"pt": "distance",
"px": "distance",
"deg": "angle",
"grad": "angle",
"rad": "angle",
"turn": "angle",
}
// Add sums the two numbers in the first numbers units
func (sn SassNumber) Add(sn2 SassNumber) SassNumber {
sn1Value, sn2Value := getConvertedUnits(sn, sn2)
return SassNumber{Value: sn1Value + sn2Value, Unit: sn.Unit}
}
// Subtract minuses the two numbers in the first numbers units
func (sn SassNumber) Subtract(sn2 SassNumber) SassNumber {
sn1Value, sn2Value := getConvertedUnits(sn, sn2)
return SassNumber{Value: sn1Value - sn2Value, Unit: sn.Unit}
}
// Multiply takes the multiplication of the two numbers
// in the first numbers units
func (sn SassNumber) Multiply(sn2 SassNumber) SassNumber {
sn1Value, sn2Value := getConvertedUnits(sn, sn2)
return SassNumber{Value: sn1Value * sn2Value, Unit: sn.Unit}
}
// Divide takes the quotient of the two numbers in the first unit
func (sn SassNumber) Divide(sn2 SassNumber) SassNumber {
sn1Value, sn2Value := getConvertedUnits(sn, sn2)
return SassNumber{Value: sn1Value / sn2Value, Unit: sn.Unit}
}
func getConvertedUnits(sn1 SassNumber, sn2 SassNumber) (float64, float64) {
var sn2Value float64
if sn2.Unit != sn1.Unit {
sn2Value = convertUnits(sn2, sn1)
} else {
sn2Value = sn2.Value
}
return sn1.Value, sn2Value
}
func convertUnits(from SassNumber, to SassNumber) float64 {
return sassUnitConversions[from.Unit][to.Unit] * from.Value
}