ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

Windows Terminal 之 ColorTool:从 iTerm 主题到控制台配色表的控制台调色工具完整指南

Windows Terminal 之 ColorTool:从 iTerm 主题到控制台配色表的控制台调色工具完整指南 Windows Terminal 之 ColorTool从 iTerm 主题到控制台配色表的控制台调色工具完整指南【免费下载链接】terminalThe new Windows Terminal and the original Windows console host, all in the same place!项目地址: https://gitcode.com/GitHub_Trending/term/terminalColorTool 是 Windows Terminal 仓库src/tools/ColorTool中自带的一个独立 .NET 工具它把 Windows 控制台的 16 色调色板与 iTerm2 主题、JSON 配色文件打通一条命令即可把.ini、.json或.itermcolors配色方案应用到当前控制台、系统注册表默认值、支持 VT 转义序列的终端含 WSL或直接生成可粘贴进 Windows Terminalsettings.json的 JSON 色块。读完本文你能掌握 ColorTool 的完整命令行用法、三种方案文件的格式细节与查找规则、四种输出目标背后的注册表/VT 实现原理以及如何把自己的配色主题集成进控制台与 Windows Terminal。一、ColorTool 是什么按 src/tools/ColorTool/README.md 的定义ColorTool 用于帮助你设置 Windows Console 的调色板它默认把指定.itermcolors、.json或.ini文件中的颜色应用到当前控制台窗口但不会自动保存属性——若想持久化需要打开属性页并点确定或者显式使用-d/-b参数直接写注册表。仓库在schemes/目录中附带了多种格式的方案示例用户也可以往里添加自己偏好的方案。理解 ColorTool 的关键背景是 Windows 控制台的16 色调色板控制台文字颜色只有 16 个固定槽位8 个暗色 8 个亮色注册表中以ColorTable00ColorTable15的 DWORD 值保存而 DWORD 内部的通道排布是 BRGR 在低字节、B 在高字节。ColorTool 内部统一用RGB(r, g, b)宏把这个 BRG 值打包成uint所有解析器和输出目标都围绕这张 16 色表工作见 ColorScheme.cs。二、命令行参考Functions 与 OptionsREADME 给出的完整用法如下源自 README.md并与 Program.cs 中的参数解析逐一对应Usage: ColorTool.exe Function ColorTool.exe [Options] Scheme name Parameters: Function : One and only one of the switches listed in the Functions section below. Scheme name: The name of a color scheme. ColorTool will try to first load it as an .ini file color scheme If that fails, it will look for it as a .json file color scheme If that fails, it will look for it as a .itermcolors file color scheme. Must be the last parameter passed to ColorTool. [Option] : One or more of the switches listed in the Options section below. Must appear before scheme name. Functions: -?, --help : Display this help message -c, --current : Print the color table for the currently applied scheme -v, --version : Display the version number -l, --location : Displays the full path to the schemes directory -s, --schemes : Displays all available schemes -o, --output filename : output the current color table to a file in .ini format Options: -q, --quiet : Dont print the color table after applying -e, --errors : Report scheme parsing errors on the console -d, --defaults : Apply the scheme to only the defaults in the registry -b, --both : Apply the scheme to both the current console and the defaults. -x, --xterm : Set the colors using VT sequences. Used for setting the colors in WSL. Only works in Windows versions 17048. -t, --terminal : Output the colors in JSON format for copying into a Windows Terminal settings file. -a, --allcolors: Output extended color table. best for 110 column terminals几个使用要点Function 每次调用只能指定一个出现第一个功能开关后其余开关被忽略Option 必须写在方案名之前方案名必须是最后一个参数Program.cs 中按args顺序switch解析args[args.Length - 1]被当作方案名。-o是唯一带参数的功能开关把当前控制台的颜色表导出为.ini文件实现在 IniSchemeWriter.cs 中。常用组合示例预览某个方案效果只改当前窗口ColorTool.exe campbell静默应用并写注册表默认值ColorTool.exe -q -b solarized_dark在 WSL 中通过 VT 序列改色ColorTool.exe -x campbell生成 Windows Terminal 的 JSON 色块ColorTool.exe -t OneHalfDark导出当前配色ColorTool.exe -o mycolors.ini列出全部方案带色块预览ColorTool.exe -s三、方案文件格式与查找规则3.1 解析器机制先 INI后 JSON再 iTermSchemeManager.GetScheme依次尝试各解析器第一个成功解析出非空结果的解析器胜出SchemeManager.cspublic static ColorScheme GetScheme(string schemeName, bool reportErrors false) { return GetParsers() .Select(parser parser.ParseScheme(schemeName, reportErrors)) .FirstOrDefault(x x ! null); }解析器通过反射在程序集中自动发现所有ISchemeParser实现SchemeManager.cs接口定义为Name、FileExtension与ParseScheme三个成员ISchemeParser.cs。当前有内置三个解析器INI 解析器、concfg JSON 解析器、iTerm XML 解析器。3.2 查找路径7 个候选位置无论哪种格式文件查找都走同一套搜索路径SchemeManager.cs// Search order, for argument name, where exe is the dir of the exe. // 1. ./name // 2. ./name.ext // 3. ./schemes/name // 4. ./schemes/name.ext // 5. exe/schemes/name // 6. exe/schemes/name.ext // 7. name (as an absolute path)也就是说你既可以cd到一个装满*.itermcolors的目录直接运行也可以把方案放进schemes/目录或传绝对路径——这正是 README 中把任意.itermcolors粘贴进schemes/目录或者先cd到含*.itermcolors文件的目录再运行这一工作方式的底层依据。3.3 INI 格式控制台属性页同款格式IniSchemeParser.cs 通过GetPrivateProfileString读取三个段落[table]16 个固定键名顺序即 Windows 调色板顺序注意是 BRG 排布的 16 色表DARK_BLACK、DARK_BLUE、DARK_GREEN、DARK_CYAN、DARK_RED、DARK_MAGENTA、DARK_YELLOW、DARK_WHITE、BRIGHT_BLACK…BRIGHT_WHITE。取值支持R,G,B或#RRGGBB两种写法ParseColor按首字符是否为#分派。[screen]FOREGROUND、BACKGROUND、CURSOR。前两个值可以直接引用[table]中的颜色名如DARK_WHITE也可以是具体颜色值解析器会先查颜色名索引查不到再按颜色值解析IniSchemeParser.cs。[popup]弹窗菜单等的FOREGROUND/BACKGROUND同样支持颜色名引用。以仓库自带的 schemes/campbell.ini 为例这是一个完整、可直接使用的 INI 方案[table] DARK_BLACK 12,12,12 DARK_BLUE 0,55,218 DARK_GREEN 19,161,14 DARK_CYAN 58,150,221 DARK_RED 197,15,31 DARK_MAGENTA 136,23,152 DARK_YELLOW 193,156,0 DARK_WHITE 204,204,204 BRIGHT_BLACK 118,118,118 BRIGHT_BLUE 59,120,255 BRIGHT_GREEN 22,198,12 BRIGHT_CYAN 97,214,214 BRIGHT_RED 231,72,86 BRIGHT_MAGENTA 180,0,158 BRIGHT_YELLOW 249,241,165 BRIGHT_WHITE 242,242,242 [screen] FOREGROUND DARK_WHITE BACKGROUND DARK_BLACK [popup] FOREGROUND DARK_MAGENTA BACKGROUND BRIGHT_WHITE [info] name Campbell author crloew如果[table]缺任何一个颜色且开启了-e/--errors会打印解析错误并放弃该方案-e参数对应源码中的reportErrors标志控制是否向控制台报告解析失败原因。3.4 JSON 格式concfg 风格JsonParser.cs自称 concfg Parser读取旧版控制台 JSON 配色文件。根节点需包含 16 个小写颜色键black、dark_blue、dark_green、dark_cyan、dark_red、dark_magenta、dark_yellow、gray对应 Dark White、dark_gray对应 Bright Black、blue、green、cyan、red、magenta、yellow、white。此外还支持两个可选的逗号对screen_colors形如fg_name,bg_name引用上面的颜色名与popup_colors解析器把这两个名字对映射回调色板索引得到前景/背景与弹窗颜色JsonParser.cs。3.5 .itermcolors 格式iTerm2 plistXmlSchemeParser.cs 解析 iTerm2 的 plist 结构调色板 16 色的键是Ansi 0 ColorAnsi 15 Color注意 ANSI 编号到 Windows 调色板槽位的映射关系0 黑、4 蓝、2 绿、6 青、1 红、5 紫、3 黄、7 白亮色为 8 加偏移额外支持Foreground Color、Background Color、Cursor Color三个键每个颜色由Red Component/Green Component/Blue Component三个01 的浮点数给出源码乘以 255 取整XmlSchemeParser.cs找齐 16 个调色板颜色才算解析成功否则配合-e报告InvalidNumberOfColors。四、四种应用目标同一个方案四种落地方式README 中 Options 的四个去向开关默认当前控制台 /-d/-b/-x/-t在源码里体现为一组IConsoleTarget实现由 Program.cs 的GetConsoleTargets()按命令行标志组合出目标列表if (setDefaults) { yield return new DefaultConsoleTarget(); } if (setProperties) { if (setUnixStyle) yield return new VirtualTerminalConsoleTarget(); else if (setTerminalStyle) yield return new TerminalSchemeConsoleTarget(); else yield return new CurrentConsoleTarget(); }注意-x与-t在解析时都会置位setProperties因此它们与默认行为互斥VT 模式、Terminal JSON 模式与直接改当前控制台三选一而-d/-b控制是否追加注册表目标。4.1 当前控制台默认CurrentConsoleTarget通过封装的 Console APIConsoleAPI.cs直接改写当前窗口的 16 色表与前后景色。这也是 README 强调不会自动保存的原因改的是窗口级属性关窗即失。4.2 注册表默认值-d/-bDefaultConsoleTarget直接写HKEY_CURRENT_USER\ConsoleDefaultConsoleTarget.csColorTable00ColorTable1516 个 DWORD 调色板值注意键名补零的格式CursorColor光标色方案未定义时写-1表示用默认ScreenColors一个 16 位属性字低 4 位是前景色索引、高 4 位是背景色索引DefaultForeground/DefaultBackground只有当颜色在调色板中没有完全相同的槽位时才写入具体 RGB 值否则写-1让系统用属性字里的索引值PopupColors弹窗配色属性字。由于ScreenColors只能存调色板索引而非任意 RGBColorTool 在 ColorScheme.cs 里做了最近邻计算把方案的前景色、背景色分别与 16 槽 RGB 值比相似度取最接近的索引。相似度采用加权 RGB 距离ColorScheme.cs绿色通道权重恒为 4红/蓝通道权重随亮度动态调整——这是比欧氏距离更符合视觉感知的近似度量源码注释中还保留了 HSV 空间最近邻、L2 距离等备选启发式。4.3 VT 转义序列-x用于 WSLVirtualTerminalConsoleTarget不改任何 API而是直接打印 OSC 序列VirtualTerminalConsoleTarget.cs\x1b]10;rgb:RR/GG/BB\x1b\\前景色\x1b]11;rgb:RR/GG/BB\x1b\\背景色\x1b]12;rgb:RR/GG/BB\x1b\\光标色\x1b]4;{i};rgb:RR/GG/BB\x07对 16 个 ANSI 索引 i 逐个设置调色板颜色。由于控制台必须先开启ENABLE_VIRTUAL_TERMINAL_PROCESSING模式才能理解这些序列Program.DoInVTModeProgram.cs会先保存原ConsoleMode、打开 VT 位、执行动作后再恢复原模式——-s的色块预览也复用了这套机制。这里同样存在 Windows 槽位与 ANSI 索引的换算表VirtualTerminalConsoleTarget.cs与 iTerm 解析器中的映射同源。README 注明该模式要求 Windows 版本 ≥ 17048。4.4 生成 Windows Terminal JSON-tTerminalSchemeConsoleTarget不做任何应用而是打印一段可直接粘进 Windows Terminalsettings.json的schemes数组的 JSON 色块TerminalSchemeConsoleTarget.cs键名为 Windows Terminal 认识的black/blue/…/brightWhite加可选的foreground/background/cursorColor颜色格式为#RRGGBB。它会提示你别忘了在上一条方案后补逗号。这是把社区 iTerm 主题快速搬进 Windows Terminal 的最短路径之一。五、内置方案与加方案实践schemes/目录src/tools/ColorTool/schemes/当前自带 10 个方案文件cmd-legacy.ini2017 年 7 月之前的 Windows 控制台旧版经典配色campbell.ini秋季创作者更新起成为控制台默认的 Campbell 配色另有campbell-legacy.ini、campbell-absolute.ini两个变体iTerm 格式OneHalfDark、OneHalfLight、solarized_dark、solarized_light、tango_dark、tango_light、deuteranopia色盲友好。README 建议从社区的 iTerm2-Color-Schemes 仓库含大量方案与预览图挑选.itermcolors文件也可以借助在线配色编辑器 terminal.sexy 的 Import/Export 功能以 iTerm2 格式可视化编辑方案两者都把文件放进schemes/即可被 ColorTool 识别。仓库还附带了一个方案巡礼脚本 all.bat遍历schemes\*逐个套用并pause一路翻到喜欢的配色后 CtrlC 结束即可for %%i in (schemes\*) do ( echo %%i .\colortool.exe %%i pause )脚本注释里也提醒当前窗口历史会被不断改写只有最近一个主题的表格显示的是它自己的颜色。六、构建与使用前提使用README 指引下载 ColorTool 的独立发布包并解压即用在仓库内查看对应源码位于 src/tools/ColorTool/工程入口为ColorTool/ColorTool.csproj与解决方案 ColorTool.sln。构建可用 Visual Studio 打开解决方案或按 README 说明使用自带的build.bat从命令行自动探测 MSBuild 版本构建。前提与限制-x依赖新版 Windows 的 VT 处理≥ 17048且目标终端必须允许 VT 输出默认应用只影响当前控制台窗口持久化需要属性页确定或-d/-b写HKCU\Console解析顺序固定为 INI → JSON → iTerm同名文件在不同目录时按第 3.2 节的 7 路径顺序取第一个存在者-a/--allcolors输出扩展色表适合超过 110 列的宽终端避免表格截断。七、小结ColorTool 用不到千行 C# 代码解决了控制台配色的完整闭环多格式解析INI / concfg JSON / iTerm plist反射自动注册、统一查找规则工作目录与安装目录的schemes/、四种落地目标当前控制台 API、HKCU\Console注册表、OSC VT 序列、Windows Terminal JSON再加上最近邻颜色索引这类针对控制台 16 色表约束的工程细节。无论是想给老 conhost 换一口味觉熟悉的 iTerm 主题还是想为 Windows Terminal 批量导入社区方案它都是仓库中现成、可直接构建的工具。【免费下载链接】terminalThe new Windows Terminal and the original Windows console host, all in the same place!项目地址: https://gitcode.com/GitHub_Trending/term/terminal创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表