Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4175bae13 | ||
|
|
a63fd85bc5 | ||
|
|
ff1063c8e2 | ||
|
|
1305e4366a | ||
|
|
4cad88d0cd | ||
|
|
58f3ef9ecd | ||
|
|
8b907ac7ed | ||
|
|
e5ce05dc89 | ||
|
|
42f88bcf53 | ||
|
|
4ddfe5a0f3 | ||
|
|
0e60e12364 | ||
|
|
06e20d5594 |
@@ -7,7 +7,9 @@
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
15349DB820BAE3F70071CAA1 /* NSBezierPath.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15349DB720BAE3F70071CAA1 /* NSBezierPath.swift */; };
|
||||
15492103207B53AE00E45BBC /* Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15492102207B53AE00E45BBC /* Util.swift */; };
|
||||
15493F6720BE6E0F007B2BA5 /* CGImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15493F6620BE6E0F007B2BA5 /* CGImage.swift */; };
|
||||
155D2C03204F61220087478B /* Credits.rtfd in Resources */ = {isa = PBXBuildFile; fileRef = 155D2C02204F61220087478B /* Credits.rtfd */; };
|
||||
159F304E2079A111000BD387 /* AppDelegateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 159F304D2079A111000BD387 /* AppDelegateTests.swift */; };
|
||||
159F30522079A147000BD387 /* PPStateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 159F30512079A147000BD387 /* PPStateTests.swift */; };
|
||||
@@ -65,7 +67,9 @@
|
||||
/* End PBXCopyFilesBuildPhase section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
15349DB720BAE3F70071CAA1 /* NSBezierPath.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSBezierPath.swift; sourceTree = "<group>"; };
|
||||
15492102207B53AE00E45BBC /* Util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Util.swift; sourceTree = "<group>"; };
|
||||
15493F6620BE6E0F007B2BA5 /* CGImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CGImage.swift; sourceTree = "<group>"; };
|
||||
155D2C02204F61220087478B /* Credits.rtfd */ = {isa = PBXFileReference; lastKnownFileType = wrapper.rtfd; path = Credits.rtfd; sourceTree = "<group>"; };
|
||||
159F304D2079A111000BD387 /* AppDelegateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegateTests.swift; sourceTree = "<group>"; };
|
||||
159F30512079A147000BD387 /* PPStateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PPStateTests.swift; sourceTree = "<group>"; };
|
||||
@@ -171,6 +175,7 @@
|
||||
B3B4C2BF1E25894B009F8E4E /* PixelPicker */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
15493F6620BE6E0F007B2BA5 /* CGImage.swift */,
|
||||
BA74C13420A6F1BE00306B27 /* ShowAndHideCursor.h */,
|
||||
BA74C13220A6F16C00306B27 /* ShowAndHideCursor.m */,
|
||||
BA71817320AE2DCC00619700 /* PPMenuShortcutView.h */,
|
||||
@@ -180,6 +185,7 @@
|
||||
BA74C14D20ADA33E00306B27 /* PPColor.swift */,
|
||||
15FC8CC7204E2E2C000B5E1E /* PPState.swift */,
|
||||
159ED10B207EB2EF005489ED /* OverlayPanel */,
|
||||
15349DB720BAE3F70071CAA1 /* NSBezierPath.swift */,
|
||||
BA74C14120AA409D00306B27 /* NSColor.swift */,
|
||||
15492102207B53AE00E45BBC /* Util.swift */,
|
||||
B3B4C2C21E25894B009F8E4E /* Assets.xcassets */,
|
||||
@@ -333,6 +339,7 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
BA74C13320A6F16C00306B27 /* ShowAndHideCursor.m in Sources */,
|
||||
15349DB820BAE3F70071CAA1 /* NSBezierPath.swift in Sources */,
|
||||
BA71817220AE2DB400619700 /* PPMenuShortcutView.m in Sources */,
|
||||
B3B4C2C11E25894B009F8E4E /* AppDelegate.swift in Sources */,
|
||||
BA22232920B04D660001069C /* AppDelegate+Menu.swift in Sources */,
|
||||
@@ -342,6 +349,7 @@
|
||||
BA74C14020AA390400306B27 /* PPOverlayPreview.swift in Sources */,
|
||||
BA74C14420AA609500306B27 /* PPOverlayWrapper.swift in Sources */,
|
||||
BA74C0F820A692D100306B27 /* PPOverlayController.swift in Sources */,
|
||||
15493F6720BE6E0F007B2BA5 /* CGImage.swift in Sources */,
|
||||
BA74C14220AA409D00306B27 /* NSColor.swift in Sources */,
|
||||
15492103207B53AE00E45BBC /* Util.swift in Sources */,
|
||||
);
|
||||
|
||||
@@ -10,8 +10,18 @@
|
||||
|
||||
import LaunchAtLogin
|
||||
|
||||
// The modifiers available to use to toggle "concentrationMode".
|
||||
let concentrationModifiers: [(String, NSEvent.ModifierFlags)] = [
|
||||
// The settings available for displaying a grid in the picker's preview.
|
||||
enum GridSetting {
|
||||
case never, always, inFocusMode
|
||||
static let withNames: [(String, GridSetting)] = [
|
||||
("Only in Focus Mode", .inFocusMode),
|
||||
("Always", .always),
|
||||
("Never", .never)
|
||||
]
|
||||
}
|
||||
|
||||
// The modifiers available to use to toggle "focusMode".
|
||||
let focusModifiers: [(String, NSEvent.ModifierFlags)] = [
|
||||
("fn Function", .function),
|
||||
("⌘ Command", .command),
|
||||
("⌃ Control", .control),
|
||||
@@ -58,8 +68,9 @@ extension AppDelegate: NSMenuDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: look into only updating the menu rather than rebuilding it each time.
|
||||
// Might not be worth it - it doesn't seem expensive to build it every time it's opened...
|
||||
// This rebuilds the context menu from scratch. For the sake of simplicity, we re-create the
|
||||
// menu from scratch each time. It's not an expensive operation, and is only called when the
|
||||
// user opens the menu.
|
||||
func rebuildContextMenu() {
|
||||
contextMenu.removeAllItems()
|
||||
|
||||
@@ -69,10 +80,11 @@ extension AppDelegate: NSMenuDelegate {
|
||||
buildRecentPicks()
|
||||
|
||||
contextMenu.addItem(.separator())
|
||||
buildShowGridMenu()
|
||||
buildColorSpaceItem()
|
||||
buildColorFormatsMenu()
|
||||
buildMagnificationItem()
|
||||
buildConcentrationMenu()
|
||||
buildMagnificationMenu()
|
||||
buildFocusModeModifierMenu()
|
||||
buildFloatPrecisionSlider()
|
||||
buildShortcutMenuItem()
|
||||
buildLaunchAtLoginItem()
|
||||
@@ -82,6 +94,25 @@ extension AppDelegate: NSMenuDelegate {
|
||||
contextMenu.addItem(withTitle: "Quit \(APP_NAME)", action: #selector(quitApplication), keyEquivalent: "")
|
||||
}
|
||||
|
||||
// Choose whether to always draw a grid, never draw one, or only draw one when in focus mode.
|
||||
private func buildShowGridMenu() {
|
||||
let submenu = NSMenu()
|
||||
for (title, setting) in GridSetting.withNames {
|
||||
let item = submenu.addItem(withTitle: title, action: #selector(selectGridSetting(_:)), keyEquivalent: "")
|
||||
item.representedObject = setting
|
||||
item.state = PPState.shared.gridSetting == setting ? .on : .off
|
||||
}
|
||||
|
||||
let item = contextMenu.addItem(withTitle: "Show Grid", action: nil, keyEquivalent: "")
|
||||
item.submenu = submenu
|
||||
}
|
||||
|
||||
@objc private func selectGridSetting(_ sender: NSMenuItem) {
|
||||
if let setting = sender.representedObject as? GridSetting {
|
||||
PPState.shared.gridSetting = setting
|
||||
}
|
||||
}
|
||||
|
||||
// A menu that allows choosing what color space the picker will use.
|
||||
private func buildColorSpaceItem() {
|
||||
let submenu = NSMenu()
|
||||
@@ -106,7 +137,7 @@ extension AppDelegate: NSMenuDelegate {
|
||||
}
|
||||
|
||||
// A menu which allows the magnification level of the picker to be adjusted.
|
||||
private func buildMagnificationItem() {
|
||||
private func buildMagnificationMenu() {
|
||||
let submenu = NSMenu()
|
||||
for i in stride(from: 4, through: 24, by: 2) {
|
||||
let item = submenu.addItem(withTitle: "\(i)x", action: #selector(selectMagnification(_:)), keyEquivalent: "")
|
||||
@@ -225,23 +256,23 @@ extension AppDelegate: NSMenuDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
// Builds and adds the concentration modifier submenu.
|
||||
private func buildConcentrationMenu() {
|
||||
// Builds and adds the focus modifier submenu.
|
||||
private func buildFocusModeModifierMenu() {
|
||||
let submenu = NSMenu()
|
||||
for (name, modifier) in concentrationModifiers {
|
||||
for (name, modifier) in focusModifiers {
|
||||
let modifierItem = submenu.addItem(withTitle: name, action: #selector(selectModifier(_:)), keyEquivalent: "")
|
||||
modifierItem.representedObject = modifier
|
||||
if PPState.shared.concentrationModeModifier == modifier { modifierItem.state = .on }
|
||||
if PPState.shared.focusModeModifier == modifier { modifierItem.state = .on }
|
||||
}
|
||||
|
||||
let item = contextMenu.addItem(withTitle: "Concentration Modifier", action: nil, keyEquivalent: "")
|
||||
let item = contextMenu.addItem(withTitle: "Focus Mode Modifier", action: nil, keyEquivalent: "")
|
||||
item.submenu = submenu
|
||||
}
|
||||
|
||||
// Set the chosen modifier to toggle "concentrationMode".
|
||||
// Set the chosen modifier to toggle "focusMode".
|
||||
@objc private func selectModifier(_ sender: NSMenuItem) {
|
||||
if let modifier = sender.representedObject as? NSEvent.ModifierFlags {
|
||||
PPState.shared.concentrationModeModifier = modifier
|
||||
PPState.shared.focusModeModifier = modifier
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// CGImage.swift
|
||||
// PixelPicker
|
||||
//
|
||||
|
||||
import CleanroomLogger
|
||||
|
||||
extension CGImage {
|
||||
// In order to get the color at a given pixel from a CGImage, we need to convert the CGImage's
|
||||
// data to a bitmap and draw it. We do so via a CGContext, and then manually extract the data at
|
||||
// the desired pixel.
|
||||
func colorAt(x: Int, y: Int) -> NSColor {
|
||||
assert(0 <= x && x < self.width)
|
||||
assert(0 <= y && y < self.height)
|
||||
|
||||
let bitmapBytesPerRow = width * 4
|
||||
let bitmapByteCount = bitmapBytesPerRow * Int(self.height)
|
||||
|
||||
// Allocate memory for image data. This is the destination in memory where any drawing to
|
||||
// the bitmap context will be rendered.
|
||||
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue)
|
||||
let bitmapData = malloc(bitmapByteCount)
|
||||
|
||||
// Since we manually allocate memeory for the data, we must ensure that the same memory is
|
||||
// freed after we've used it.
|
||||
defer { free(bitmapData) }
|
||||
|
||||
// Create the bitmap context.
|
||||
let context = CGContext(
|
||||
data: bitmapData,
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
bitsPerComponent: 8,
|
||||
bytesPerRow: bitmapBytesPerRow,
|
||||
space: CGColorSpaceCreateDeviceRGB(),
|
||||
bitmapInfo: bitmapInfo.rawValue
|
||||
)
|
||||
|
||||
// Extract the pixel data from the right offset.
|
||||
if context != nil {
|
||||
// First, we draw the image data onto the context we created.
|
||||
let rect = CGRect(x: 0, y: 0, width: self.width, height: self.height)
|
||||
context!.draw(self, in: rect)
|
||||
|
||||
// Then we extract the data at the right spot.
|
||||
let data = context!.data!
|
||||
let offset = 4 * (y * width + x)
|
||||
|
||||
let a = CGFloat(data.load(fromByteOffset: offset, as: UInt8.self)) / 255.0
|
||||
let r = CGFloat(data.load(fromByteOffset: offset + 1, as: UInt8.self)) / 255.0
|
||||
let g = CGFloat(data.load(fromByteOffset: offset + 2, as: UInt8.self)) / 255.0
|
||||
let b = CGFloat(data.load(fromByteOffset: offset + 3, as: UInt8.self)) / 255.0
|
||||
return NSColor(red: r, green: g, blue: b, alpha: a)
|
||||
}
|
||||
|
||||
// Creating the context failed, so return a default color instead.
|
||||
// Hopefully, this should never happen.
|
||||
Log.error?.message("Failed to create CGContext!")
|
||||
return NSColor.black
|
||||
}
|
||||
}
|
||||
@@ -17,9 +17,9 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>APPL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.1.0</string>
|
||||
<string>1.2.1</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>4</string>
|
||||
<string>6</string>
|
||||
<key>LSApplicationCategoryType</key>
|
||||
<string>public.app-category.utilities</string>
|
||||
<key>LSMinimumSystemVersion</key>
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// NSBezierPath.swift
|
||||
// PixelPicker
|
||||
//
|
||||
|
||||
// Extend NSBezierPath to allow easy conversion to a CGPath.
|
||||
// https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Paths/Paths.html#//apple_ref/doc/uid/TP40003290-CH206-SW2
|
||||
extension NSBezierPath {
|
||||
var cgPath: CGPath {
|
||||
let path = CGMutablePath()
|
||||
var points = [CGPoint](repeating: .zero, count: 3)
|
||||
for i in 0 ..< self.elementCount {
|
||||
switch self.element(at: i, associatedPoints: &points) {
|
||||
case .moveToBezierPathElement: path.move(to: points[0])
|
||||
case .lineToBezierPathElement: path.addLine(to: points[0])
|
||||
case .curveToBezierPathElement: path.addCurve(to: points[2], control1: points[0], control2: points[1])
|
||||
case .closePathBezierPathElement: path.closeSubpath()
|
||||
}
|
||||
}
|
||||
return path
|
||||
}
|
||||
}
|
||||
@@ -21,14 +21,14 @@ class PPOverlayController: NSWindowController {
|
||||
|
||||
// This mode increases the picker's size, increases the magnification and also slows down move
|
||||
// events to make it easier to pick the right pixel. We dissociate the mouse (input) from the
|
||||
// mouse cursor while the concentrationMode is active. This is so we can slow it down.
|
||||
var concentrationMode: Bool = false {
|
||||
// mouse cursor while the focusMode is active. This is so we can slow it down.
|
||||
var focusMode: Bool = false {
|
||||
didSet {
|
||||
if isEnabled {
|
||||
panelSize = concentrationMode ? PPOverlayController.panelSizeLarge : PPOverlayController.panelSizeNormal
|
||||
panelSize = focusMode ? PPOverlayController.panelSizeLarge : PPOverlayController.panelSizeNormal
|
||||
overlayPanel.activate(withSize: panelSize, infoPanel: infoPanel)
|
||||
wrapper.layer?.cornerRadius = PPState.shared.paschaModeEnabled ? 0 : panelSize / 2
|
||||
CGAssociateMouseAndMouseCursorPosition(boolean_t(truncating: concentrationMode ? 0 : 1))
|
||||
CGAssociateMouseAndMouseCursorPosition(boolean_t(truncating: focusMode ? 0 : 1))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,7 @@ class PPOverlayController: NSWindowController {
|
||||
startMonitoringEvents()
|
||||
} else {
|
||||
stopMonitoringEvents()
|
||||
concentrationMode = false
|
||||
focusMode = false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,24 +106,24 @@ class PPOverlayController: NSWindowController {
|
||||
}
|
||||
}
|
||||
|
||||
// Enable "concentrationMode" when correct modifier flag is changed.
|
||||
// Enable "focusMode" when correct modifier flag is changed.
|
||||
override func flagsChanged(with event: NSEvent) {
|
||||
if isEnabled {
|
||||
concentrationMode = event.modifierFlags.contains(PPState.shared.concentrationModeModifier)
|
||||
focusMode = event.modifierFlags.contains(PPState.shared.focusModeModifier)
|
||||
updatePreview(aroundPoint: lastMouseLocation)
|
||||
}
|
||||
}
|
||||
|
||||
// Move the picker with the mouse, and if "concentrationMode" is active then
|
||||
// Move the picker with the mouse, and if "focusMode" is active then
|
||||
// slow it down to make it easier to pick the correct pixel.
|
||||
override func mouseMoved(with event: NSEvent) {
|
||||
if isEnabled {
|
||||
let currentMouseLocation = NSEvent.mouseLocation
|
||||
var nextMouseLocation = currentMouseLocation
|
||||
|
||||
// Slow down tracking speed when concentration mode is active.
|
||||
if concentrationMode {
|
||||
let speed: CGFloat = concentrationMode ? 0.1 : 0.5
|
||||
// Slow down tracking speed when focusMode is active.
|
||||
if focusMode {
|
||||
let speed: CGFloat = focusMode ? 0.1 : 0.5
|
||||
let x = lastMouseLocation.x + (event.deltaX * speed)
|
||||
let y = lastMouseLocation.y - (event.deltaY * speed)
|
||||
|
||||
@@ -236,6 +236,10 @@ class PPOverlayController: NSWindowController {
|
||||
// Ensure the overlayPanel is always the key window.
|
||||
if !overlayPanel.isKeyWindow {
|
||||
overlayPanel.activate(withSize: panelSize, infoPanel: infoPanel)
|
||||
// Reset the cursor state to ensure it's hidden (the foremost application changes then
|
||||
// WindowServer resets our "SetsCursorInBackground" magic).
|
||||
ShowCursor()
|
||||
HideCursor()
|
||||
}
|
||||
|
||||
// Center the mouse in the picker window.
|
||||
@@ -245,7 +249,7 @@ class PPOverlayController: NSWindowController {
|
||||
|
||||
// Calculate a zoomed rect which will crop the screenshot we took.
|
||||
let magnification = CGFloat(PPState.shared.magnificationLevel)
|
||||
let zoomReciprocal: CGFloat = 1.0 / (concentrationMode ? magnification * 2.5 : magnification)
|
||||
let zoomReciprocal: CGFloat = 1.0 / (focusMode ? magnification * 2.5 : magnification)
|
||||
let currentSize = CGFloat(screenShot.width) + 1
|
||||
let origin = floor(currentSize * ((1 - zoomReciprocal) / 2))
|
||||
let x = origin + (isHalf(normalisedPoint.x) ? 1 : 0)
|
||||
@@ -256,43 +260,49 @@ class PPOverlayController: NSWindowController {
|
||||
let croppedRect = CGRect(x: x, y: y, width: zoomedSize, height: zoomedSize)
|
||||
let zoomedImage: CGImage = screenShot.cropping(to: croppedRect)!
|
||||
let pixelSize = panelSize / zoomedSize
|
||||
let middlePosition = zoomedSize / 2
|
||||
|
||||
// Convert the preview to the correct color space, and update the overlay.
|
||||
|
||||
// User has chosen a custom color space.
|
||||
if let colorSpace = getChosenColorSpace(point) {
|
||||
if colorSpace == zoomedImage.colorSpace {
|
||||
return updatePreview(zoomedImage, pixelSize, middlePosition)
|
||||
return updatePreview(zoomedImage, pixelSize, zoomedSize)
|
||||
} else if let image = zoomedImage.copy(colorSpace: colorSpace) {
|
||||
return updatePreview(image, pixelSize, middlePosition)
|
||||
return updatePreview(image, pixelSize, zoomedSize)
|
||||
}
|
||||
}
|
||||
|
||||
// No custom color space chosen, so use the screen's one.
|
||||
if let colorSpace = getScreenColorSpace(point) {
|
||||
if colorSpace == zoomedImage.colorSpace {
|
||||
return updatePreview(zoomedImage, pixelSize, middlePosition)
|
||||
return updatePreview(zoomedImage, pixelSize, zoomedSize)
|
||||
} else if let image = zoomedImage.copy(colorSpace: colorSpace) {
|
||||
return updatePreview(image, pixelSize, middlePosition)
|
||||
return updatePreview(image, pixelSize, zoomedSize)
|
||||
}
|
||||
}
|
||||
|
||||
// If that also didn't work, then just return the image in its default color space.
|
||||
return updatePreview(zoomedImage, pixelSize, middlePosition)
|
||||
return updatePreview(zoomedImage, pixelSize, zoomedSize)
|
||||
}
|
||||
}
|
||||
|
||||
private func updatePreview(_ image: CGImage, _ pixelSize: CGFloat, _ middlePosition: CGFloat) {
|
||||
private func updatePreview(_ image: CGImage, _ pixelSize: CGFloat, _ numberOfPixels: CGFloat) {
|
||||
let middlePosition = numberOfPixels / 2
|
||||
|
||||
// Extract the middle pixel color from the prepared image.
|
||||
let colorAtPixel = image.colorAt(x: Int(middlePosition), y: Int(middlePosition))
|
||||
let contrastingColor = colorAtPixel.bestContrastingColor()
|
||||
|
||||
preview.layer?.contents = image
|
||||
preview.updateCrosshair(pixelSize, middlePosition, contrastingColor.cgColor)
|
||||
// Update picker border and info panel.
|
||||
wrapper.update(contrastingColor.cgColor)
|
||||
updateInfoPanel(colorAtPixel, contrastingColor)
|
||||
|
||||
// Update the picker preview.
|
||||
preview.layer?.contents = image
|
||||
preview.updateCrosshair(pixelSize, middlePosition, contrastingColor.cgColor)
|
||||
let showGrid = PPState.shared.gridSetting == .always || (focusMode && PPState.shared.gridSetting == .inFocusMode)
|
||||
preview.updateGrid(cellSize: pixelSize, numberOfCells: Int(numberOfPixels), shouldDisplay: showGrid)
|
||||
|
||||
// Save color under pixel (used when copied).
|
||||
lastHighlightedColor = colorAtPixel
|
||||
}
|
||||
@@ -310,58 +320,3 @@ class PPOverlayController: NSWindowController {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
extension CGImage {
|
||||
// In order to get the color at a given pixel from a CGImage, we need to convert the CGImage's
|
||||
// data to a bitmap and draw it. We do so via a CGContext, and then manually extract the data at
|
||||
// the desired pixel.
|
||||
func colorAt(x: Int, y: Int) -> NSColor {
|
||||
assert(0 <= x && x < self.width)
|
||||
assert(0 <= y && y < self.height)
|
||||
|
||||
let bitmapBytesPerRow = width * 4
|
||||
let bitmapByteCount = bitmapBytesPerRow * Int(self.height)
|
||||
|
||||
// Allocate memory for image data. This is the destination in memory where any drawing to
|
||||
// the bitmap context will be rendered.
|
||||
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue)
|
||||
let bitmapData = malloc(bitmapByteCount)
|
||||
|
||||
// Since we manually allocate memeory for the data, we must ensure that the same memory is
|
||||
// freed after we've used it.
|
||||
defer { free(bitmapData) }
|
||||
|
||||
// Create the bitmap context.
|
||||
let context = CGContext(
|
||||
data: bitmapData,
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
bitsPerComponent: 8,
|
||||
bytesPerRow: bitmapBytesPerRow,
|
||||
space: CGColorSpaceCreateDeviceRGB(),
|
||||
bitmapInfo: bitmapInfo.rawValue
|
||||
)
|
||||
|
||||
// Extract the pixel data from the right offset.
|
||||
if context != nil {
|
||||
// First, we draw the image data onto the context we created.
|
||||
let rect = CGRect(x: 0, y: 0, width: self.width, height: self.height)
|
||||
context!.draw(self, in: rect)
|
||||
|
||||
// Then we extract the data at the right spot.
|
||||
let data = context!.data!
|
||||
let offset = 4 * (y * width + x)
|
||||
|
||||
let a = CGFloat(data.load(fromByteOffset: offset, as: UInt8.self)) / 255.0
|
||||
let r = CGFloat(data.load(fromByteOffset: offset + 1, as: UInt8.self)) / 255.0
|
||||
let g = CGFloat(data.load(fromByteOffset: offset + 2, as: UInt8.self)) / 255.0
|
||||
let b = CGFloat(data.load(fromByteOffset: offset + 3, as: UInt8.self)) / 255.0
|
||||
return NSColor(red: r, green: g, blue: b, alpha: a)
|
||||
}
|
||||
|
||||
// Creating the context failed, so return a default color instead.
|
||||
// Hopefully, this should never happen.
|
||||
Log.error?.message("Failed to create CGContext!")
|
||||
return NSColor.black
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,12 @@
|
||||
//
|
||||
|
||||
// This class is in charge of rendering the pixel preview.
|
||||
//
|
||||
// TODO: draw grid b/w pixels when zoomed?
|
||||
class PPOverlayPreview: NSView, CALayerDelegate {
|
||||
// A grid that's optionally shown in the preview.
|
||||
private var grid: CAShapeLayer = CAShapeLayer()
|
||||
private var lastCellSize: CGFloat?
|
||||
private var lastNumberOfCells: Int?
|
||||
|
||||
// The small pixel-box in the centre of the picker.
|
||||
var crosshair: CAShapeLayer = CAShapeLayer()
|
||||
|
||||
@@ -21,6 +24,11 @@ class PPOverlayPreview: NSView, CALayerDelegate {
|
||||
layer?.contentsGravity = kCAGravityResizeAspectFill
|
||||
layer?.delegate = self
|
||||
|
||||
// Add the grid shape layers to the view.
|
||||
grid.strokeColor = NSColor.gray.cgColor
|
||||
grid.fillColor = nil
|
||||
layer?.addSublayer(grid)
|
||||
|
||||
// Prepare crosshair shape layers.
|
||||
crosshair.strokeColor = NSColor.black.cgColor
|
||||
crosshair.fillColor = nil
|
||||
@@ -36,4 +44,31 @@ class PPOverlayPreview: NSView, CALayerDelegate {
|
||||
crosshair.strokeColor = color
|
||||
setNeedsDisplay(pixelRect)
|
||||
}
|
||||
|
||||
// Redraws the grid in the preview.
|
||||
func updateGrid(cellSize size: CGFloat, numberOfCells n: Int, shouldDisplay: Bool) {
|
||||
// Disable implicit animations within this transaction.
|
||||
CATransaction.setDisableActions(true)
|
||||
grid.opacity = shouldDisplay ? 0.25 : 0
|
||||
|
||||
// Only redraw the grid if we need to display it and the dimensions have changed since last
|
||||
// the last time we drew it.
|
||||
guard shouldDisplay && (size != lastCellSize || n != lastNumberOfCells) else { return }
|
||||
lastCellSize = size
|
||||
lastNumberOfCells = n
|
||||
|
||||
let path = NSBezierPath()
|
||||
let start = CGFloat(0)
|
||||
let end = CGFloat(n) * size
|
||||
|
||||
for i in 1..<n {
|
||||
let pos = CGFloat(i) * size
|
||||
path.move(to: NSPoint(x: pos, y: start))
|
||||
path.line(to: NSPoint(x: pos, y: end))
|
||||
path.move(to: NSPoint(x: start, y: pos))
|
||||
path.line(to: NSPoint(x: end, y: pos))
|
||||
}
|
||||
|
||||
grid.path = path.cgPath
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,9 +31,12 @@ import CleanroomLogger
|
||||
|
||||
// Magnification level of the picker.
|
||||
var magnificationLevel: Int = 8
|
||||
|
||||
// When to draw a grid in the preview.
|
||||
var gridSetting: GridSetting = .inFocusMode
|
||||
|
||||
// Hold this down to enter concentration mode.
|
||||
var concentrationModeModifier: NSEvent.ModifierFlags = .control
|
||||
// Hold this down to enter focus mode.
|
||||
var focusModeModifier: NSEvent.ModifierFlags = .control
|
||||
|
||||
// The currently chosen format.
|
||||
var chosenFormat: PPColor = .genericHex
|
||||
@@ -65,7 +68,7 @@ import CleanroomLogger
|
||||
|
||||
func resetState() {
|
||||
paschaModeEnabled = false
|
||||
concentrationModeModifier = .control
|
||||
focusModeModifier = .control
|
||||
activatingShortcut = nil
|
||||
chosenFormat = .genericHex
|
||||
floatPrecision = 3
|
||||
@@ -96,8 +99,8 @@ import CleanroomLogger
|
||||
switch key {
|
||||
case "paschaModeEnabled":
|
||||
paschaModeEnabled = value.bool ?? false
|
||||
case "concentrationModeModifier":
|
||||
concentrationModeModifier = NSEvent.ModifierFlags(rawValue: value.uInt ?? NSEvent.ModifierFlags.control.rawValue)
|
||||
case "focusModeModifier":
|
||||
focusModeModifier = NSEvent.ModifierFlags(rawValue: value.uInt ?? NSEvent.ModifierFlags.control.rawValue)
|
||||
case "activatingShortcut":
|
||||
if let keyCode = value["keyCode"].uInt, let modifierFlags = value["modifierFlags"].uInt {
|
||||
let shortcut = MASShortcut(keyCode: keyCode, modifierFlags: modifierFlags)
|
||||
@@ -150,7 +153,7 @@ import CleanroomLogger
|
||||
|
||||
let json: JSON = [
|
||||
"paschaModeEnabled": paschaModeEnabled,
|
||||
"concentrationModeModifier": concentrationModeModifier.rawValue,
|
||||
"focusModeModifier": focusModeModifier.rawValue,
|
||||
"activatingShortcut": shortcutData,
|
||||
"magnificationLevel": magnificationLevel,
|
||||
"colorSpace": colorSpace ?? "",
|
||||
|
||||
@@ -15,10 +15,18 @@ void LogWarning(void);
|
||||
|
||||
// We use an undocumented API to hide the cursor even when the application *isn't* active.
|
||||
// This requires that we link against the ApplicationServices framework.
|
||||
// See https://stackoverflow.com/a/3939241/5552584
|
||||
// and https://web.archive.org/web/20150609013355/http://lists.apple.com:80/archives/carbon-dev/2006/Jan/msg00555.html
|
||||
// and http://doomlaser.com/cursorcerer-hide-your-cursor-at-will/
|
||||
void CGSSetConnectionProperty(int a, int b, CFStringRef c, CFBooleanRef d);
|
||||
int _CGSDefaultConnection(void);
|
||||
// See: https://stackoverflow.com/a/3939241/5552584
|
||||
// Also: https://github.com/asmagill/hammerspoon_asm.undocumented/blob/master/cursor/CGSConnection.h
|
||||
|
||||
/// Every application is given a singular connection ID through which it can receieve and manipulate
|
||||
/// values, state, notifications, events, etc. in the Window Server.
|
||||
typedef int CGSConnectionID;
|
||||
|
||||
/// Associates a value for the given key on the given connection.
|
||||
CGError CGSSetConnectionProperty(CGSConnectionID cid, CGSConnectionID targetCID, CFStringRef key, CFTypeRef value);
|
||||
|
||||
/// Both of these methods get the default connection for this process.
|
||||
CGSConnectionID _CGSDefaultConnection(void);
|
||||
CGSConnectionID CGSMainConnectionID(void);
|
||||
|
||||
#endif /* ShowAndHideCursor_h */
|
||||
|
||||
@@ -31,7 +31,8 @@ void HideCursor() {
|
||||
#endif
|
||||
|
||||
CFStringRef propertyString = CFStringCreateWithCString(NULL, "SetsCursorInBackground", kCFStringEncodingUTF8);
|
||||
CGSSetConnectionProperty(_CGSDefaultConnection(), _CGSDefaultConnection(), propertyString, kCFBooleanTrue);
|
||||
CGSConnectionID cid = _CGSDefaultConnection();
|
||||
CGSSetConnectionProperty(cid, cid, propertyString, kCFBooleanTrue);
|
||||
CFRelease(propertyString);
|
||||
|
||||
CGDisplayHideCursor(kCGDirectMainDisplay);
|
||||
|
||||
@@ -4,11 +4,13 @@ PixelPicker is like Digital Color Meter, but lives in your menu bar and lets you
|
||||
|
||||
### Features
|
||||
|
||||
* Pick *any* pixel on your screen with ease
|
||||
* Different zoom levels
|
||||
* Multiple color export formats
|
||||
* Multi-monitor support
|
||||
* Global keyboard shortcuts
|
||||
* 📸 Pick *any* pixel on your screen with ease
|
||||
* 🔎 Different magnification levels
|
||||
* ✏ Multiple color export formats
|
||||
* 🖥 Multi-monitor support
|
||||
* 🎨 Supports different color spaces
|
||||
* ⌨️ Global keyboard shortcut activation
|
||||
* 🌄 Customisable preview
|
||||
* ✨
|
||||
|
||||

|
||||
@@ -26,9 +28,9 @@ After you've installed PixelPicker, it will live in your menu bar. You have many
|
||||
* Simply right-click the menu item and the picker will activate
|
||||
|
||||
Once you have the picker open, it should be quite straightforward! Move your mouse around the screen and click on the pixel you want.
|
||||
You can also hold down the <kbd>control (⌃)</kbd> key (this can be changed to another key) while the picker is open, and the picker will enter "Concentration Mode". This will zoom the pixel preview even further, and will slow down the picker's movement to make it easier to pick that hard-to-get pixel!
|
||||
You can also hold down the <kbd>control (⌃)</kbd> key (this can be changed to another key) while the picker is open, and the picker will enter "Focus Mode". This will zoom the pixel preview even further, and will slow down the picker's movement to make it easier to pick that hard-to-get pixel!
|
||||
|
||||
Remember, you can press <kbd>space</kbd> (or <kbd>shift</kbd> + <kbd>space</kbd>) to cycle between color formats while the picker is open.
|
||||
**ProTip:** Remember, you can press <kbd>space</kbd> (or <kbd>shift</kbd> + <kbd>space</kbd>) to cycle between color formats while the picker is open.
|
||||
|
||||
### Options
|
||||
|
||||
@@ -36,20 +38,26 @@ All of PixelPicker's options are in its menubar dropdown. Simply click on the me
|
||||
|
||||
PixelPicker provides some neat options:
|
||||
|
||||
* **Concentration Modifier**
|
||||
* Choose which modifier activates concentration mode. When concentration mode is active, the picker zooms closer in, and your mouse moves slower to help you pick the right pixel.
|
||||
* **Color Format**
|
||||
* 🔳 **Show Grid**
|
||||
* Whether or not to show a grid around the pixels in the picker's preview.
|
||||
* 🎨 **Color Space**
|
||||
* Computer screens are complex things, and have various color spaces. You can either leave this setting as its default (it will choose the default color space of your screen) or you can specify a specific color space and PixelPicker will try and use that instead (compatibility depends on your computer's screen/monitor).
|
||||
* ✏️ **Color Format**
|
||||
* How should the color be copied when you select it? Examples include CSS Hex: `#aabbcc`, or Java RGB: `new Color(158, 198, 117)`, etc.
|
||||
* **Float Precision**
|
||||
* 🔍 **Magnification**
|
||||
- How far in should the preview zoom/how big should each pixel be?
|
||||
* 🧐 **Focus Modifier**
|
||||
* Choose which modifier activates focus mode. When focus mode is active, the picker zooms even closer in, and your mouse moves slower to help you pick the right pixel.
|
||||
* 🔢 **Float Precision**
|
||||
* This controls how many digits will come after the decimal point when using float-based color formats.
|
||||
* **Picker Shortcut**
|
||||
* ⌨️ **Picker Shortcut**
|
||||
* The global keyboard shortcut to use that will activate Pixel Picker.
|
||||
* **Launch at Login**
|
||||
* 🚀 **Launch at Login**
|
||||
* If this is enabled then PixelPicker will be launched when you log into your computer.
|
||||
|
||||
#### Extra Overrides or Hidden Settings
|
||||
|
||||
PixelPicker has some experimental overrides that can be toggled via the Terminal (`defaults write ...`, etc). Look under the "Experimental Overrides" section at `PixelPicker` -> `About` for more information.
|
||||
In some releases PixelPicker might have some experimental overrides that can be toggled via the Terminal (`defaults write ...`, etc). Look under the "Experimental Overrides" section at `PixelPicker` -> `About` for more information.
|
||||
|
||||
## Questions/Concerns
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
|
Before Width: | Height: | Size: 328 KiB After Width: | Height: | Size: 354 KiB |
Reference in New Issue
Block a user