Swiftpack.co - Package - RougeWare/Swift-Drawing-Tools

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
import RectangleTools



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

Github

link
Stars: 0

Used By

Total: 0

Releases

1.1.1 - `drawNew` now `rethrows` instead of `throws` - 2020-08-20 00:45:20

Turns out i just typed throws out of habit after having to write a try. Whoops! Now you can use this API without a try keyword if your artist doesn't throw any errors.


Patch Changes

  • 1.1.1 (#4) - Better handling of AppKit context scale/PPI. It's not perfect, but it's better. Now, drawNew does a better job at considering the scale/PPI of the image you want to draw, via the context you passed

1.0.0 - MVP - 2020-08-17 00:48:18

Some drawing tools! Check out the Readme for what shipped first in this package 😄