DSFColorPicker

一个用于 macOS 的 Swift 库,可显示可自定义的颜色选择器。

特性

安装

https://github.com/dagronf/DSFColorPicker 添加到您的项目中

配置

SwiftUI

您可以将 DSFColorPickerUI 添加到您的 SwiftUI 视图,以在您的 SwiftUI 内容中显示 DSFColorPicker。

参数

参数 描述
name 选择器的名称(例如“笔画颜色”)
theme 要使用的主题,或使用 nil 使用默认颜色
cellSize 每个颜色单元格的大小
spacing 每个颜色单元格之间的间距
displaySettings 要呈现的颜色选择器的组件
selectedColor 与所选颜色的绑定

示例

struct MyContent: View {
   @State var selectedColor: Color = .red
   var body: some View {
      ...
      DSFColorPickerUI(
         named: "My color picker",
         theme: miniColorTheme,
         selectedColor: $selectedColor
      )
   )
}

let miniColorTheme = DSFColorPickerTheme(name: "Basic Colors", argbValueGrid: [
	[0xF5402C, 0xEB1360, 0x9C1BB1, 0x6634B8, 0x3D4DB7],
	[0x47AE4A, 0x009687, 0x01BBD5, 0x00A6F6, 0x0C93F5],
	[0x89C43F, 0xCCDD1E, 0xFFEC17, 0xFEC001, 0xFF9800],
	[0x000000, 0x5E7C8B, 0x9D9D9D, 0x7A5447, 0xFF5506],
])

Interface Builder

您可以在 Interface Builder 中完全配置您的控件。拖入一个自定义视图,将自定义类型更改为 DSFColorView (或重载),并使用自定义类型中的“命名主题”设置选择您的显示主题

drawing

全尺寸

在代码中

可以使用颜色二维数组进行配置,例如

一个 5x4 的网格

drawing

全尺寸

	let theme = DSFColorPickerTheme(name: "My Colors", argbValueGrid: [
		[0xF5402C, 0xEB1360, 0x9C1BB1, 0x6634B8, 0x3D4DB7],
		[0x47AE4A, 0x009687, 0x01BBD5, 0x00A6F6, 0x0C93F5],
		[0x89C43F, 0xCCDD1E, 0xFFEC17, 0xFEC001, 0xFF9800],
		[0x000000, 0x5E7C8B, 0x9D9D9D, 0x7A5447, 0xFF5506]]
	)
	self.colorView.selectedTheme = theme

一个没有最近颜色的单行

drawing

全尺寸

	let theme = DSFColorPickerTheme(name: "Simple Line", argbValueGrid: [
		[0xE0BA4240, 0xE0F0C976, 0xE096A873, 0xE089C9B8, 0xE0AFE6E4, 0xE07AA3C0, 0xE08B719F, 0xE0DB849A]]
	)
	self.showRecents = false
	self.colorView.selectedTheme = theme

最近使用颜色的数量(如果显示)与颜色网格中的列数相同

颜色采样支持

从 macOS 10.15 开始,您的应用需要屏幕录制权限才能使用颜色采样器(吸管)。

用户第一次选择吸管时,macOS 会显示一个对话框,要求提供权限。

您可以在系统偏好设置的安全性与隐私窗格中查看应用程序的屏幕录制权限。

系统偏好设置 > 安全性与隐私 > 隐私 > 屏幕录制

主题支持

提供一组主题,并配置控件以显示主题选择器。

拖放支持

黑暗模式支持

自动动态调整为亮色和暗色模式

高对比度支持

动态调整为高对比度显示模式

drawing

全尺寸

辅助功能支持(初步)

许可证

MIT License

Copyright (c) 2024 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.