| 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
4×
4×
4×
4×
2×
2×
2×
2×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
4×
4×
1×
3×
3×
3×
1×
372×
194×
178×
178×
1×
1×
1×
1×
1×
1×
7×
7×
7×
7×
7×
7×
7×
1×
7×
7×
7×
7×
3×
3×
3×
3×
3×
3×
3×
1×
1×
1×
3×
3×
3×
3×
4×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
2×
1×
19×
1×
6×
6×
6×
6×
6×
6×
1×
180×
180×
1×
180×
180×
1×
180×
180×
1×
180×
180×
1×
1×
| // We can't use goog.color or goog.color.alpha because they interally use a hex
// string representation that encodes each channel in a single byte. This
// causes occasional loss of precision and rounding errors, especially in the
// alpha channel.
goog.provide('ol.Color');
goog.provide('ol.color');
goog.require('goog.asserts');
goog.require('goog.color');
goog.require('goog.color.names');
goog.require('goog.vec.Mat4');
goog.require('ol');
goog.require('ol.math');
/**
* A color represented as a short array [red, green, blue, alpha].
* red, green, and blue should be integers in the range 0..255 inclusive.
* alpha should be a float in the range 0..1 inclusive.
* @typedef {Array.<number>}
* @api
*/
ol.Color;
/**
* This RegExp matches # followed by 3 or 6 hex digits.
* @const
* @type {RegExp}
* @private
*/
ol.color.hexColorRe_ = /^#(?:[0-9a-f]{3}){1,2}$/i;
/**
* @see goog.color.rgbColorRe_
* @const
* @type {RegExp}
* @private
*/
ol.color.rgbColorRe_ =
/^(?:rgb)?\((0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2})\)$/i;
/**
* @see goog.color.alpha.rgbaColorRe_
* @const
* @type {RegExp}
* @private
*/
ol.color.rgbaColorRe_ =
/^(?:rgba)?\((0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|[1-9]\d{0,2}),\s?(0|1|0\.\d{0,10})\)$/i;
/**
* @param {ol.Color} dst Destination.
* @param {ol.Color} src Source.
* @param {ol.Color=} opt_color Color.
* @return {ol.Color} Color.
*/
ol.color.blend = function(dst, src, opt_color) {
// http://en.wikipedia.org/wiki/Alpha_compositing
// FIXME do we need to scale by 255?
var out = opt_color ? opt_color : [];
var dstA = dst[3];
var srcA = src[3];
if (dstA == 1) {
out[0] = (src[0] * srcA + dst[0] * (1 - srcA) + 0.5) | 0;
out[1] = (src[1] * srcA + dst[1] * (1 - srcA) + 0.5) | 0;
out[2] = (src[2] * srcA + dst[2] * (1 - srcA) + 0.5) | 0;
out[3] = 1;
} else if (srcA === 0) {
out[0] = dst[0];
out[1] = dst[1];
out[2] = dst[2];
out[3] = dstA;
} else {
var outA = srcA + dstA * (1 - srcA);
Iif (outA === 0) {
out[0] = 0;
out[1] = 0;
out[2] = 0;
out[3] = 0;
} else {
out[0] = ((src[0] * srcA + dst[0] * dstA * (1 - srcA)) / outA + 0.5) | 0;
out[1] = ((src[1] * srcA + dst[1] * dstA * (1 - srcA)) / outA + 0.5) | 0;
out[2] = ((src[2] * srcA + dst[2] * dstA * (1 - srcA)) / outA + 0.5) | 0;
out[3] = outA;
}
}
goog.asserts.assert(ol.color.isValid(out),
'Output color of blend should be a valid color');
return out;
};
/**
* Return the color as an array. This function maintains a cache of calculated
* arrays which means the result should not be modified.
* @param {ol.Color|string} color Color.
* @return {ol.Color} Color.
* @api
*/
ol.color.asArray = function(color) {
Iif (goog.isArray(color)) {
return color;
} else {
goog.asserts.assert(goog.isString(color), 'Color should be a string');
return ol.color.fromString(color);
}
};
/**
* Return the color as an rgba string.
* @param {ol.Color|string} color Color.
* @return {string} Rgba string.
* @api
*/
ol.color.asString = function(color) {
if (goog.isString(color)) {
return color;
} else {
goog.asserts.assert(goog.isArray(color), 'Color should be an array');
return ol.color.toString(color);
}
};
/**
* @param {ol.Color} color1 Color1.
* @param {ol.Color} color2 Color2.
* @return {boolean} Equals.
*/
ol.color.equals = function(color1, color2) {
return color1 === color2 || (
color1[0] == color2[0] && color1[1] == color2[1] &&
color1[2] == color2[2] && color1[3] == color2[3]);
};
/**
* @param {string} s String.
* @return {ol.Color} Color.
*/
ol.color.fromString = (
/**
* @return {function(string): ol.Color}
*/
function() {
// We maintain a small cache of parsed strings. To provide cheap LRU-like
// semantics, whenever the cache grows too large we simply delete an
// arbitrary 25% of the entries.
/**
* @const
* @type {number}
*/
var MAX_CACHE_SIZE = 1024;
/**
* @type {Object.<string, ol.Color>}
*/
var cache = {};
/**
* @type {number}
*/
var cacheSize = 0;
return (
/**
* @param {string} s String.
* @return {ol.Color} Color.
*/
function(s) {
var color;
Iif (cache.hasOwnProperty(s)) {
color = cache[s];
} else {
Iif (cacheSize >= MAX_CACHE_SIZE) {
var i = 0;
var key;
for (key in cache) {
if ((i++ & 3) === 0) {
delete cache[key];
--cacheSize;
}
}
}
color = ol.color.fromStringInternal_(s);
cache[s] = color;
++cacheSize;
}
return color;
});
})();
/**
* @param {string} s String.
* @private
* @return {ol.Color} Color.
*/
ol.color.fromStringInternal_ = function(s) {
var isHex = false;
Iif (ol.ENABLE_NAMED_COLORS && goog.color.names.hasOwnProperty(s)) {
// goog.color.names does not have a type declaration, so add a typecast
s = /** @type {string} */ (goog.color.names[s]);
isHex = true;
}
var r, g, b, a, color, match;
if (isHex || (match = ol.color.hexColorRe_.exec(s))) { // hex
var n = s.length - 1; // number of hex digits
goog.asserts.assert(n == 3 || n == 6,
'Color string length should be 3 or 6');
var d = n == 3 ? 1 : 2; // number of digits per channel
r = parseInt(s.substr(1 + 0 * d, d), 16);
g = parseInt(s.substr(1 + 1 * d, d), 16);
b = parseInt(s.substr(1 + 2 * d, d), 16);
if (d == 1) {
r = (r << 4) + r;
g = (g << 4) + g;
b = (b << 4) + b;
}
a = 1;
color = [r, g, b, a];
goog.asserts.assert(ol.color.isValid(color),
'Color should be a valid color');
return color;
} else if ((match = ol.color.rgbaColorRe_.exec(s))) { // rgba()
r = Number(match[1]);
g = Number(match[2]);
b = Number(match[3]);
a = Number(match[4]);
color = [r, g, b, a];
return ol.color.normalize(color, color);
} else Eif ((match = ol.color.rgbColorRe_.exec(s))) { // rgb()
r = Number(match[1]);
g = Number(match[2]);
b = Number(match[3]);
color = [r, g, b, 1];
return ol.color.normalize(color, color);
} else {
goog.asserts.fail(s + ' is not a valid color');
}
};
/**
* @param {ol.Color} color Color.
* @return {boolean} Is valid.
*/
ol.color.isValid = function(color) {
return 0 <= color[0] && color[0] < 256 &&
0 <= color[1] && color[1] < 256 &&
0 <= color[2] && color[2] < 256 &&
0 <= color[3] && color[3] <= 1;
};
/**
* @param {ol.Color} color Color.
* @param {ol.Color=} opt_color Color.
* @return {ol.Color} Clamped color.
*/
ol.color.normalize = function(color, opt_color) {
var result = opt_color || [];
result[0] = ol.math.clamp((color[0] + 0.5) | 0, 0, 255);
result[1] = ol.math.clamp((color[1] + 0.5) | 0, 0, 255);
result[2] = ol.math.clamp((color[2] + 0.5) | 0, 0, 255);
result[3] = ol.math.clamp(color[3], 0, 1);
return result;
};
/**
* @param {ol.Color} color Color.
* @return {string} String.
*/
ol.color.toString = function(color) {
var r = color[0];
if (r != (r | 0)) {
r = (r + 0.5) | 0;
}
var g = color[1];
if (g != (g | 0)) {
g = (g + 0.5) | 0;
}
var b = color[2];
if (b != (b | 0)) {
b = (b + 0.5) | 0;
}
var a = color[3];
return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
};
/**
* @param {!ol.Color} color Color.
* @param {goog.vec.Mat4.Number} transform Transform.
* @param {!ol.Color=} opt_color Color.
* @return {ol.Color} Transformed color.
*/
ol.color.transform = function(color, transform, opt_color) {
var result = opt_color ? opt_color : [];
result = goog.vec.Mat4.multVec3(transform, color, result);
goog.asserts.assert(goog.isArray(result), 'result should be an array');
result[3] = color[3];
return ol.color.normalize(result, result);
};
/**
* @param {ol.Color|string} color1 Color2.
* @param {ol.Color|string} color2 Color2.
* @return {boolean} Equals.
*/
ol.color.stringOrColorEquals = function(color1, color2) {
if (color1 === color2 || color1 == color2) {
return true;
}
if (goog.isString(color1)) {
color1 = ol.color.fromString(color1);
}
if (goog.isString(color2)) {
color2 = ol.color.fromString(color2);
}
return ol.color.equals(color1, color2);
};
|