新增获取随机颜色的方法

This commit is contained in:
禾几海
2020-08-07 21:10:10 +08:00
parent a81f41e15c
commit fee6f45ea9

View File

@@ -1,4 +1,9 @@
export const ColorList: { bgColor: string, fontColor: string }[] = [
export class Color {
bgColor: string;
fontColor: string
}
export const ColorList: Color[] = [
{bgColor: '#7bcfa6', fontColor: '#000000'}, // 石青
{bgColor: '#bce672', fontColor: '#000000'}, // 松花色
{bgColor: '#ff8936', fontColor: '#000000'}, // 橘黄
@@ -7,3 +12,27 @@ export const ColorList: { bgColor: string, fontColor: string }[] = [
{bgColor: '#3eede7', fontColor: '#000000'}, // 碧蓝
{bgColor: '#177cb0', fontColor: '#ffffff'}, // 靛青
];
export const ColorListLength = ColorList.length
/**
* 获取一组随机颜色
* @param count 数量
*/
export function RandomColor(count: number = 1): Color[] {
const map = new Map<number, number>();
ColorList.forEach((color, index) => map.set(index, 0))
const colorArray: Color[] = [];
const oneRandomColor = () => {
const minValue = Math.min.apply(null, Array.from(map.values()))
const keys = Array.from(map.keys()).filter(key => map.get(key) === minValue);
const keyIndex = Math.floor(Math.random() * keys.length);
const index = keys[keyIndex];
map.set(index, minValue + 1);
return ColorList[index]
};
for (let i = 0; i < count; i++) {
colorArray.push(oneRandomColor());
}
return colorArray;
}