Skip to content

Instantly share code, notes, and snippets.

View hebertialmeida's full-sized avatar
✈️

Heberti Almeida hebertialmeida

✈️
View GitHub Profile
@banjun
banjun / WindowCapture.swift
Last active January 31, 2024 18:44
specific window capture implementation memo for https://github.com/mzp/HeartVoice
import Cocoa
import CoreGraphics
import Vision
struct TargetWindow {
let id: CGWindowID
let bounds: CGRect
init?(appName: String, windowTitle: String) {
guard let windows = CGWindowListCopyWindowInfo(.optionAll, kCGNullWindowID) as? [[String: Any]] else { return nil }
@rnystrom
rnystrom / ViewController.swift
Created May 29, 2018 01:16
Interactive NSAttributedString link highlighting with TextKit
import UIKit
public extension CGSize {
func snapped(scale: CGFloat) -> CGSize {
var size = self
size.width = ceil(size.width * scale) / scale
size.height = ceil(size.height * scale) / scale
return size
}
@milseman
milseman / eat_seek_peek.swift
Created February 28, 2018 20:26
Playground for Self-sliced Collections: eat/seek/peek
// Eat/seek/peek
extension Collection where SubSequence == Self, Element: Equatable {
mutating func eat() -> Element {
defer { self = self.dropFirst() }
return peek()
}
mutating func eat(_ n: Int) -> SubSequence {
let (pre, rest) = self.seek(n)
defer { self = rest }
@JaviSoto
JaviSoto / UIView+TWSafeAreaInsets.swift
Last active April 14, 2020 11:55
Status-bar agnostic UIView.safeAreaInsets API
extension UIView {
/// Convenience API to query iOS 11's `UIView.safeAreaInsets`'s insets (also known as "#NotEmbraceTheNotch")
/// in a backwards compatible API.
/// It also differs slightly from `UIView.safeAreaInsets` in that it only takes the "Notch" into account
/// and not the status bar. This allows you to inset content so that the notch doesn't clip it, but you can still
/// lay it out below the status bar.
/// Note: This won't be as versitile as the UIKit version because it won't take into account things like navigation bars,
/// but it should be correct for views in "full-screen" view controllers, where the UIKit `safeAreaInsets` API falls short.
var twSafeAreaInsets: UIEdgeInsets {
guard #available(iOS 11.0, *) else {
@PrasannaKumarChalla
PrasannaKumarChalla / RSVerticallyCenteredTextFieldCell.swift
Created August 27, 2017 18:08
vertically align nstextfield text
class RSVerticallyCenteredTextFieldCell: NSTextFieldCell {
var mIsEditingOrSelecting:Bool = false
override func drawingRect(forBounds theRect: NSRect) -> NSRect {
//Get the parent's idea of where we should draw
var newRect:NSRect = super.drawingRect(forBounds: theRect)
// When the text field is being edited or selected, we have to turn off the magic because it screws up
// the configuration of the field editor. We sneak around this by intercepting selectWithFrame and editWithFrame and sneaking a
// reduced, centered rect in at the last minute.
/*
Erica Sadun, http://ericasadun.com
Cross Platform Defines
Apple Platforms Only
Will update to #if canImport() when available
*/
// Makes the text in an NSTextFieldCell vertically centered. Works with single line, non-editable cells.
// Maybe doesn't work with others.
// Stolen from this stackoverflow question:
// http://stackoverflow.com/questions/1235219/is-there-a-right-way-to-have-nstextfieldcell-draw-vertically-centered-text
import Cocoa
class VerticallyCenteredTextFieldCell: NSTextFieldCell {
override func titleRect(forBounds rect: NSRect) -> NSRect {
import UIKit
extension UIImage {
// colorize image with given tint color
// this is similar to Photoshop's "Color" layer blend mode
// this is perfect for non-greyscale source images, and images that have both highlights and shadows that should be preserved
// white will stay white and black will stay black as the lightness of the image is preserved
func tint(tintColor: UIColor) -> UIImage {
import cv2
import json
import numpy
import re
import sys
from operator import itemgetter, attrgetter
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
@steipete
steipete / ios-xcode-device-support.sh
Last active December 12, 2023 03:36
Using iOS 15 devices with Xcode 12.5 (instead of Xcode 13)
# The trick is to link the DeviceSupport folder from the beta to the stable version.
# sudo needed if you run the Mac App Store version. Always download the dmg instead... you'll thank me later :)
# Support iOS 15 devices (Xcode 13.0) with Xcode 12.5:
sudo ln -s /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/15.0 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
# Then restart Xcode and reconnect your devices. You will need to do that for every beta of future iOS versions
# (A similar approach works for older versions too, just change the version number after DeviceSupport)