| 188 | } |
| 189 | |
| 190 | function resolveTheme(theme: ThemeJson, mode: "dark" | "light") { |
| 191 | const defs = theme.defs ?? {} |
| 192 | function resolveColor(c: ColorValue): RGBA { |
| 193 | if (c instanceof RGBA) return c |
| 194 | if (typeof c === "string") { |
| 195 | if (c === "transparent" || c === "none") return RGBA.fromInts(0, 0, 0, 0) |
| 196 | |
| 197 | if (c.startsWith("#")) return RGBA.fromHex(c) |
| 198 | |
| 199 | if (c.startsWith("rgba(")) { |
| 200 | const match = c.match(/rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)/) |
| 201 | if (match) { |
| 202 | return RGBA.fromInts( |
| 203 | Number.parseInt(match[1]), |
| 204 | Number.parseInt(match[2]), |
| 205 | Number.parseInt(match[3]), |
| 206 | Math.round(Number.parseFloat(match[4]) * 255), |
| 207 | ) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if (defs[c] != null) { |
| 212 | return resolveColor(defs[c]) |
| 213 | } else if (theme.theme[c as keyof ThemeColors] !== undefined) { |
| 214 | return resolveColor(theme.theme[c as keyof ThemeColors]!) |
| 215 | } else { |
| 216 | throw new Error(`Color reference "${c}" not found in defs or theme`) |
| 217 | } |
| 218 | } |
| 219 | if (typeof c === "number") { |
| 220 | return ansiToRgba(c) |
| 221 | } |
| 222 | return resolveColor(c[mode]) |
| 223 | } |
| 224 | |
| 225 | const resolved = Object.fromEntries( |
| 226 | Object.entries(theme.theme) |
| 227 | .filter( |
| 228 | ([key]) => |
| 229 | key !== "selectedListItemText" && |
| 230 | key !== "backgroundMenu" && |
| 231 | key !== "thinkingOpacity" && |
| 232 | key !== "markdownTableBorder" && |
| 233 | key !== "markdownTableHeader" && |
| 234 | key !== "markdownTableCell", |
| 235 | ) |
| 236 | .map(([key, value]) => { |
| 237 | return [key, resolveColor(value as ColorValue)] |
| 238 | }), |
| 239 | ) as Partial<ThemeColors> |
| 240 | |
| 241 | // Handle selectedListItemText separately since it's optional |
| 242 | const hasSelectedListItemText = theme.theme.selectedListItemText !== undefined |
| 243 | if (hasSelectedListItemText) { |
| 244 | resolved.selectedListItemText = resolveColor(theme.theme.selectedListItemText!) |
| 245 | } else { |
| 246 | // Backward compatibility: if selectedListItemText is not defined, use background color |
| 247 | // This preserves the current behavior for all existing themes |