Skip to content

Commit

Permalink
Gardening
Browse files Browse the repository at this point in the history
Remove unnecessary code
Style changes
  • Loading branch information
jordanbaird committed Dec 29, 2023
1 parent 57f07e1 commit fd292fd
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 132 deletions.
52 changes: 0 additions & 52 deletions Sources/ColorWellKit/Utilities/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@ extension CGRect {
copy.origin.y = other.midY - copy.height / 2
return copy
}

/// Returns a rectangle that has been inset by the given dimension.
func inset(by dimension: CGFloat) -> CGRect {
insetBy(dx: dimension, dy: dimension)
}
}

// MARK: Comparable
extension Comparable {
/// Returns a value that is the result of clamping the current value to
/// the given limiting range.
func clamped(to limits: ClosedRange<Self>) -> Self {
min(max(self, limits.lowerBound), limits.upperBound)
}
}

// MARK: NSColor
Expand Down Expand Up @@ -75,44 +61,6 @@ extension NSColor {
self.init(srgbRed: r, green: g, blue: b, alpha: a)
}

/// Creates a new color object whose component values are a weighted sum
/// of the current and specified color objects.
///
/// This method converts both colors to RGB before blending. If either
/// color is unable to be converted, this method returns the current color
/// unaltered.
///
/// - Parameters:
/// - fraction: The amount of `color` to blend with the current color.
/// - color: The color to blend with the current color.
///
/// - Returns: The blended color, if successful. If either color is unable
/// to be converted, the current color is returned unaltered.
func blending(fraction: CGFloat, of color: NSColor) -> NSColor {
guard
let color1 = usingColorSpace(.genericRGB),
let color2 = color.usingColorSpace(.genericRGB)
else {
return self
}

var (r1, g1, b1, a1): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
var (r2, g2, b2, a2): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)

color1.getRed(&r1, green: &g1, blue: &b1, alpha: &a1)
color2.getRed(&r2, green: &g2, blue: &b2, alpha: &a2)

let clampedFraction = fraction.clamped(to: 0...1)
let inverseFraction = 1 - clampedFraction

let r = (r1 * inverseFraction).addingProduct(r2, clampedFraction)
let g = (g1 * inverseFraction).addingProduct(g2, clampedFraction)
let b = (b1 * inverseFraction).addingProduct(b2, clampedFraction)
let a = (a1 * inverseFraction).addingProduct(a2, clampedFraction)

return NSColor(calibratedRed: r, green: g, blue: b, alpha: a)
}

/// Returns a Boolean value that indicates whether this color resembles
/// the given color, using the specified tolerance.
///
Expand Down
18 changes: 0 additions & 18 deletions Sources/ColorWellKit/Utilities/Injection.swift

This file was deleted.

15 changes: 5 additions & 10 deletions Sources/ColorWellKit/Utilities/ObjectAssociation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@ enum AssociationPolicy {

fileprivate var objcValue: objc_AssociationPolicy {
switch self {
case .assign:
return .OBJC_ASSOCIATION_ASSIGN
case .copy:
return .OBJC_ASSOCIATION_COPY
case .copyNonatomic:
return .OBJC_ASSOCIATION_COPY_NONATOMIC
case .retain:
return .OBJC_ASSOCIATION_RETAIN
case .retainNonatomic:
return .OBJC_ASSOCIATION_RETAIN_NONATOMIC
case .assign: .OBJC_ASSOCIATION_ASSIGN
case .copy: .OBJC_ASSOCIATION_COPY
case .copyNonatomic: .OBJC_ASSOCIATION_COPY_NONATOMIC
case .retain: .OBJC_ASSOCIATION_RETAIN
case .retainNonatomic: .OBJC_ASSOCIATION_RETAIN_NONATOMIC
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class CWColorWellLayoutView: NSGridView {
.nsBezierPath()
case .default, .minimal:
bezelPath = Path.fullColorWellPath(
rect: bounds.inset(by: lineWidth / 2),
rect: bounds.insetBy(dx: lineWidth / 2, dy: lineWidth / 2),
controlSize: colorWell.controlSize
)
.stroked(lineWidth: lineWidth)
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 @@ -366,7 +366,7 @@ extension CWColorWellPopover {
let color = color.usingColorSpace(.displayP3) ?? color
color.drawSwatch(in: bounds)
NSColor(white: 1 - color.averageBrightness, alpha: 0.3).setStroke()
let path = NSBezierPath(rect: bounds.inset(by: 1))
let path = NSBezierPath(rect: bounds.insetBy(dx: 1, dy: 1))
path.lineWidth = 2
path.stroke()
}
Expand Down
44 changes: 23 additions & 21 deletions Sources/ColorWellKit/Views/Cocoa/CWColorWellSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class CWColorWellSegment: NSView {
/// The fill color for a highlighted color well segment.
var highlightedSegmentColor: NSColor {
switch ColorScheme.current {
case .light: segmentColor.blending(fraction: 0.5, of: .selectedControlColor)
case .dark: segmentColor.blending(fraction: 0.2, of: .highlightColor)
case .light: segmentColor.blended(withFraction: 0.5, of: .selectedControlColor) ?? segmentColor
case .dark: segmentColor.blended(withFraction: 0.2, of: .highlightColor) ?? segmentColor
}
}

Expand Down Expand Up @@ -433,7 +433,7 @@ class CWBorderedSwatchSegment: CWSwatchSegment {

override var borderColor: NSColor {
switch ColorScheme.current {
case .light: super.borderColor.blending(fraction: 0.25, of: .controlTextColor)
case .light: super.borderColor.blended(withFraction: 0.25, of: .controlTextColor) ?? super.borderColor
case .dark: super.borderColor
}
}
Expand All @@ -454,25 +454,26 @@ class CWBorderedSwatchSegment: CWSwatchSegment {

segmentPath(bounds).fill(with: bezelColor)

let (inset, radius) = with(colorWell?.controlSize ?? .regular) { controlSize in
let standardInset: CGFloat = 3
let standardRadius: CGFloat = 2
return switch controlSize {
case .large:
(standardInset + 0.25, standardRadius + 0.2)
case .regular:
(standardInset, standardRadius)
case .small:
(standardInset - 0.75, standardRadius - 0.6)
case .mini:
(standardInset - 1, standardRadius - 0.8)
@unknown default:
(standardInset, standardRadius)
}
var inset: CGFloat = 3
var radius: CGFloat = 2
switch colorWell?.controlSize {
case .large:
inset += 0.25
radius += 0.2
case .regular, .none:
break // no change
case .small:
inset -= 0.75
radius -= 0.6
case .mini:
inset -= 1
radius -= 0.8
@unknown default:
break
}

let clippingPath = NSBezierPath(
roundedRect: bounds.inset(by: inset),
roundedRect: bounds.insetBy(dx: inset, dy: inset),
xRadius: radius,
yRadius: radius
)
Expand Down Expand Up @@ -607,7 +608,8 @@ class CWPullDownSwatchSegment: CWSwatchSegment {
width: bgDimension,
height: bgDimension
)
let caretBounds = with((bgDimension - lineWidth) / sizeFactor) { dimension in
let caretBounds: NSRect = {
let dimension = (bgDimension - lineWidth) / sizeFactor
let size = NSSize(
width: dimension,
height: dimension / 2
Expand All @@ -617,7 +619,7 @@ class CWPullDownSwatchSegment: CWSwatchSegment {
y: bgBounds.midY - (size.height / 2) - (lineWidth / 4)
)
return NSRect(origin: origin, size: size)
}
}()

return (bgBounds, caretBounds, lineWidth)
}()
Expand Down
29 changes: 0 additions & 29 deletions Tests/ColorWellKitTests/ColorWellKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,4 @@ final class ColorWellKitTests: XCTestCase {
XCTAssertEqual(rect3.origin.x, rect1.midX - (rect3.width / 2))
XCTAssertEqual(rect3.origin.y, rect1.midY - (rect3.height / 2))
}

func testNSColorBlending() {
continueAfterFailure = false

func randomColor() -> NSColor {
NSColor(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1),
alpha: .random(in: 0...1)
)
}

for _ in 0..<10_000 {
let color1 = randomColor()
let color2 = randomColor()

// it's important to test with fractions that are less than 0 and
// greater than 1, as they essentially invert the blending
// algorithm; randomize the stride so that we aren't only testing
// with "pretty" numbers
for n in stride(from: -0.1, through: 1.1, by: .random(in: 0.04...0.06)) {
XCTAssertEqual(
color1.blended(withFraction: n, of: color2),
color1.blending(fraction: n, of: color2)
)
}
}
}
}

0 comments on commit fd292fd

Please sign in to comment.