DrawingTools

2.1.1

Some tools to help with drawing in Swift
RougeWare/Swift-Drawing-Tools

What's New

2.1 - RectangleTools 2.10

2021-07-23T15:40:28Z

RectangleTools found a way to solve the same problem without a major version change, so this downgraded to the newer RectangleTools 2.10.0

Patches

  • 2.1.1 - RectangleTools 2.10.1 (#11)

Swift Drawing Tools

Some tools to help with drawing in Swift

Context Sugar

The first tool in this package is some syntactic sugar around CGContext. This lets you discard the boilerplate and get down to what really matters. For example, if you wanted to draw a color swatch:

With Drawing Tools:

import DrawingTools
import CrossKitTypes  // Both of these are
import RectangleTools // included in this package



extension NativeImage {
    static func swatch(color: NativeColor, size: CGSize = .one) throws -> NativeImage {
        try drawNew(size: size, context: .goodForSwatch(size: size)) { context in
            guard let context = context else { return }
            context.setFillColor(color.cgColor)
            context.fill(CGRect(origin: .zero, size: size))
        }
    }
}

Without Drawing Tools:

#if canImport(UIKit)

import UIKit



extension UIImage {
    static func swatch(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(size, true, 1)
        defer { UIGraphicsEndImageContext() }
        if let context = UIGraphicsGetCurrentContext() {
            context.setFillColor(color.cgColor)
            context.fill(CGRect(origin: .zero, size: size))
        }
        else {
            assertionFailure("No context?")
        }
        
        return UIGraphicsGetImageFromCurrentImageContext()
            ?? UIImage(size: size)
    }
}

#elseif canImport(AppKit)

import AppKit



extension NSImage {
    static func swatch(color: NSColor, size: CGSize = CGSize(width: 1, height: 1)) -> NSImage {
        let displayScale: CGSize
        
        if let currentScreen = NSScreen.main ?? NSScreen.deepest ?? NSScreen.screens.first {
            let scaleFactor = currentScreen.backingScaleFactor
            displayScale = CGSize(width: scaleFactor, height: scaleFactor)
        }
        else {
            print("Attempted to scale CGContext for AppKit, but there doesn't seem to be a screen attached")
            displayScale = CGSize(width: 1, height: 1)
        }
        
        let image = NSImage(size: CGSize(width:  (size.width  / displayScale.width)  * 1,
                                         height: (size.height / displayScale.height) * 1))
        
        image.lockFocusFlipped(false)
        defer { image.unlockFocus() }
        if let context = NSGraphicsContext.current?.cgContext {
            context.setFillColor(color.cgColor)
            context.fill(CGRect(origin: .zero, size: size))
        }
        else {
            assertionFailure("No context?")
        }
        
        return image
    }
}

#else
#error("This library requires either UIKit or AppKit")
#endif

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

Last updated: Sat Mar 16 2024 11:51:30 GMT-0900 (Hawaii-Aleutian Daylight Time)