From fee6f45ea9080e3479799697121ee385eac218f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Fri, 7 Aug 2020 21:10:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96=E9=9A=8F?= =?UTF-8?q?=E6=9C=BA=E9=A2=9C=E8=89=B2=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/utils/color.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/app/utils/color.ts b/src/app/utils/color.ts index 944ab63..7f9773c 100644 --- a/src/app/utils/color.ts +++ b/src/app/utils/color.ts @@ -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(); + 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; +}