DSFColorSampler 4.2.1

DSFColorSampler 4.2.1

Darren Ford 维护。



  • Darren Ford

DSFColorSampler

一个 Swift 5 和 Objective-C 兼容的类,模仿 macOS 色彩面板中的放大镜。

与 NSColorSampler 兼容的 API(macOS 10.15 Catalina 及以后版本)

概览

基于 https://github.com/wentingliu/ScreenPicker 对 Swift 5 进行了调整,修复了错误并进行了一些小改进。

所有归功于原作者 (Wenting Liu),许可来自 WTFPL

演示视频

为什么?

Apple 为 macOS 10.15+ 发布了 NSColorSampler,但这对于那些仍然需要支持旧系统的人来说作用不大。

功能

  • 支持回滚到 macOS 10.9
  • 简单的共享控制器与阻塞回调
  • 按Esc键取消选择
  • 使用鼠标滚轮进行缩放和缩小

使用方法

Swift包管理器

https://github.com/dagronf/DSFColorSampler 添加到您的项目。

直接方法

DSFColorSampler.swift 添加到您的项目。

接口

简单

显示颜色放大镜,当用户选择颜色时调用提供的完成块。

Swift

DSFColorSampler.show { (selectedColor) in
   if let selectedColor = selectedColor {
      // Do something with selectedColor
   }
   else {
      // User cancelled
   }
}

Objective-C

[DSFColorSampler showWithLocationChange:^(NSImage* snapshot, NSColor* color) {
   //
} selectionHandler:^(NSColor* color) {
   //
}];

选择时动态图像和鼠标颜色

显示颜色放大镜,并为鼠标移动和选择提供回调块。对于鼠标移动,还提供了鼠标区域的照片快照。

DSFColorSampler.show(
   locationChange: { (image, currentColor) in
      // Do something with the image and currentColor at the new location
   },
   selectionHandler: { (selectedColor) in
      // Do something with selectedColor
   }
)

异步支持

仅限macOS 10.15+

这个库支持Swift 5.5中新的async/await模型,并通过sample()方法实现。

Task { [weak self] in
   self?.selectedColor = await DSFColorSampler.sample()
}

sample()方法被标记为@MainActor,因此可以保证在主线程上执行。

请注意,如果您使用此调用,可能会更好地使用macOS 10.15+中的NSColorSampler类。这个功能被作为一个学习练习提供,以展示如何将基于回调的异步调用转换为基于Task的。

NSColorSampler (10.15+)的向下兼容性

在DSFColorSampler中添加了selectColor函数,在macOS 10.15或更高版本上调用NSColorSampler().show(),在第10.15之前的系统上通过DSFColorSampler().show()进行调用。

Swift

DSFColorSampler.selectColor { (selectedColor: NSColor?) in
   // Do something with selectedColor
}

Objective-C

[DSFColorSampler selectColorWithSelectionHandler:^(NSColor* _Nullable selectedColor) {
	// Do something with
} ];

版本发布

3.0.0

  • 移除了对小于10.13的SPM支持(您仍然可以通过将DSFColorSampler.swift复制到您的项目中在更早的macOS中使用此库)

2.1.0

  • 为macOS 10.15+添加了异步任务支持

2.0.0

  • 添加了selectColor静态方法,为macOS 10.15之前的系统提供回退功能。

1.5.0

  • 在多屏配置上固定颜色拾取功能(包括将放大镜从一个屏幕拖到另一个屏幕)。以前只能从主屏幕拾取颜色,额外的屏幕将显示一个“空白”颜色。

1.4.0

  • 添加了objective-c演示。

1.3.0

  • 移除了未完成的演示应用。

许可证

MIT License

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