|
| 1 | +import type { ColorMappings, NativeModule, TreemapLayout } from 'squarified' |
| 2 | +import { hashCode } from '../../shared' |
| 3 | + |
| 4 | +export function presetDecorator(app: TreemapLayout) { |
| 5 | + Object.assign(app.decorator, { |
| 6 | + layout: { |
| 7 | + titleAreaHeight: { |
| 8 | + max: 60, |
| 9 | + min: 30 |
| 10 | + }, |
| 11 | + rectGap: 5 |
| 12 | + }, |
| 13 | + font: { |
| 14 | + color: '#000', |
| 15 | + fontSize: { |
| 16 | + max: 70, |
| 17 | + min: 0 |
| 18 | + }, |
| 19 | + fontFamily: 'sans-serif' |
| 20 | + }, |
| 21 | + color: { mappings: evaluateColorMappings(app.data) } |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +function evaluateColorMappings(data: NativeModule[]): ColorMappings { |
| 26 | + const colorMappings: ColorMappings = {} |
| 27 | + |
| 28 | + const hashToHue = (id: string): number => { |
| 29 | + const hash = Math.abs(hashCode(id)) |
| 30 | + return hash % 360 |
| 31 | + } |
| 32 | + |
| 33 | + const lightScale = (depth: number) => 70 - depth * 5 |
| 34 | + const baseSaturation = 40 |
| 35 | + const siblingHueShift = 20 |
| 36 | + |
| 37 | + const rc = 0.2126 |
| 38 | + const gc = 0.7152 |
| 39 | + const bc = 0.0722 |
| 40 | + |
| 41 | + const hslToRgb = (h: number, s: number, l: number): { r: number, g: number, b: number } => { |
| 42 | + const a = s * Math.min(l, 1 - l) |
| 43 | + const f = (n: number) => { |
| 44 | + const k = (n + h / 30) % 12 |
| 45 | + return l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1) |
| 46 | + } |
| 47 | + return { r: f(0), g: f(8), b: f(4) } |
| 48 | + } |
| 49 | + |
| 50 | + const calculateLuminance = (r: number, g: number, b: number): number => { |
| 51 | + return rc * r + gc * g + bc * b |
| 52 | + } |
| 53 | + |
| 54 | + const calculateColor = ( |
| 55 | + module: NativeModule, |
| 56 | + depth: number, |
| 57 | + parentHue: number | null, |
| 58 | + siblingIndex: number, |
| 59 | + totalSiblings: number |
| 60 | + ) => { |
| 61 | + const nodeHue = hashToHue(module.id) |
| 62 | + const hue = parentHue !== null |
| 63 | + ? (parentHue + siblingHueShift * siblingIndex / totalSiblings) % 360 |
| 64 | + : nodeHue |
| 65 | + const lightness = lightScale(depth) |
| 66 | + |
| 67 | + const hslColor = { |
| 68 | + h: hue, |
| 69 | + s: baseSaturation, |
| 70 | + l: lightness / 100 |
| 71 | + } |
| 72 | + const { r, g, b } = hslToRgb(hslColor.h, hslColor.s / 100, hslColor.l) |
| 73 | + const luminance = calculateLuminance(r, g, b) |
| 74 | + |
| 75 | + if (luminance < 0.6) { |
| 76 | + hslColor.l += 0.2 |
| 77 | + } else if (luminance > 0.8) { |
| 78 | + hslColor.l -= 0.1 |
| 79 | + } |
| 80 | + |
| 81 | + hslColor.l *= 100 |
| 82 | + |
| 83 | + colorMappings[module.id] = { |
| 84 | + mode: 'hsl', |
| 85 | + desc: hslColor |
| 86 | + } |
| 87 | + |
| 88 | + if (module.groups && Array.isArray(module.groups)) { |
| 89 | + const totalChildren = module.groups.length |
| 90 | + for (let i = 0; i < totalChildren; i++) { |
| 91 | + const child = module.groups[i] |
| 92 | + calculateColor(child, depth + 1, hue, i, totalChildren) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + for (let i = 0; i < data.length; i++) { |
| 98 | + const module = data[i] |
| 99 | + calculateColor(module, 0, null, i, data.length) |
| 100 | + } |
| 101 | + |
| 102 | + return colorMappings |
| 103 | +} |
0 commit comments