Code Monkey home page Code Monkey logo

dsfcolorsampler's Introduction

DSFColorSampler

A Swift 5, Objective-C compatible class that mimics the magnifying glass in color panel of macOS.

API Compatible with NSColorSampler (macOS 10.15 Catalina and later)

Overview

Adapted from https://github.com/wentingliu/ScreenPicker for Swift 5 with bug fixes and some minor improvements.

All credit to the original author (Wenting Liu), adapted licensing from WTFPL

Demonstration Video

Why?

Apple released NSColorSampler for macOS 10.15+, which isn't much use to those who still need to support earlier systems.

Features

  • Compatible back to macOS 10.9
  • Simple shared controller with block callbacks
  • Hit Escape to cancel picking
  • Use the mouse scroll wheel to zoom in and zoom out

Usage

Swift Package Manager

Add https://github.com/dagronf/DSFColorSampler to your project.

Direct

Add DSFColorSampler.swift to your project.

API

Simple

Show the color loupe, and call the provided completion block when the user selects a color.

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) {
   //
}];

Dynamic image and mouse colors during selection

Show the color loupe, and provide callback blocks for both mouse movement and selection. For mouse movement, an image snapshot of the mouse area is also provided.

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

Async support

macOS 10.15+ only

This library supports the new async/await model in Swift 5.5 using the sample() method

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

The sample() method is marked as @MainActor so it's guaranteed to be run on the main thread.

Note that if you're using this call, you might better be served using the NSColorSampler class in macOS 10.15+. This feature was made available as a learning exercise as how to convert callback-based async calls to Task based.

Backwards compatibility for NSColorSampler (10.15+)

The selectColor function has been added to DSFColorSampler that calls NSColorSampler().show() on 10.15 or later, and DSFColorSampler().show() on systems prior to 10.15.

Swift

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

Objective-C

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

Releases

3.0.0

  • Removed SPM support for < 10.13 (you can still use this library in earlier macOS by copying DSFColorSampler.swift into your project)

2.1.0

  • Added async task support for macOS 10.15+

2.0.0

  • Added selectColor static method to provide fallback capability for system before macOS 10.15.

1.5.0

  • Fixed color picking on multi-screen setups (including dragging the loupe from one screen to another). Previously could only pick colors from the main screen and additional screens would show a 'blank' color.

1.4.0

  • Added objc demo.

1.3.0

  • Removed unfinshed demo app.

License

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.

dsfcolorsampler's People

Contributors

cool8jay avatar dagronf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

dsfcolorsampler's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.