A color palette reader/editor/writer package for iOS, macOS, macCatalyst, tvOS, watchOS and Linux.
Supports the following color palette formats
.ase
).aco
).act
).acb
) (read only).clr
) (macOS only).rgb
).rgba
).gpl
).pal
, .psppalette
).pal
) (read only).sketchpalette
).xml
).jsoncolorpalette
) ColorPaletteCodable internal file format.hex
)
I wanted to be able to read and write .ase
palette files in my Swift app.
This then extended to .aco
Adobe Photoshop Color Swatch files.
Which then expanded to other types :-)
Some features :-
Type | Description |
---|---|
PAL.Palette |
The full representation of a palette |
PAL.Group |
An optionally named collection of colors |
PAL.Color |
An optionally named color |
Type | Description |
---|---|
PAL.Coder.ACB |
Adobe Color Book (.acb) |
PAL.Coder.ACO |
Adobe Photoshop Color Swatch (.aco) |
PAL.Coder.ACT |
Adobe Color Table (.act) |
PAL.Coder.ASE |
Adobe Swatch Exchange (.ase) |
PAL.Coder.CLR |
NSColorList (.clr) (macOS only) |
PAL.Coder.GIMP |
GIMP palette files (.gpl) |
PAL.Coder.HEX |
Hex Color Palette (.hex ) |
PAL.Coder.JSON |
JSON encoded palette (.jsoncolorpalette) |
PAL.Coder.PaintShopPro |
Paint Shop Pro palette (.pal;.psppalette) |
PAL.Coder.RGB |
RGB text files (.rgb) |
PAL.Coder.RGBA |
RGB(A) text files (.rgba) |
PAL.Coder.RIFF |
Microsoft RIFF palette (.pal) |
PAL.Coder.SketchPalette |
Sketch Palette (.sketchpalette) |
PAL.Coder.XMLPalette |
CorelDraw/Adobe Illustrator Palette (.xml) |
do {
let myFileURL = URL(fileURL: ...)
let palette = try PAL.Palette.Decode(from: myFileURL)
// do something with 'palette'
}
catch {
// Do something with 'error'
}
do {
// 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)
}
catch {
// Do something with 'error'
}
do {
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)
}
catch {
// Do something with 'error'
}
This package also includes a Quicklook Plugin for palette files. macOS 12 has changed the was quicklook plugins work, by creating an .appex extension (which is the quicklook plugin) embedded within an application.
In the Quicklook
subfolder you'll find an xcodeproj
which you can use to build the application Palette Viewer
which contains the QuickLook plugin.
For the plugin to register, you need to run the application. After the first run the QuickLook plugin will be registered.
Palette Viewer allows you to view the contents of
You can drag colors out of the preview window into applications that support dropping of NSColor
instances.
You can also save the palette to a new format (eg. saving a gimp .gpl
format to an Adobe .aco
format)
File Type | Named Colors? |
Named palette? |
Color Groups? |
ColorType Support? |
Supports Colorspaces? |
|
---|---|---|---|---|---|---|
PAL.Coder.ACB |
Binary | ✅ | ❌ | ❌ | ❌ | ✅ |
PAL.Coder.ACO |
Binary | ✅ | ❌ | ❌ | ❌ | ✅ |
PAL.Coder.ACT |
Binary | ❌ | ❌ | ❌ | ❌ | RGB only |
PAL.Coder.ASE |
Binary | ✅ | ❌ | ✅ | ✅ | ✅ |
PAL.Coder.CLR |
Binary (macOS only) |
✅ | ❌ | ❌ | ❌ | ✅ |
PAL.Coder.GIMP |
Text | ✅ | ✅ | ❌ | ❌ | RGB only |
PAL.Coder.HEX |
Text | ❌ | ❌ | ❌ | ❌ | RGB only |
PAL.Coder.JSON |
JSON Text | ✅ | ✅ | ✅ | ✅ | ✅ |
PAL.Coder.PaintShopPro |
Text | ❌ | ❌ | ❌ | ❌ | RGB only |
PAL.Coder.RGB/A |
Text | ✅ | ❌ | ❌ | ❌ | RGB only |
PAL.Coder.RIFF |
Binary | ❌ | ❌ | ❌ | ❌ | RGB only |
PAL.Coder.SketchPalette |
XML | ❌ | ❌ | ❌ | ❌ | RGB only |
PAL.Coder.XMLPalette |
XML | ✅ | ✅ | ✅ | ❌ | ✅ |
(A ColorType represents the type of color (global/spot/normal))
The library additional defines PAL.Gradient
which defines a collection of colors with positions
that can be used when defining gradient types.
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"))
]
)
let coder = PAL.Gradient.Coder.JSON()
// Encode the gradient using the JSON encoder
let data = try coder.encode(gradient)
// Decode a gradient from data
let decoded = try PAL.Gradient.Decode(
from: data,
fileExtension: PAL.Gradient.Coder.JSON.fileExtension
)
The gradient coder includes basic importers/exporters.
Type | Description |
---|---|
PAL.Gradient.Coder.JSON |
Built-in JSON format (.jsongradient) |
PAL.Gradient.Coder.GGR |
GIMP gradient file (.ggr) |
.ggr
support doesn't respect segment blending functions other than linear (always imported as linear).ggr
support doesn't allow for segment coloring functions other than rgb (throws an error)For some nice gradient files, cptcity has all of them :-)
cptcity also has a nice converter for gradients to ggr
See: Testing Swift packages on Linux using Docker
docker run --rm --privileged --interactive --tty --volume "$(pwd):/src" --workdir "/src" swift:latest
Now, from within the docker container, run
swift build
swift test
Note that the /src directory in the Linux container is a direct mirror of the current directory on the host OS, not a copy. If you delete a file in /src in the Linux container, that file will be gone on the host OS, too.
The .ase
file format is not formally defined, however there are a number of deconstructions available on the web.
I used the breakdown of the format defined here.
The .aco
file format is defined here.
The .act
file format is defined here.
The .acb
format discussed and deined here
The CorelDraw/Adobe Illustrator .xml
file format is (somewhat) defined here
MIT. Use it for anything you want, just attribute my work if you do. Let me know if you do use it somewhere, I'd love to hear about it!
MIT License
Copyright (c) 2023 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.
link |
Stars: 18 |
Last commit: 4 weeks ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics