|
| 1 | +/** |
| 2 | + * @description 频谱图调色板的生成器 |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * @description 渐变色标 |
| 7 | + * |
| 8 | + * @param pos - 位置,从 0.0 到 1.0 |
| 9 | + * @param color - HEX 颜色字符串 |
| 10 | + */ |
| 11 | +export type ColorStop = { |
| 12 | + id: string |
| 13 | + pos: number |
| 14 | + color: string |
| 15 | +} |
| 16 | + |
| 17 | +function hslToRgb(h: number, s: number, l: number): [number, number, number] { |
| 18 | + if (s === 0.0) { |
| 19 | + const gray = (l * 255) | 0 |
| 20 | + return [gray, gray, gray] |
| 21 | + } |
| 22 | + |
| 23 | + const chroma = (1.0 - Math.abs(2.0 * l - 1.0)) * s |
| 24 | + const hPrime = h / 60.0 |
| 25 | + const secondComponent = chroma * (1.0 - Math.abs((hPrime % 2.0) - 1.0)) |
| 26 | + const lightnessModifier = l - chroma / 2.0 |
| 27 | + |
| 28 | + let rPrime = 0, |
| 29 | + gPrime = 0, |
| 30 | + bPrime = 0 |
| 31 | + |
| 32 | + if (hPrime >= 0 && hPrime < 1) { |
| 33 | + ;[rPrime, gPrime, bPrime] = [chroma, secondComponent, 0.0] |
| 34 | + } else if (hPrime >= 1 && hPrime < 2) { |
| 35 | + ;[rPrime, gPrime, bPrime] = [secondComponent, chroma, 0.0] |
| 36 | + } else if (hPrime >= 2 && hPrime < 3) { |
| 37 | + ;[rPrime, gPrime, bPrime] = [0.0, chroma, secondComponent] |
| 38 | + } else if (hPrime >= 3 && hPrime < 4) { |
| 39 | + ;[rPrime, gPrime, bPrime] = [0.0, secondComponent, chroma] |
| 40 | + } else if (hPrime >= 4 && hPrime < 5) { |
| 41 | + ;[rPrime, gPrime, bPrime] = [secondComponent, 0.0, chroma] |
| 42 | + } else if (hPrime >= 5 && hPrime < 6) { |
| 43 | + ;[rPrime, gPrime, bPrime] = [chroma, 0.0, secondComponent] |
| 44 | + } |
| 45 | + |
| 46 | + const r = ((rPrime + lightnessModifier) * 255) | 0 |
| 47 | + const g = ((gPrime + lightnessModifier) * 255) | 0 |
| 48 | + const b = ((bPrime + lightnessModifier) * 255) | 0 |
| 49 | + |
| 50 | + return [r, g, b] |
| 51 | +} |
| 52 | + |
| 53 | +export function getIcyBlueColor(value: number): [number, number, number] { |
| 54 | + const v = Math.max(0.0, Math.min(value, 1.0)) |
| 55 | + const h = ((((-128.0 * v + 191.0) % 256) + 256) % 256) * (360.0 / 255.0) |
| 56 | + const s = Math.max(0.0, Math.min(128.0 * v + 127.0, 255.0)) / 255.0 |
| 57 | + const l = Math.max(0.0, Math.min(255.0 * v, 255.0)) / 255.0 |
| 58 | + return hslToRgb(h, s, l) |
| 59 | +} |
| 60 | + |
| 61 | +export function getGrayscaleColor(value: number): [number, number, number] { |
| 62 | + const v = value * 255 |
| 63 | + return [v, v, v] |
| 64 | +} |
| 65 | + |
| 66 | +export function getGreenColor(value: number): [number, number, number] { |
| 67 | + const v = Math.max(0.0, Math.min(value, 1.0)) |
| 68 | + const h = 85.0 * (360.0 / 255.0) |
| 69 | + const s = 1.0 |
| 70 | + const l = Math.max(0.0, Math.min(200.0 * v, 255.0)) / 255.0 |
| 71 | + return hslToRgb(h, s, l) |
| 72 | +} |
| 73 | + |
| 74 | +export function generatePalette(colorFn: (value: number) => [number, number, number]): Uint8Array { |
| 75 | + const lut = new Uint8Array(256 * 4) |
| 76 | + for (let i = 0; i < 256; i++) { |
| 77 | + const [r, g, b] = colorFn(i / 255.0) |
| 78 | + const idx = i * 4 |
| 79 | + lut[idx] = r |
| 80 | + lut[idx + 1] = g |
| 81 | + lut[idx + 2] = b |
| 82 | + lut[idx + 3] = 255 |
| 83 | + } |
| 84 | + return lut |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * @description 解析 HEX 颜色字符串为 RGB |
| 89 | + */ |
| 90 | +function parseHexColor(hex: string): [number, number, number] { |
| 91 | + const r = parseInt(hex.slice(1, 3), 16) |
| 92 | + const g = parseInt(hex.slice(3, 5), 16) |
| 93 | + const b = parseInt(hex.slice(5, 7), 16) |
| 94 | + return [r, g, b] |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * @description 线性插值 |
| 99 | + */ |
| 100 | +function lerp(a: number, b: number, t: number): number { |
| 101 | + return a * (1 - t) + b * t |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * @description 从色标生成一个 256 色的 LUT |
| 106 | + * |
| 107 | + * @param stops 渐变色标 |
| 108 | + * @returns 一个 1024 字节的 Uint8Array (256 * RGBA) |
| 109 | + */ |
| 110 | +export function generateLutFromStops(stops: ColorStop[]): Uint8Array { |
| 111 | + const lut = new Uint8Array(256 * 4) |
| 112 | + |
| 113 | + if (stops.length === 0) { |
| 114 | + return lut |
| 115 | + } |
| 116 | + |
| 117 | + const sortedStops = [...stops].sort((a, b) => a.pos - b.pos) |
| 118 | + |
| 119 | + const parsedStops = sortedStops.map((s) => ({ |
| 120 | + pos: s.pos, |
| 121 | + rgb: parseHexColor(s.color), |
| 122 | + })) |
| 123 | + |
| 124 | + for (let i = 0; i < 256; i++) { |
| 125 | + const currentPos = i / 255.0 |
| 126 | + |
| 127 | + // 前面已有 if (stops.length === 0) 检查,所有下面的非空断言都是安全的 |
| 128 | + let stopA = parsedStops[0]! |
| 129 | + let stopB = parsedStops[parsedStops.length - 1]! |
| 130 | + |
| 131 | + if (currentPos <= parsedStops[0]!.pos) { |
| 132 | + stopA = parsedStops[0]! |
| 133 | + stopB = parsedStops[0]! |
| 134 | + } else if (currentPos >= parsedStops[parsedStops.length - 1]!.pos) { |
| 135 | + stopA = parsedStops[parsedStops.length - 1]! |
| 136 | + stopB = parsedStops[parsedStops.length - 1]! |
| 137 | + } else { |
| 138 | + for (let j = 0; j < parsedStops.length - 1; j++) { |
| 139 | + if (currentPos >= parsedStops[j]!.pos && currentPos <= parsedStops[j + 1]!.pos) { |
| 140 | + stopA = parsedStops[j]! |
| 141 | + stopB = parsedStops[j + 1]! |
| 142 | + break |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + const range = stopB.pos - stopA.pos |
| 148 | + const t = range < 1e-6 ? 0 : (currentPos - stopA.pos) / range |
| 149 | + |
| 150 | + const r = lerp(stopA.rgb[0], stopB.rgb[0], t) |
| 151 | + const g = lerp(stopA.rgb[1], stopB.rgb[1], t) |
| 152 | + const b = lerp(stopA.rgb[2], stopB.rgb[2], t) |
| 153 | + |
| 154 | + const idx = i * 4 |
| 155 | + lut[idx] = r |
| 156 | + lut[idx + 1] = g |
| 157 | + lut[idx + 2] = b |
| 158 | + lut[idx + 3] = 255 |
| 159 | + } |
| 160 | + |
| 161 | + return lut |
| 162 | +} |
0 commit comments