彩色MCP服务器
用于在各种网络颜色格式之间进行转换的模型上下文协议(MCP)服务器。此服务器提供用于检测颜色格式并在HEX、RGB、HSL、HSB/HSV、OKLCH、LAB、XYZ、HWB和CMYK格式之间转换颜色的工具。
特性
- 自动检测:自动检测输入颜色格式
- 多种格式:支持10多种颜色格式
- Alpha通道:根据需要保留或剥离阿尔法通道
- 颜色比较:使用OKLCHΔE进行感知相似性分析
- 无障碍:WCAG对比度计算
- 高精度:使用精确的颜色空间转换
- 快速:纯Go实现,无外部依赖
- 综合测试:>90%的测试覆盖率
支持的颜色格式
| 格式 | 示例 | 描述 |
|---|---|---|
| 六角形 | #FF0000, #FF000080 | 十六进制RGB/RGBA |
| RGB | rgb(255, 0, 0), rgb(100%, 0%, 0%) | RGB颜色空间 |
| RGBA | rgba(255, 0, 0, 0.5) | 带阿尔法的RGB |
| HSL | hsl(0, 100%, 50%) | 色调、饱和度、亮度 |
| HSLA公司 | hsla(0, 100%, 50%, 0.5) | HSL与阿尔法 |
| HSB/HSV | hsb(0, 100%, 100%) | 色调、饱和度、亮度/值 |
关闭 oklch(0.5 0.1 120) | 感知均匀的颜色空间 | |
| 实验室 | lab(50 50 50) | CIE LAB颜色空间 |
| XYZ | xyz(0.5 0.5 0.5) | CIE XYZ颜色空间 |
| HWB | hwb(0 0% 0%) | 色调、白色、黑色 |
| CMYK | cmyk(0% 100% 100% 0%) | 青色、品红色、黄色、钥匙色(黑色) |
安装
来源
# Clone the repository
git clone https://github.com/InkyQuill/color-mcp.git
cd color-mcp
# Build the binary
go build -o color-mcp
# (Optional) Install to system
sudo install color-mcp /usr/local/bin/使用Go安装
go install github.com/InkyQuill/color-mcp@latestMCP配置
添加到MCP客户端配置中(通常在 ~/.config/claude/mcp.json 或类似):
{
"mcpServers": {
"color-converter": {
"command": "/path/to/color-mcp"
}
}
}替换 /path/to/color-mcp 与二进制文件的实际路径(例如。, /usr/local/bin/color-mcp 或 ~/go/bin/color-mcp).
用法
可用工具
1.转换颜色
将颜色从一种格式转换为另一种格式。
参数:
color(string,必填):以任何支持的格式输入颜色值target_format(字符串,必填):目标格式(十六进制、rgb、hsl、hsla、hsb、oklch、lab、xyz、hwb、cmyk)preserve_alpha(布尔值,可选):是否保留alpha通道(默认值:true)
例子:
Convert #FF0000 to HSL format2.检测格式
检测输入颜色字符串的格式。
参数:
color(字符串,必填):用于检测格式的颜色值
例子:
Detect the format of rgb(255, 0, 0)3.列表格式
列出所有支持的颜色格式。
例子:
List all supported color formats4.比较颜色
比较两种颜色的感知相似性、对比度和成分差异。
参数:
color1(string,必填):任何支持格式的第一个颜色值color2(string,必填):任何支持格式的第二个颜色值detailed(布尔值,可选):是否包含详细的组件分解(默认值:false)
例子:
Compare #FF0000 and #00FF00 with detailed output结果(基本):
Color Comparison: #FF0000 vs #00FF00
Perceptual Difference: 0.520 ΔE
Verdict: different
Contrast Ratio: 2.91:1 (Fail)结果(详细):
Color Comparison: #FF0000 (hex) vs #00FF00 (hex)
Perceptual Difference: 0.520 ΔE
Verdict: different
Component Breakdown:
Hue Difference: 120.0°
Lightness Difference: 0.0%
Saturation Difference: 0.0%
Contrast Ratio: 2.91:1
WCAG Grade: Fail例子
将HEX转换为HSL
Convert #FF5733 to hsl结果:
Input color: #FF5733 (format: hex)
Output color: hsl(11, 100%, 60%) (format: hsl)
Alpha preserved: true将RGB转换为OKLCH
Convert rgb(255, 0, 0) to oklch结果:
Input color: rgb(255, 0, 0) (format: rgb)
Output color: oklch(0.6280 0.2577 29.23 / 1.00) (format: oklch)
Alpha preserved: true检测颜色格式
Detect the format of hsl(120, 100%, 50%)结果:
Color: hsl(120, 100%, 50%)
Detected format: hsl阿尔法通道处理
Convert rgba(255, 0, 0, 0.5) to hex结果:
Input color: rgba(255, 0, 0, 0.5) (format: rgba)
Output color: #FF000080 (format: hex)
Alpha preserved: trueConvert rgba(255, 0, 0, 0.5) to rgb without preserving alpha结果:
Input color: rgba(255, 0, 0, 0.5) (format: rgba)
Output color: rgb(255, 0, 0) (format: rgb)
Alpha preserved: false颜色比较
Compare #000000 and #FFFFFF结果:
Color Comparison: #000000 vs #FFFFFF
Perceptual Difference: 1.000 ΔE
Verdict: different
Contrast Ratio: 21.00:1 (AAA)Compare hsl(350, 100%, 50%) and hsl(10, 100%, 50%) with detailed output结果:
Color Comparison: hsl(350, 100%, 50%) (hsl) vs hsl(10, 100%, 50%) (hsl)
Perceptual Difference: 0.032 ΔE
Verdict: slightly different
Component Breakdown:
Hue Difference: 20.0°
Lightness Difference: 0.0%
Saturation Difference: 0.0%
Contrast Ratio: 1.06:1
WCAG Grade: Fail发展
运行测试
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run tests with race detection
go test -race ./...
# Run benchmarks
go test -bench=. ./...建筑
# Build for current platform
go build
# Build for multiple platforms
GOOS=linux GOARCH=amd64 go build -o color-mcp-linux-amd64
GOOS=darwin GOARCH=amd64 go build -o color-mcp-darwin-amd64
GOOS=darwin GOARCH=arm64 go build -o color-mcp-darwin-arm64
GOOS=windows GOARCH=amd64 go build -o color-mcp-windows-amd64.exe项目结构
color-mcp/
├── internal/
│ ├── types.go # Color format detection and parsing
│ ├── convert.go # Color conversion algorithms
│ ├── converter.go # Main conversion logic and formatting
│ ├── compare.go # Color comparison and contrast calculation
│ ├── constants.go # Color space constants and thresholds
│ ├── value_objects.go # Channel value types
│ └── *_test.go # Comprehensive tests
├── main.go # MCP server implementation
├── go.mod
├── go.sum
└── README.md颜色转换算法
转换器使用OKLCH作为最高质量转换的中间格式:
- 输入检测:解析并检测输入格式
- RGB转换:将输入格式转换为RGB
- 目标转化:将RGB转换为目标格式
- 格式化:根据目标规范格式化输出
特殊处理:
- OKLCH:使用适当的CIE XYZ中间体以获得感知精度
- 实验室/XYZ:标准CIE颜色空间转换
- CMYK:应用正确的黑键生成
- 阿尔法:根据参数保留或剥离
贡献
欢迎投稿!拜托:
- 分叉存储库
- 创建要素分支
- 添加新功能的测试
- 确保所有测试通过
- 提交拉取请求
许可证
MIT许可证-有关详细信息,请参阅许可证文件
作者
由Inky(@Inky)创建
致谢
- 基于CSS颜色模块4级的颜色空间转换公式
- 基于Björn Ottosson工作的OKLCH实施
- Anthropic的MCP协议规范
