I've implemented custom button color highlitening this way:
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect);
setHighlightedColors();
}
private func setHighlightedColors() {
let textColorDifference: CGFloat = 50/255;
let textHighlightedColor = textColor == .clear ? textColor : NSColor(red: textColor.redComponent - textColorDifference,
green: textColor.greenComponent - textColorDifference,
blue: textColor.blueComponent - textColorDifference,
alpha: textColor.alphaComponent);
let backgroundColorDifference: CGFloat = 20/255;
let backgroundHighlightedColor = backgroundColor == .clear ? backgroundColor : NSColor(red: backgroundColor.redComponent - backgroundColorDifference,
green: backgroundColor.greenComponent - backgroundColorDifference,
blue: backgroundColor.blueComponent - backgroundColorDifference,
alpha: backgroundColor.alphaComponent);
let borderColorDifference: CGFloat = 50/255;
let borderHighlightedColor = borderColor == .clear ? borderColor : NSColor(red: borderColor.redComponent - borderColorDifference,
green: borderColor.greenComponent - borderColorDifference,
blue: borderColor.blueComponent - borderColorDifference,
alpha: borderColor.alphaComponent);
if isHighlighted {
applyTextColorAndFont(color: textColor, font: customFont);
applyBackgroundColor(color: backgroundHighlightedColor);
applyBorder(color: borderHighlightedColor, width: borderWidth)
} else {
applyTextColorAndFont(color: textHighlightedColor, font: customFont);
applyBackgroundColor(color: backgroundColor);
applyBorder(color: borderColor, width: borderWidth)
}
}