forked from paulirish/css3please
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_math.js
212 lines (175 loc) · 5.12 KB
/
test_math.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
cssMath = {
/* ---- General ---- */
/* Isset */
isset: function (s) {
return s != undefined;
},
/* Trim */
trim: function (s) {
return s.replace(/^\s+|\s+$/g, '');
},
/* Number rounded by Length */
round: function (n, l) {
l = Math.pow(10, l) || 10;
return Math.round(n * l) / l;
},
/* ---- Rotation Adjustments ---- */
/* X and Y coordinates to rotation and strength */
xy2rs: function (x, y) {
return {
r: this.round(Math.atan2(x, y * -1) * 180 / Math.PI, 3),
s: this.round(Math.sqrt((x * x) + (y * y)), 3)
};
},
/* Rotation and Strength to x and y coordinates */
rs2xy: function (r, s) {
return {
x: this.round(Math.sin(r * Math.PI / 180) * s, 3),
y: this.round(Math.cos(r * Math.PI / 180) * s * -1, 3)
};
},
/* Rotation to degree */
r2d: function (r) {
return this.round(r * 90, 4);
},
/* Degree to rotation */
d2r: function (d) {
return this.round(d / 90, 4);
},
/* ---- Color Adjustments ---- */
/* Long Hexadecimals compressed to short hexadecimals */
lh2sh: function (lh) {
return '#' + this.trim(lh).replace(/^#?/, '').replace(/^([A-f0-9])\1([A-f0-9])\2([A-f0-9])\3([A-f0-9])\4$/, '$1$2$3$4').replace(/^([A-f0-9])\1([A-f0-9])\2([A-f0-9])\3$/, '$1$2$3');
},
/* Short Hexadecimals expanded to long hexadecimals */
sh2lh: function (sh) {
return '#' + this.trim(sh).replace(/^#?/, '').replace(/^([A-f0-9])([A-f0-9])([A-f0-9])([A-f0-9])$/, '$1$1$2$2$3$3$4$4').replace(/^([A-f0-9])([A-f0-9])([A-f0-9])$/, '$1$1$2$2$3$3');
},
/* Channel to hexadecimal */
c2h: function (c) {
return ('0' + parseInt(c, 10).toString(16)).substr(-2).toUpperCase();
},
/* Hexadecimal to channel */
h2c: function (h) {
return '' + parseInt(h, 16);
},
/* Channels to array */
c2a: function (c) {
return (c = /(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(c) || /(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(c)) && c.shift() ? c : false;
},
/* Hexadecimals to array */
h2a: function (h) {
h = this.sh2lh(h);
return (h = /([A-f0-9]{2})([A-f0-9]{2})([A-f0-9]{2})([A-f0-9]{2})/.exec(h) || /([A-f0-9]{2})([A-f0-9]{2})([A-f0-9]{2})/.exec(h)) && h.shift() ? h : false;
},
/* Channels to hexadecimals */
c2h2: function (c) {
c = this.c2a(c);
return this.hf((c[3] ? this.c2h(c[3]) : '') + this.c2h(c[0]) + this.c2h(c[1]) + this.c2h(c[2]));
},
/* Hexadecimals to channels */
h2c2: function (h) {
h = this.h2a(h);
return this.cf((h[3] ? [this.h2c(h[1]), this.h2c(h[2]), this.h2c(h[3]), this.h2c(h[0])] : [this.h2c(h[0]), this.h2c(h[1]), this.h2c(h[2])]).join(', '));
},
/* Channels proper formatting */
cf: function (c) {
c = this.c2a(c);
return (c[3] ? 'rgba(' : 'rgb(') + c.join(', ') + ')';
},
/* Hexadecimals proper formatting */
hf: function (h) {
return '#' + this.h2a(h).join('');
},
/* Channel Move */
cm: function (c, m) {
return Math.max(Math.min(parseInt(c, 10) + m, 255), 0) + '';
},
/* Hexadecimal Move */
hm: function (h, m) {
return this.c2h(Math.max(Math.min(parseInt(h, 16) + m, 255), 0));
},
/* Channels Move */
cm2: function (c, m) {
var e = -1;
c = this.c2a(c);
while (++e < c.length) {
c[e] = this.cm(c[e], m);
}
return this.cf(c.join(','));
},
/* Hexadecimals Move */
hm2: function (h, m) {
var e = -1;
h = this.h2a(h);
while (++e < h.length) {
h[e] = this.hm(h[e], m);
}
return this.hf(h.join(''));
},
/* ---- String Adjustments ---- */
/* String to short hexadecimals (move capable) */
s2sh: function (s, m) {
m = this.isset(m) ? m : 0;
return this.lh2sh(
this.hm2(
(s.indexOf(',') > -1 ? this.c2h2(s) : s),
m
)
);
},
/* String to long hexadecimals (move capable) */
s2lh: function (s, m) {
m = this.isset(m) ? m : 0;
return this.sh2lh(
this.hm2(
(s.indexOf(',') > -1 ? this.c2h2(s) : s),
m
)
);
},
/* String to channels (move capable) */
s2c: function (s, m) {
m = this.isset(m) ? m : 0;
return s.indexOf(',') > -1 ? this.cf(s) : this.h2c2(s);
},
/* String to degree */
s2d: function (s, m) {
m = this.isset(m) ? m : 0;
return s > 4 ? s : Math.max(this.round(this.r2d(s) + m, 3), 0);
},
/* String of channels, hexadecimals, or units to string (move capable) */
s2x: function (s, m, sh) {
s = this.trim(s);
m = this.isset(m) ? m : 0;
sh = this.isset(sh) ? sh : false;
/* Hex */
if (/^[#A-f0-9]+$/.test(s)) {
if (sh || /^[#A-f0-9]{3,5}$/.test(s)) {
return this.lh2sh(this.hm2(s, m));
}
return this.hm2(s, m);
}
/* RGB */
else if (s.indexOf(',') > -1) {
return this.cm2(s, m);
}
/* Unit */
else if (/([\+\-\d]+)(.*)/.test(s)) {
var unit,
units = [],
re = /([\+\-\d]+)(%|in|cm|mm|em|ex|pt|pc|px)*(\s|$)/g;
while ((unit = re.exec(s)) != null) {
units.push(Math.max(parseInt(unit[1], 10) + m, 0) + (unit[2] || 'px'), unit[3]);
}
return units.join('');
}
else {
return false;
}
},
/* String of channels, hexadecimals, or units to short string (move capable) */
s2sx: function (s, m) {
return this.s2x(s, m, true);
}
};