UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

1 lines • 8.71 kB
{"version":3,"file":"ng-zorro-antd-core-color.mjs","names":[],"sources":["../../components/core/color/color.ts","../../components/core/color/generate.ts"],"sourcesContent":["/**\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE\n */\n\nimport { NzSafeAny } from 'ng-zorro-antd/core/types';\n\nexport const statusColors = ['success', 'processing', 'error', 'default', 'warning'] as const;\n\nexport const presetColors = [\n 'pink',\n 'red',\n 'yellow',\n 'orange',\n 'cyan',\n 'green',\n 'blue',\n 'purple',\n 'geekblue',\n 'magenta',\n 'volcano',\n 'gold',\n 'lime'\n] as const;\n\nexport type NzPresetColor = (typeof presetColors)[number];\nexport type NzStatusColor = (typeof statusColors)[number];\n\nexport function isPresetColor(color: string): color is NzPresetColor {\n return presetColors.indexOf(color as NzSafeAny) !== -1;\n}\n\nexport function isStatusColor(color: string): color is NzPresetColor {\n return statusColors.indexOf(color as NzSafeAny) !== -1;\n}\n","/**\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE\n */\n\n/**\n * Sync from @ant-design/colors(https://github.com/ant-design/ant-design-colors)\n */\n\nimport { rgbToHsv, rgbToHex, inputToRGB } from '@ctrl/tinycolor';\n\nconst hueStep = 2; // 色相阶梯\nconst saturationStep = 0.16; // 饱和度阶梯,浅色部分\nconst saturationStep2 = 0.05; // 饱和度阶梯,深色部分\nconst brightnessStep1 = 0.05; // 亮度阶梯,浅色部分\nconst brightnessStep2 = 0.15; // 亮度阶梯,深色部分\nconst lightColorCount = 5; // 浅色数量,主色上\nconst darkColorCount = 4; // 深色数量,主色下\n// 暗色主题颜色映射关系表\nconst darkColorMap = [\n { index: 7, opacity: 0.15 },\n { index: 6, opacity: 0.25 },\n { index: 5, opacity: 0.3 },\n { index: 5, opacity: 0.45 },\n { index: 5, opacity: 0.65 },\n { index: 5, opacity: 0.85 },\n { index: 4, opacity: 0.9 },\n { index: 3, opacity: 0.95 },\n { index: 2, opacity: 0.97 },\n { index: 1, opacity: 0.98 }\n];\n\ninterface HsvObject {\n h: number;\n s: number;\n v: number;\n}\n\ninterface RgbObject {\n r: number;\n g: number;\n b: number;\n}\n\n// Wrapper function ported from TinyColor.prototype.toHsv\n// Keep it here because of `hsv.h * 360`\nfunction toHsv({ r, g, b }: RgbObject): HsvObject {\n const hsv = rgbToHsv(r, g, b);\n return { h: hsv.h * 360, s: hsv.s, v: hsv.v };\n}\n\n// Wrapper function ported from TinyColor.prototype.toHexString\n// Keep it here because of the prefix `#`\nfunction toHex({ r, g, b }: RgbObject): string {\n return `#${rgbToHex(r, g, b, false)}`;\n}\n\n// Wrapper function ported from TinyColor.prototype.mix, not treeshakable.\n// Amount in range [0, 1]\n// Assume color1 & color2 has no alpha, since the following src code did so.\nfunction mix(rgb1: RgbObject, rgb2: RgbObject, amount: number): RgbObject {\n const p = amount / 100;\n const rgb = {\n r: (rgb2.r - rgb1.r) * p + rgb1.r,\n g: (rgb2.g - rgb1.g) * p + rgb1.g,\n b: (rgb2.b - rgb1.b) * p + rgb1.b\n };\n return rgb;\n}\n\nfunction getHue(hsv: HsvObject, i: number, light?: boolean): number {\n let hue: number;\n // 根据色相不同,色相转向不同\n if (Math.round(hsv.h) >= 60 && Math.round(hsv.h) <= 240) {\n hue = light ? Math.round(hsv.h) - hueStep * i : Math.round(hsv.h) + hueStep * i;\n } else {\n hue = light ? Math.round(hsv.h) + hueStep * i : Math.round(hsv.h) - hueStep * i;\n }\n if (hue < 0) {\n hue += 360;\n } else if (hue >= 360) {\n hue -= 360;\n }\n return hue;\n}\n\nfunction getSaturation(hsv: HsvObject, i: number, light?: boolean): number {\n // grey color don't change saturation\n if (hsv.h === 0 && hsv.s === 0) {\n return hsv.s;\n }\n let saturation: number;\n if (light) {\n saturation = hsv.s - saturationStep * i;\n } else if (i === darkColorCount) {\n saturation = hsv.s + saturationStep;\n } else {\n saturation = hsv.s + saturationStep2 * i;\n }\n // 边界值修正\n if (saturation > 1) {\n saturation = 1;\n }\n // 第一格的 s 限制在 0.06-0.1 之间\n if (light && i === lightColorCount && saturation > 0.1) {\n saturation = 0.1;\n }\n if (saturation < 0.06) {\n saturation = 0.06;\n }\n return Number(saturation.toFixed(2));\n}\n\nfunction getValue(hsv: HsvObject, i: number, light?: boolean): number {\n let value: number;\n if (light) {\n value = hsv.v + brightnessStep1 * i;\n } else {\n value = hsv.v - brightnessStep2 * i;\n }\n if (value > 1) {\n value = 1;\n }\n return Number(value.toFixed(2));\n}\n\ninterface Opts {\n theme?: 'dark' | 'default';\n backgroundColor?: string;\n}\n\nexport function generate(color: string, opts: Opts = {}): string[] {\n const patterns: string[] = [];\n const pColor = inputToRGB(color);\n for (let i = lightColorCount; i > 0; i -= 1) {\n const hsv = toHsv(pColor);\n const colorString: string = toHex(\n inputToRGB({\n h: getHue(hsv, i, true),\n s: getSaturation(hsv, i, true),\n v: getValue(hsv, i, true)\n })\n );\n patterns.push(colorString);\n }\n patterns.push(toHex(pColor));\n for (let i = 1; i <= darkColorCount; i += 1) {\n const hsv = toHsv(pColor);\n const colorString: string = toHex(\n inputToRGB({\n h: getHue(hsv, i),\n s: getSaturation(hsv, i),\n v: getValue(hsv, i)\n })\n );\n patterns.push(colorString);\n }\n\n // dark theme patterns\n if (opts.theme === 'dark') {\n return darkColorMap.map(({ index, opacity }) => {\n const darkColorString: string = toHex(\n mix(inputToRGB(opts.backgroundColor || '#141414'), inputToRGB(patterns[index]), opacity * 100)\n );\n return darkColorString;\n });\n }\n return patterns;\n}\n"],"mappings":";AAOA,MAAa,eAAe;CAAC;CAAW;CAAc;CAAS;CAAW;AAAS;AAEnF,MAAa,eAAe;CAC1B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACQ;AAKV,SAAgB,cAAc,OAAa;CACzC,OAAO,aAAa,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