北京经济技术开发区经开区虚拟城市项目-【前端】-Web
Jin Lei
2023-12-23 5df6240f6cd274e698d1e6358443364d543c126c
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
 
 
var RD_SALT_COLOR = [
    [30, [10, 25, 68]],
    [32, [10, 25, 250]],
    [33.5, [255, 233, 102]],
    [34.5, [255, 155, 15]],
    [38, [255, 0, 0]],
];
function colorInterpolator(start, end) {
    var r = start[0],
        g = start[1],
        b = start[2];
    var Δr = end[0] - r,
        Δg = end[1] - g,
        Δb = end[2] - b;
    return function(i, a) {
        return [
            Math.floor(r + i * Δr),
            Math.floor(g + i * Δg),
            Math.floor(b + i * Δb),
            a,
        ];
    };
}
/**
 * Creates a color scale composed of the specified segments. Segments is an array of two-element arrays of the
 * form [value, color], where value is the point along the scale and color is the [r, g, b] color at that point.
 * For example, the following creates a scale that smoothly transitions from red to green to blue along the
 * points 0.5, 1.0, and 3.5:
 *
 *     [ [ 0.5, [255, 0, 0] ],
 *       [ 1.0, [0, 255, 0] ],
 *       [ 3.5, [0, 0, 255] ] ]
 *
 * @param segments array of color segments
 * @returns {Function} a function(point, alpha) that returns the color [r, g, b, alpha] for the given point.
 */
function segmentedColorScale(segments) {
    var points = [],
        interpolators = [],
        ranges = [];
    for (var i = 0; i < segments.length - 1; i++) {
        points.push(segments[i + 1][0]);
        interpolators.push(
            colorInterpolator(segments[i][1], segments[i + 1][1])
        );
        ranges.push([segments[i][0], segments[i + 1][0]]);
    }
 
    return function(point, alpha) {
        var i;
        for (i = 0; i < points.length - 1; i++) {
            if (point <= points[i]) {
                break;
            }
        }
        var range = ranges[i];
        return interpolators[i](proportion(point, range[0], range[1]), alpha);
    };
}
/**
 * @returns {Number} the value x clamped to the range [low, high].
 */
function clamp(x, low, high) {
    return Math.max(low, Math.min(x, high));
}
 
/**
 * @returns {number} the fraction of the bounds [low, high] covered by the value x, after clamping x to the
 *          bounds. For example, given bounds=[10, 20], this method returns 1 for x>=20, 0.5 for x=15 and 0
 *          for x<=10.
 */
function proportion(x, low, high) {
    return (clamp(x, low, high) - low) / (high - low);
}
var RD_OVERLAY_ALPHA = Math.floor(0.8 * 255); // 默认覆盖透明度 ([0, 255])
const Color = function (option){
    let colorBar = RD_SALT_COLOR;
    let valMax = option.max;
    let valMin = option.min;
    let delta = valMax - valMin;
    delta = delta / (colorBar.length - 1);
    for (let i = 0; i < colorBar.length; i++) {
        colorBar[i][0] = valMin + i * delta;
    }
    this._colorgradient = segmentedColorScale(colorBar);
 
};
Color.prototype.getColor = function (value){
    let color = this._colorgradient(value);
    let nColor;
    nColor = Cesium.Color.fromBytes(color[0], color[1], color[2], 255, nColor);
    return nColor;
}