diff --git a/Sources/ColorWellKit/Utilities/Extensions.swift b/Sources/ColorWellKit/Utilities/Extensions.swift index dcc77ea..49b6c1a 100644 --- a/Sources/ColorWellKit/Utilities/Extensions.swift +++ b/Sources/ColorWellKit/Utilities/Extensions.swift @@ -169,6 +169,33 @@ extension NSColor { return copy } + + /// Special swatch drawing for clear color + func cw_drawSwatch(in rect: NSRect) { + if self.alphaComponent == 0.0 { + guard let context = NSGraphicsContext.current else { + self.drawSwatch(in: rect) + return + } + + context.saveGraphicsState() + defer { + context.restoreGraphicsState() + } + + context.cgContext.setFillColor(NSColor.white.cgColor) + context.cgContext.fill(rect) + context.cgContext.clip(to: rect) + context.cgContext.setLineWidth(2.0) + context.cgContext.setLineCap(.square) + context.cgContext.setStrokeColor(NSColor.red.cgColor) + context.cgContext.move(to: CGPoint(x: rect.minX, y: rect.minY)) + context.cgContext.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY)) + context.cgContext.strokePath() + } else { + self.drawSwatch(in: rect) + } + } } // MARK: NSColorPanel diff --git a/Sources/ColorWellKit/Views/Cocoa/CWColorWellPopover.swift b/Sources/ColorWellKit/Views/Cocoa/CWColorWellPopover.swift index 715214e..fac7f01 100644 --- a/Sources/ColorWellKit/Views/Cocoa/CWColorWellPopover.swift +++ b/Sources/ColorWellKit/Views/Cocoa/CWColorWellPopover.swift @@ -370,7 +370,7 @@ extension CWColorWellPopover { context.compositingOperation = .multiply let color = color.usingColorSpace(.displayP3) ?? color - color.drawSwatch(in: bounds) + color.cw_drawSwatch(in: bounds) NSColor(white: 1 - color.averageBrightness, alpha: 0.3).setStroke() let path = NSBezierPath(rect: bounds.insetBy(dx: 1, dy: 1)) path.lineWidth = 2 diff --git a/Sources/ColorWellKit/Views/Cocoa/CWColorWellSegment.swift b/Sources/ColorWellKit/Views/Cocoa/CWColorWellSegment.swift index d7afd85..6b4035b 100644 --- a/Sources/ColorWellKit/Views/Cocoa/CWColorWellSegment.swift +++ b/Sources/ColorWellKit/Views/Cocoa/CWColorWellSegment.swift @@ -324,7 +324,7 @@ class CWSwatchSegment: CWColorWellSegment { guard let displayColor else { return false } - displayColor.drawSwatch(in: bounds) + displayColor.cw_drawSwatch(in: bounds) return true } @@ -481,7 +481,7 @@ class CWBorderedSwatchSegment: CWSwatchSegment { clippingPath.lineWidth = 1 clippingPath.addClip() - displayColor.drawSwatch(in: bounds) + displayColor.cw_drawSwatch(in: bounds) borderColor.setStroke() clippingPath.stroke()