ColorPaletteCodable

一个适用于 iOS、macOS、macCatalyst、tvOS、watchOS 和 Linux 的调色板读取/编辑/写入包。

tag Swift License MIT SPM Build

macOS iOS tvOS watchOS macCatalyst Linux

支持以下格式:-

支持的调色板格式

支持的渐变格式

为什么?

我想在我的 Swift 应用程序中读取和写入 Adobe .ase 调色板文件。 然后它扩展到 .aco Adobe Photoshop Color Swatch 文件。 然后扩展到其他类型 :-)

一些功能:-

在线 API 文档

调色板 API

类型 描述
PAL.Palette 调色板的完整表示
PAL.Group 颜色的可选命名集合
PAL.Color 可选命名的颜色

可用的编码器

类型 描述
PAL.Coder.ACB Adobe Color Book (.acb)
PAL.Coder.ACO Adobe Photoshop Color Swatch (.aco)
PAL.Coder.ACT Adobe Color Table (.act)
PAL.Coder.AndroidColorsXML Android color.xml 资源 (.xml)
PAL.Coder.ASE Adobe Swatch Exchange (.ase)
PAL.Coder.BasicXML 基本 XML 结构 (.xml)
PAL.Coder.CLR NSColorList (.clr) (仅限 macOS)
PAL.Coder.CorelPainter CorelPainter 色板 (.txt)
PAL.Coder.CorelXMLPalette CorelDraw/Adobe Illustrator 调色板 (.xml)
PAL.Coder.CPL Corel Paint (.cpl)
PAL.Coder.CSV CSV (.csv)
PAL.Coder.DCP DCP 二进制调色板格式 (.dcp)
PAL.Coder.GIMP GIMP 调色板文件 (.gpl)
PAL.Coder.HEX 十六进制颜色调色板 (.hex)
PAL.Coder.Image 图像文件 (.png, .jpg, .gif)
PAL.Coder.JSON JSON 编码的调色板 (.jsoncolorpalette)
PAL.Coder.OpenOfficePaletteCoder OpenOffice 调色板 (.soc)
PAL.Coder.PaintNET Paint.NET 调色板 (.txt)
PAL.Coder.PaintShopPro Paint Shop Pro 调色板 (.pal;.psppalette)
PAL.Coder.RGBA RGB(A) 文本文件 (.rgba)
PAL.Coder.RGB RGB 文本文件 (.rgb)
PAL.Coder.RIFF Microsoft RIFF 调色板 (.pal)
PAL.Coder.SketchPalette Sketch 调色板 (.sketchpalette)
PAL.Coder.SVG SVG 图像文件 (.svg)

每个编码器定义 .encode.decode。 并非所有编码器都支持编码和解码。

使用示例

解码调色板文件

do {
   let myFileURL = URL(fileURL: ...)
   
   // Try to decode the palette based on its file extension
   let palette = try PAL.Palette.Decode(from: myFileURL)
   
   // do something with 'palette'
}
catch {
   // Do something with 'error'
}

构建调色板并生成 ASE 二进制表示

// Build a palette
var palette = PAL.Palette()
let c1 = try PAL.Color.rgb(name: "red",   1, 0, 0)
let c2 = try PAL.Color.rgb(name: "green", 0, 1, 0)
let c3 = try PAL.Color.rgb(name: "blue",  0, 0, 1)
palette.colors.append(contentsOf: [c1, c2, c3])

// Generate a simple image from the colors
let image = try PAL.Image.Image(colors: [c1, c2, c3], size: CGSize(width: 100, height: 25))

// Create an ASE coder
let coder = PAL.Coder.ASE()

// Get the .ase format data
let rawData = try coder.encode(palette)
   
// Do something with 'rawData' (like write to a file for example)

读取 ACO 文件,写入 ASE 文件

let acoFileURL = URL(fileURL: ...)
let coder = PAL.Coder.ACO()
var palette = try coder.decode(from: acoFileURL)
   
// do something with 'palette'
   
// re-encode the palette to an ASE format
let encoder = PAL.Coder.ASE()
let rawData = try encoder.encode(palette) 

调色板格式编码/解码限制

文件类型 解码? 编码? 命名
颜色?
命名
调色板?
颜色
分组?
颜色类型
支持?
支持
颜色空间?
PAL.Coder.ACB 二进制
PAL.Coder.ACO 二进制
PAL.Coder.ACT 二进制 仅 RGB
PAL.Coder.AndroidColorsXML XML 仅 RGB
PAL.Coder.ASE 二进制
PAL.Coder.BasicXML XML 仅 RGB
PAL.Coder.CLR 二进制
(仅限 macOS)
PAL.Coder.CorelPainter 文本 仅 RGB
PAL.Coder.CPL 二进制
PAL.Coder.CSV 文本 仅 RGB
PAL.Coder.GIMP 文本 仅 RGB
PAL.Coder.HEX 文本 仅 RGB
PAL.Coder.Image 二进制
PAL.Coder.JSON JSON 文本
PAL.Coder.OpenOfficePalette XML
PAL.Coder.PaintNET 文本 仅 RGB
PAL.Coder.PaintShopPro 文本 仅 RGB
PAL.Coder.RGB/A 文本 仅 RGB
PAL.Coder.RIFF 二进制 仅 RGB
PAL.Coder.SketchPalette XML 仅 RGB
PAL.Coder.SVG SVG 文本 仅 RGB
PAL.Coder.XMLPalette XML

(颜色类型代表颜色的类型(全局/专色/正常))

渐变

该库定义了 PAL.Gradients,它定义了一个颜色集合,其中包含可用于渐变的位置。 某些渐变类型(例如 .grd)支持同一文件中的多个渐变。

渐变 API

类型 描述
PAL.Gradients 渐变的集合
PAL.Gradient 一个渐变
PAL.Stop 渐变中的颜色停止点
PAL.TransparencyStop 渐变中的透明度停止点

可用的编码器

类型 描述 解码? 编码?
PAL.Gradients.Coder.CPT CPT 渐变文件 (.cpt)
PAL.Gradients.Coder.JSON 内置 JSON 格式 (.jsongradient)
PAL.Gradients.Coder.GGR GIMP 渐变文件 (.ggr)
PAL.Gradients.Coder.GPF GNUPlot 颜色调色板文件 (.gpf)
PAL.Gradients.Coder.GRD 基本 Adobe Photoshop 渐变文件 (.grd)
PAL.Gradients.Coder.PSP 基本 Paint Shop Pro 渐变文件 (.pspgradient)
PAL.Gradients.Coder.SVG SVG 文件 (.svg)

一些不错的渐变文件

cptcity 还有一个 不错的转换器 用于将渐变转换为 ggr

示例

创建渐变

let gradient = PAL.Gradient(
   colorPositions: [
      (0.0, try PAL.Color(rgbHexString: "#FFFFFF")),
      (0.5, try PAL.Color(rgbHexString: "#444444")),
      (1.0, try PAL.Color(rgbHexString: "#000000"))
   ]
)

// Create a gradients container
let gradients = PAL.Gradients(gradients: [gradient])

// Create the appropriate coder
let coder = PAL.Gradients.Coder.GGR()

// Encode the gradient using the GIMP gradient encoder
let data = try coder.encode(gradients)

// Decode a gradient from data
let decoded = try PAL.Gradients.Decode(
   from: data,
   fileExtension: PAL.Gradients.Coder.GGR.fileExtension
)

加载渐变

// Load a gradient from a file, inferring the type from the file's extension
let gradient1 = try PAL.Gradients.Decode(from: fileURL)

// Load a specific gradient format from a file
let coder = PAL.Gradients.Coder.GRD()
let gradient2 = try coder.decode(from: i)

调色板查看器

调色板查看器允许您查看所有支持的调色板和渐变文件的内容

您可以将颜色从预览窗口拖到支持拖放 NSColor 实例的应用程序中。

您还可以将调色板保存为新格式(例如,将 gimp .gpl 格式保存为 Adobe .aco 格式)

QuickLook 支持

此软件包还包括调色板和渐变文件的 Quicklook 插件。

Quicklook 子文件夹中,您会找到一个 xcodeproj,您可以使用它来构建包含 QuickLook 插件的应用程序 Palette Viewer

要注册插件,您需要运行该应用程序。 首次运行后,将注册 QuickLook 插件。

Linux 支持

使用 Mac 构建/测试 Linux 支持

请参阅:使用 Docker 在 Linux 上测试 Swift 包

  1. 在您的 Mac 上安装 Docker Desktop for Mac
  2. 确保 Docker 正在运行(否则,下一个命令将失败并显示奇怪或无错误消息)
  3. 在您要镜像到 Linux 中的目录中运行以下命令
docker run --rm --privileged --interactive --tty --volume "$(pwd):/src" --workdir "/src" swift:latest

现在,从 Docker 容器内部运行

swift build
swift test

请注意,Linux 容器中的 /src 目录是主机操作系统上当前目录的直接镜像,而不是副本。 如果您删除 Linux 容器中 /src 中的文件,该文件也会在主机操作系统上消失。

格式规范

.ase 文件格式没有正式定义,但是网络上有许多解构。 我使用了此处定义的格式分解。

.aco 文件格式定义此处

.act 文件格式定义此处

.acb 格式在此处讨论和定义

CorelDraw/Adobe Illustrator .xml 文件格式(有些)定义在此处

GRD 参考

温度参考

许可证

MIT License

Copyright (c) 2025 Darren Ford

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

其他许可证

OkLab

https://bottosson.github.io/posts/oklab/#oklab-implementations

https://bottosson.github.io/misc/License.txt

Copyright (c) 2020 Björn Ottosson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.