Skip to content

Commit

Permalink
Add special swatch drawing for clear color
Browse files Browse the repository at this point in the history
  • Loading branch information
myell0w committed Nov 20, 2024
1 parent 523580c commit ce22a57
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
27 changes: 27 additions & 0 deletions Sources/ColorWellKit/Utilities/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/ColorWellKit/Views/Cocoa/CWColorWellPopover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/ColorWellKit/Views/Cocoa/CWColorWellSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ class CWSwatchSegment: CWColorWellSegment {
guard let displayColor else {
return false
}
displayColor.drawSwatch(in: bounds)
displayColor.cw_drawSwatch(in: bounds)
return true
}

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit ce22a57

Please sign in to comment.