目录

moonbit-palette-forge

CI MoonBit License OSC 2026

面向设计系统的高精度配色与色彩调和工具库

A high-precision color palette generation and harmony engine for design systems — written entirely in MoonBit.


简介

moonbit-palette-forge 是一个使用 MoonBit 编写的现代化色彩科学工具库,专为设计系统(Design System)的色彩工程场景而构建。项目实现了从色彩空间转换、调和算法、无障碍合规检测,到设计令牌生成与多格式导出的完整工作流。

核心特性

模块 功能
src/core 基础色彩类型、数学工具、格式化输出
src/spaces 14+ 色彩空间相互转换(RGB, HSL, HSV, HWB, CMYK, XYZ, LAB, LCH, OkLAB, OkLCH, YUV, YCbCr, HSLuv, CIELUV, IPT, HSI, Hunter Lab…)
src/harmony 7 种专业配色调和算法 + OkLCH 渐变 + K-Means 聚类 + 色盘分析
src/accessibility WCAG 2.1 / APCA 对比度分析、自动修正器、完整对比度网格
src/vision 8 种色觉障碍模拟(Brettel / Machado 模型)+ CSS 滤镜矩阵
src/distance CIE76 / CIE94 / CIEDE2000 / OkLCH 色差计算 + 最近邻匹配
src/tokens Material 3 / Tailwind / Ant Design / Radix UI / Open Color 设计令牌生成
src/export CSS/SCSS/JSON(W3C)/Tailwind Config/SVG/HTML/Android XML/Swift/Flutter/CSV/GIMP 多格式导出
src/lib 统一高层门面 API
src/cli 命令行演示程序

快速开始

环境要求

克隆与构建

git clone https://github.com/lkdrt/moonbit-palette-forge.git
cd moonbit-palette-forge
moon build

运行测试

moon test --deny-warn

运行命令行演示

moon run src/cli

示例输出:

=======================================================================
      MoonBit Palette Forge (lkdrt/palette_forge v0.1.0)
      面向设计系统的高级调色与调和工具 - Design System Engine
=======================================================================
[*] Seed Color: #3b82f6 (rgb(59, 130, 246))
[+] Generated Analogous Palette (5 colors):
    - Analogous-1: #3bf6af (Analogous)
    - Analogous-2: #3bdff6 (Analogous)
    - Analogous-3: #3b82f6 (Seed)
    - Analogous-4: #513bf6 (Analogous)
    - Analogous-5: #af3bf6 (Analogous)
...
[✓] MoonBit Palette Forge Execution Completed Successfully!

模块文档

色彩空间转换 (src/spaces)

let red = @core.Color::from_rgb_bytes(255, 0, 0)

// 转换为各色彩空间
let hsl  = @spaces.color_to_hsl(red)    // HSL
let lab  = @spaces.color_to_lab(red)    // CIE L*a*b*
let oklch = @spaces.color_to_oklch(red) // OkLCH

// 从字符串解析
let c = @spaces.parse_color_string("#3b82f6")
let c2 = @spaces.parse_color_string("hsl(210, 100%, 60%)")
let c3 = @spaces.parse_color_string("rgb(59, 130, 246)")

调和配色 (src/harmony)

let seed = @lib.create_color("#6366f1")

// 7 种调和方式
let mono   = @lib.create_harmony_palette(seed, Monochromatic)
let ana    = @lib.create_harmony_palette(seed, Analogous)
let comp   = @lib.create_harmony_palette(seed, Complementary)
let split  = @lib.create_harmony_palette(seed, SplitComplementary)
let triad  = @lib.create_harmony_palette(seed, Triadic)
let tetrad = @lib.create_harmony_palette(seed, Tetradic)
let square = @lib.create_harmony_palette(seed, Square)

// OkLCH 渐变
let stops = @harmony.generate_gradient(seed, target, steps=10)

无障碍合规 (src/accessibility)

let fg = @lib.create_color("#4f46e5")
let bg = @lib.create_color("#ffffff")

// WCAG 2.1 评估
let result = @lib.check_wcag(fg, bg)
// result.ratio, result.aa_normal, result.aaa_normal, result.compliance

// 自动修正至 AA 级别
let fixed = @lib.fix_contrast(fg, bg, target=4.5)

// 获取完整对比度网格
let grid = @accessibility.generate_contrast_grid(palette)

设计令牌生成 (src/tokens)

let brand = @lib.create_color("#6366f1")

// Material Design 3 Tonal Palette (13 tones)
let m3 = @lib.create_material_tokens(brand)

// Tailwind CSS 11-step scale
let tw = @lib.create_tailwind_tokens(brand)

// Ant Design 10-step scale
let ant = @tokens.generate_ant_design_scale(brand)

// Radix UI 12-step semantic scale
let radix = @tokens.generate_radix_scale(brand)

多格式导出 (src/export)

// CSS Custom Properties
let css = @lib.export_palette(palette, CSS)
// :root { --color-1: #6366f1; ... }

// W3C 设计令牌 JSON
let json = @lib.export_palette(palette, JSON)

// Tailwind config.js
let tw = @lib.export_palette(palette, TailwindConfig)

// SVG 色板可视化
let svg = @lib.export_palette(palette, SVG)

// Android colors.xml
let xml = @export.export_android_xml(palette)

// SwiftUI / Dart Flutter
let swift  = @export.export_swift_enum(palette)
let flutter = @export.export_flutter_dart(palette)

// GIMP GPL 色板
let gpl = @export.export_gimp_gpl(palette)

项目结构

moonbit-palette-forge/
├── moon.mod.json               # MoonBit 模块定义
├── LICENSE                     # Apache-2.0
├── NOTICE                      # 来源声明
├── README.md                   # 项目说明(本文件)
├── .github/
│   └── workflows/
│       └── ci.yml              # GitHub Actions CI 配置
└── src/
    ├── core/                   # 基础类型、数学与格式化工具
    ├── spaces/                 # 14+ 色彩空间转换实现
    ├── harmony/                # 调和算法与色盘生成器
    ├── accessibility/          # WCAG 合规与无障碍工具
    ├── vision/                 # 色觉障碍模拟引擎
    ├── distance/               # 色差计算与最近邻匹配
    ├── tokens/                 # 设计令牌规模生成器
    ├── export/                 # 多格式设计令牌导出器
    ├── lib/                    # 统一门面库 API
    └── cli/                    # 命令行演示程序

代码规模

文件数 源码行数(约)
core 5 708
spaces 16 820
harmony 9 360
accessibility 6 180
vision 6 170
distance 6 224
tokens 7 220
export 11 480
lib 2 140
cli 1 67
合计 69 >4000

CI 持续集成

本项目配置了 GitHub Actions 多平台 CI,在每次 push 和 PR 时自动完成:

  • ✅ 代码格式检查 (moon fmt --check)
  • ✅ 项目构建 (moon build --deny-warn)
  • ✅ 全量单元测试 (moon test --deny-warn)
  • ✅ 接口文件生成 (moon info)
  • 🖥️ 平台:Ubuntu / macOS / Windows(三端矩阵)

参赛信息

本项目参加 开源之夏 2026 (OSC 2026) MoonBit 赛道。


许可证

Copyright 2026 lkdrt

Licensed under the Apache License, Version 2.0. See LICENSE for details.

关于

调色板生成、WCAG 对比度、色盲模拟、品牌色变体、CSS token 导出;避免只做颜色空间转换。

448.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号