wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
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
import { isString } from "lodash-es";
 
// 获取当前颜色
export function getCurrentColor({
    color,
    max,
    value,
}: {
    color: string | string[];
    max: number;
    value: number;
}) {
    if (isString(color)) {
        return color;
    } else {
        const colorArray = color
            .map((item, index) => {
                if (isString(item)) {
                    return {
                        color: item,
                        value: (index + 1) * (max / color.length),
                    };
                }
                return item;
            })
            .sort((a, b) => a.value - b.value);
 
        for (let i = 0; i < colorArray.length; i++) {
            if (colorArray[i].value >= value) {
                return colorArray[i].color;
            }
        }
 
        return colorArray[colorArray.length - 1].color;
    }
}