second commit

This commit is contained in:
Alex E. Tafoya
2026-09-17 16:15:02 -05:00
parent 87fe7927c4
commit 7e3801c7c6
10 changed files with 585 additions and 151 deletions
+32
View File
@@ -76,6 +76,38 @@ enum ImageExporter {
return url
}
/// Reads a single pixel from an image. `point` is in image points with a top-left
/// origin; `scale` converts those points to pixels.
static func color(at point: CGPoint, in image: CGImage, scale: CGFloat) -> NSColor? {
let x = Int((point.x * scale).rounded(.down))
let y = Int((point.y * scale).rounded(.down))
guard x >= 0, y >= 0, x < image.width, y < image.height else { return nil }
var pixel: [UInt8] = [0, 0, 0, 0]
guard let context = CGContext(data: &pixel,
width: 1,
height: 1,
bitsPerComponent: 8,
bytesPerRow: 4,
space: CGColorSpace(name: CGColorSpace.sRGB) ?? CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
else { return nil }
// Draw the image shifted so the pixel of interest lands in the 1x1 context.
context.draw(image,
in: CGRect(x: -CGFloat(x),
y: -CGFloat(image.height - 1 - y),
width: CGFloat(image.width),
height: CGFloat(image.height)))
let alpha = CGFloat(pixel[3]) / 255
guard alpha > 0 else { return NSColor.clear }
return NSColor(srgbRed: CGFloat(pixel[0]) / 255 / alpha,
green: CGFloat(pixel[1]) / 255 / alpha,
blue: CGFloat(pixel[2]) / 255 / alpha,
alpha: 1)
}
static func format(forPathExtension pathExtension: String) -> OutputFormat {
switch pathExtension.lowercased() {
case "jpg", "jpeg": return .jpg