1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.rgb2arr = rgb2arr;
|
9 | exports.decodePickingColor = decodePickingColor;
|
10 | exports.encodePickingColor = encodePickingColor;
|
11 | exports.generateColorRamp = generateColorRamp;
|
12 |
|
13 | var d3 = _interopRequireWildcard(require("d3-color"));
|
14 |
|
15 | function rgb2arr(str) {
|
16 | var color = d3.color(str);
|
17 | var arr = [0, 0, 0, 0];
|
18 |
|
19 | if (color != null) {
|
20 | arr[0] = color.r / 255;
|
21 | arr[1] = color.g / 255;
|
22 | arr[2] = color.b / 255;
|
23 | arr[3] = color.opacity;
|
24 | }
|
25 |
|
26 | return arr;
|
27 | }
|
28 |
|
29 | function decodePickingColor(color) {
|
30 | var i1 = color && color[0];
|
31 | var i2 = color && color[1];
|
32 | var i3 = color && color[2];
|
33 | var index = i1 + i2 * 256 + i3 * 65536 - 1;
|
34 | return index;
|
35 | }
|
36 |
|
37 | function encodePickingColor(featureIdx) {
|
38 | return [featureIdx + 1 & 255, featureIdx + 1 >> 8 & 255, featureIdx + 1 >> 8 >> 8 & 255];
|
39 | }
|
40 |
|
41 | function generateColorRamp(colorRamp) {
|
42 | var canvas = document.createElement('canvas');
|
43 | var ctx = canvas.getContext('2d');
|
44 | canvas.width = 256;
|
45 | canvas.height = 1;
|
46 | var gradient = ctx.createLinearGradient(0, 0, 256, 1);
|
47 | var data = null;
|
48 | var min = colorRamp.positions[0];
|
49 | var max = colorRamp.positions[colorRamp.positions.length - 1];
|
50 |
|
51 | for (var i = 0; i < colorRamp.colors.length; ++i) {
|
52 | var value = (colorRamp.positions[i] - min) / (max - min);
|
53 | gradient.addColorStop(value, colorRamp.colors[i]);
|
54 | }
|
55 |
|
56 | ctx.fillStyle = gradient;
|
57 | ctx.fillRect(0, 0, 256, 1);
|
58 | data = new Uint8ClampedArray(ctx.getImageData(0, 0, 256, 1).data);
|
59 | return new ImageData(data, 256, 1);
|
60 | }
|
61 |
|
\ | No newline at end of file |