一个基于 ScreenCaptureKit 的高度可定制 Swift 5.4 颜色采样器

SCColorSampler

一个 Swift 5.4,高度可定制的,基于 ScreenCaptureKit 的取色器。

概述

改编自 https://github.com/dagronf/DSFColorSampler 针对 ScreenCaptureKit,并进行了一些小的改进和更多定制。

需要屏幕共享权限!

为什么?

虽然 Apple 确实提供了 NSColorSampler() 作为本地解决方案,但它不可定制,因此可能无法满足每个人的需求。

此外,随着 macOS 14.0(Sonoma)的发布,Apple 不再支持 CGWindowListCreateImage,该函数以前用于多个自定义取色器包。

特点

  • 高度可定制以满足每个项目的需要
  • 简单的块回调
  • 点击 Escape 取消选择
  • 使用鼠标滚轮 | 捏合手势可放大和缩小

使用

Swift 包管理器

https://github.com/danielcapra/SCColorSampler 添加到您的项目中。

API

let configuration = SCColorSamplerConfiguration()

SCColorSampler.sample(configuration: configuration) { hoveredColor in
    // Do something with the hoveredColor
    // ...
} selectionHandler: { selectedColor in 
    // Check if user cancelled
    guard let selectedColor = selectedColor else {
        return
    }
    // Do something with the selectedColor
    // ...
}

配置自定义示例

放大镜形状

Image showing rounded rectangle loupe shape option Image showing rectangle loupe shape option Image showing circle loupe shape option

let configuration = SCColorSamplerConfiguration()

configuration.loupeShape = .roundedRect // default
// or
configuration.loupeShape = .circle
// or
configuration.loupeShape = .rect

SCColorSampler.sample(configuration: configuration) { ... }

放大镜大小

Image showing small loupe size option Image showing medium loupe size option Image showing large loupe size option