ColorKit:高级 Swift 色彩管理 - Openclaw Skills

作者:互联网

2026-03-30

AI教程

什么是 ColorKit?

ColorKit 是一个全面的跨平台 Swift 库,专为整个 Apple 生态系统中的专业级色彩管理而设计。本技能提供实施复杂色彩逻辑的专家指导,涵盖从基础十六进制初始化到基于 OKLAB 的高级感知色域映射。通过在 Openclaw Skills 中利用此工具,开发者可以确保在 iOS、macOS、watchOS 和 visionOS 上实现高保真色彩呈现和视觉一致性。

该技能强调现代标准,为 Display P3 和 Adobe RGB 等广色域空间提供深度支持。它弥合了技术色彩科学与实际 UI 开发之间的差距,为 SwiftUI、UIKit 和 AppKit 提供无缝集成模式。无论您是在构建设计工具,还是确保应用符合严格的无障碍要求,Openclaw Skills 的这一补充都为精确的色彩控制提供了所需的技术深度。

下载入口:https://github.com/openclaw/skills/tree/main/skills/szpakkamil/colorkit

安装与下载

1. ClawHub CLI

从源直接安装技能的最快方式。

npx clawhub@latest install colorkit

2. 手动安装

将技能文件夹复制到以下位置之一

全局模式 ~/.openclaw/skills/ 工作区 /skills/

优先级:工作区 > 本地 > 内置

3. 提示词安装

将此提示词复制到 OpenClaw 即可自动安装。

请帮我使用 Clawhub 安装 colorkit。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。

ColorKit 应用场景

  • 创建符合 WCAG 2.1 和现代 APCA 对比度标准的无障碍用户界面。
  • 直接在 Swift 中实现 Photoshop 风格的混合模式,如正片叠底、叠加和屏幕。
  • 在不同空间之间转换颜色,同时使用感知色域映射保留视觉意图。
  • 生成可自动在明亮和黑暗模式之间切换的自适应颜色集。
  • 使用 Codable 和 Sendable 协议管理复杂的颜色数据持久化。
ColorKit 工作原理
  1. 确定特定的色域要求,例如标准 sRGB 或广色域 Display P3 支持。
  2. 使用 CKColor 统一入口点初始化颜色,以自动处理平台相关的元数据。
  3. 使用 OKLAB 等专业级算法进行高级变换或混合,以确保视觉准确性。
  4. 使用 APCA 算法运行无障碍审计,以确保排版在各种字体大小和粗细下保持可读性。
  5. 使用适用于 SwiftUI、UIKit 或 Core Graphics 的内置桥接器将生成的逻辑导出到原生 UI 组件中。

ColorKit 配置指南

要将 ColorKit 集成到您的 Swift 项目中,请使用 Swift Package Manager。将以下存储库 URL 添加到您的项目依赖项中:

https://github.com/SzpakKamil/ColorKit.git

添加后,将库导入您的 Swift 文件中,即可开始在 Openclaw Skills 中使用扩展色彩功能:

import ColorKit

要求:Swift 5.9+、iOS 13.0+、macOS 10.15+、tvOS 13.0+、watchOS 6.0+ 或 visionOS 1.0+。

ColorKit 数据架构与分类体系

该技能将颜色数据和逻辑组织成模块化分类,以便于参考:

模块 描述
CKColor 支持自适应变体和持久化的核心通用颜色结构体。
ColorModels OKLAB、HSL、CMYK 和其他颜色模型的专门逻辑。
ColorOperations 色域映射、转换和基础修改的方法。
Accessibility 针对 WCAG 和 APCA 标准的详细对比度计算逻辑。
Blending 高级图层混合模式和不透明度处理。
NativeBridges SwiftUI、UIKit、AppKit 和 Core Graphics 的集成映射。
name: ColorKit
description: 'Expert guidance on ColorKit, a Swift library for advanced color manipulation, conversion, and accessibility management. Use when developers mention: (1) CKColor, CKBlendMode, CKAPCA, (2) color space conversion (OKLAB, Display P3, sRGB), (3) WCAG or APCA contrast checks, (4) hex color initialization, (5) dynamic/adaptive colors for Dark Mode, (6) perceptual gamut mapping.'

ColorKit Skill

Overview

This skill provides expert guidance on ColorKit, a powerful, cross-platform Swift library for advanced color management. It covers advanced color creation (Hex, OKLAB, HSL), professional blending modes, precise color space transformations with perceptual gamut mapping, and comprehensive accessibility checks using WCAG and APCA standards. Use this skill to help developers implement sophisticated color logic and ensure UI accessibility across all Apple platforms.

Agent Behavior (Follow These Rules)

  1. Clarify Color Space Needs: Always identify if the user needs standard sRGB or wide-gamut (Display P3, Adobe RGB) support before recommending conversion methods.
  2. Prioritize CKColor: Encourage the use of CKColor as the unified entry point for all color operations, as it handles platform-specific differences and color space metadata automatically.
  3. Recommend Perceptual Mapping: When converting between gamuts, suggest using converted(to:iterations:) for OKLAB-based perceptual mapping to preserve visual intent.
  4. Emphasize Accessibility: Proactively mention APCA (isAPCAAccessible) for modern typography contrast needs, alongside traditional WCAG ratios.
  5. Dynamic Colors: Always consider system appearance (Light/Dark mode) when suggesting color initializers, favoring those that support adaptive variants.
  6. Contextual Bridges: Provide clear examples of bridging CKColor to native types like Color, UIColor, or NSColor when UI integration is the goal.

Project Settings

ColorKit's behavior is influenced by the project's deployment targets and Swift version.

  • Deployment Targets: iOS 13.0+, macOS 10.15+, tvOS 13.0+, watchOS 6.0+, and visionOS 1.0+.
  • Swift Version: Requires Swift 5.9+.

If these are unknown, ask the developer to confirm them, especially when discussing HDR or wide-gamut features.

Quick Decision Tree

When a developer needs ColorKit guidance, follow this decision tree:

  1. Creating a new color?

    • From Hex string/int → references/CKColor.md
    • Using specific models (OKLAB, HSL, CMYK) → references/ColorModels.md
    • Adaptive for Light/Dark mode → references/CKColor.md
  2. Converting between color spaces?

    • Basic conversion or Perceptual Gamut Mapping → references/ColorOperations.md
    • Handling wide-gamut (P3, Adobe RGB) → references/ColorOperations.md
  3. Performing accessibility checks?

    • WCAG 2.1 Contrast Ratio → references/Accessibility.md
    • APCA (WCAG 3.0) Perceptual Contrast → references/Accessibility.md
    • Font-specific readability → references/Accessibility.md
  4. Blending or modifying colors?

    • Photoshop-style blending (Multiply, Overlay, etc.) → references/Blending.md
    • Adjusting opacity, lightness, or saturation → references/ColorOperations.md
  5. Integrating with UI frameworks?

    • SwiftUI (Color, ShapeStyle) → references/NativeBridges.md
    • UIKit/AppKit (UIColor, NSColor) → references/NativeBridges.md
    • Core Graphics/Image (CGColor, CIColor) → references/NativeBridges.md
  6. Storing or persisting colors?

    • Using Codable or Sendablereferences/CKColor.md

Triage-First Playbook

  • "My colors look different after conversion."
    • Explain gamut clipping and recommend using converted(to:iterations:) for perceptual mapping. Refer to references/ColorOperations.md.
  • "How do I support Dark Mode with custom colors?"
    • Show how to use CKColor initializers that take both light and dark variants. Refer to references/CKColor.md.
  • "Is my text readable on this background?"
    • Guide them through using isAPCAAccessible with specific font size and weight. Refer to references/Accessibility.md.
  • "I get a compiler error when using CKColor in SwiftUI."
    • Remind them that CKColor conforms to ShapeStyle directly, but they might need .color property for some modifiers. Refer to references/NativeBridges.md.

Core Patterns Reference

Basic Initialization & Usage

import ColorKit

// Hex initialization
let brand = CKColor(hexString: "#007AFF")

// Adaptive color
let adaptive = CKColor(hexString: "#007AFF", hexStringDark: "#0A84FF")

// Use in SwiftUI
Circle().fill(adaptive)

Advanced Operations

// Perceptual conversion to sRGB
let p3 = CKColor(red: 1.0, green: 0.0, blue: 0.0, colorSpace: .displayP3)
let sRGB = p3.converted(to: .sRGB, iterations: 6)

// Blending
let blended = brand.blended(with: .black, mode: .multiply, opacity: 0.5)

Accessibility Check

let bg = CKColor.white
let isAccessible = brand.isAPCAAccessible(on: bg, size: 16, weight: .regular)

Integration Quick Guide

ColorKit is integrated via Swift Package Manager.

  1. Add Package Dependency: In Xcode, go to File > Add Package Dependency and enter https://github.com/SzpakKamil/ColorKit.git.
  2. Import: import ColorKit in your Swift files.
  3. Deployment Targets: iOS 13.0+, macOS 10.15+, tvOS 13.0+, watchOS 6.0+, visionOS 1.0+ (Swift 5.9+).

For detailed setup, see references/Setup.md.

Reference Files

Load these files as needed for specific topics:

  • ColorKit.md - General overview and key capabilities.
  • Setup.md - Installation and project integration.
  • CKColor.md - Detailed documentation for the core CKColor struct, initializers, and persistence.
  • ColorOperations.md - Conversion, gamut mapping, and basic modifications.
  • ColorModels.md - Using specialized models like OKLAB, HSL, CMYK, etc.
  • NativeBridges.md - Integration with SwiftUI, UIKit, AppKit, and Core Graphics.
  • Accessibility.md - WCAG and APCA contrast calculations and readability checks.
  • Blending.md - Advanced blending modes and transparency handling.
  • _index.md - A comprehensive index for all ColorKit reference documentation.

Best Practices Summary

  1. Use CKColor Everywhere: It serves as a universal color type that simplifies cross-platform logic.
  2. Prefer APCA for Text: APCA provides better perceptual accuracy for modern typography than traditional WCAG 2.1.
  3. Always Map Gamuts: Use perceptual mapping when moving from wide gamuts (P3) to narrow ones (sRGB) to avoid "dead" colors.
  4. Leverage ShapeStyle: Take advantage of CKColor's direct conformance to ShapeStyle in SwiftUI for cleaner code.
  5. Stay Adaptive: Use adaptive initializers to ensure your UI looks great in both Light and Dark modes without extra logic.

Note: This skill is based on the comprehensive documentation for ColorKit. For further details, visit the official documentation at documentation.kamilszpak.com/documentation/colorkit/ or the project website at kamilszpak.com/pl/colorkit.

相关推荐