-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for apple color list / .clr files (#87)
* feat: add support for apple color list / .clr files * style: format with swift-format * refactor: minor changes * refactor: simplify `hexToRGBA` * fix: use `red` not `calibratedRed` parameter for NSColor * refactor: clean up arg handling * ci(release-please): use macos runner, setup swift * fix: import Buffer type * fix: require `COMPILE_APPLE_COLOR_LIST=1` * ci(test): switch to macos & build everything * ci: upload palettes as artifact * Update .github/workflows/test.yml Co-authored-by: uncenter <47499684+uncenter@users.noreply.github.com> --------- Co-authored-by: sgoudham <sgoudham@gmail.com> Co-authored-by: Hammy <58985301+sgoudham@users.noreply.github.com>
- Loading branch information
1 parent
628a04a
commit 88e2795
Showing
9 changed files
with
135 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { CatppuccinColors } from "@catppuccin/palette"; | ||
|
||
export const generateClrJson = ( | ||
_name: string, | ||
palette: CatppuccinColors, | ||
): string => { | ||
const data: Record<string, { hex: string; order: number }> = Object | ||
.fromEntries( | ||
Object.entries(palette).map(([_, { name, hex, order }]) => { | ||
return [name, { hex, order }]; | ||
}), | ||
); | ||
|
||
return JSON.stringify(data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import AppKit | ||
import Foundation | ||
|
||
struct ColorProperties: Decodable { | ||
let hex: String | ||
let order: Int | ||
} | ||
|
||
typealias ColorList = [String: ColorProperties] | ||
|
||
func hexToRGBA(_ color: String) -> (r: CGFloat, g: CGFloat, b: CGFloat) { | ||
var hexColor = String(color.dropFirst()) | ||
|
||
let r = CGFloat(Int(hexColor.prefix(2), radix: 16) ?? 0) / 255 | ||
let g = CGFloat(Int(hexColor.dropFirst(2).prefix(2), radix: 16) ?? 0) / 255 | ||
let b = CGFloat(Int(hexColor.dropFirst(4).prefix(2), radix: 16) ?? 0) / 255 | ||
return (r, g, b) | ||
} | ||
|
||
func convertJSONToCLR(inputFilePath: String, outputFilePath: String) { | ||
let url = URL(fileURLWithPath: inputFilePath) | ||
guard let data = try? Data(contentsOf: url), | ||
let colorList = try? JSONDecoder().decode(ColorList.self, from: data) | ||
else { | ||
print("Failed to read or parse JSON file.") | ||
return | ||
} | ||
|
||
let sortedColors = colorList.sorted { (lhs, rhs) -> Bool in | ||
return lhs.value.order < rhs.value.order | ||
} | ||
|
||
let paletteName = url.deletingPathExtension().lastPathComponent | ||
let nsColorList = NSColorList(name: paletteName) | ||
|
||
for (name, properties) in sortedColors { | ||
let hex = properties.hex | ||
let color = hexToRGBA(hex) | ||
nsColorList.setColor( | ||
NSColor(red: color.r, green: color.g, blue: color.b, alpha: 1), forKey: name) | ||
} | ||
|
||
let clrFilePath = URL(fileURLWithPath: outputFilePath) | ||
do { | ||
try nsColorList.write(to: clrFilePath) | ||
print("Successfully saved palette to \(outputFilePath).") | ||
} catch { | ||
print("Failed to save color palette: \(error)") | ||
} | ||
} | ||
|
||
if CommandLine.argc != 3 { | ||
print("\(CommandLine.arguments[0]): Not enough arguments provided (expected input and output filenames)") | ||
exit(1) | ||
} | ||
|
||
convertJSONToCLR(inputFilePath: CommandLine.arguments[1], outputFilePath: CommandLine.arguments[2]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters