From adf5a5e78412bb681623b695cf671cb4a997572c Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 17 Dec 2017 10:48:24 -0800 Subject: [PATCH] Add NSImage extension method to tint an image with a color. --- .../RSCore/RSCore.xcodeproj/project.pbxproj | 4 +++ .../RSCore/RSCore/Images/NSImage+RSCore.swift | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 Frameworks/RSCore/RSCore/Images/NSImage+RSCore.swift diff --git a/Frameworks/RSCore/RSCore.xcodeproj/project.pbxproj b/Frameworks/RSCore/RSCore.xcodeproj/project.pbxproj index 33d44a143..99818cfd2 100755 --- a/Frameworks/RSCore/RSCore.xcodeproj/project.pbxproj +++ b/Frameworks/RSCore/RSCore.xcodeproj/project.pbxproj @@ -71,6 +71,7 @@ 842E45CC1ED623C7000A8B52 /* UniqueIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 842E45CB1ED623C7000A8B52 /* UniqueIdentifier.swift */; }; 8432B1861DACA0E90057D6DF /* NSResponder-Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8432B1851DACA0E90057D6DF /* NSResponder-Extensions.swift */; }; 8432B1881DACA2060057D6DF /* NSWindow-Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8432B1871DACA2060057D6DF /* NSWindow-Extensions.swift */; }; + 84411E731FE5FFC3004B527F /* NSImage+RSCore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84411E721FE5FFC3004B527F /* NSImage+RSCore.swift */; }; 844C915B1B65753E0051FC1B /* RSPlist.h in Headers */ = {isa = PBXBuildFile; fileRef = 844C91591B65753E0051FC1B /* RSPlist.h */; settings = {ATTRIBUTES = (Public, ); }; }; 844C915C1B65753E0051FC1B /* RSPlist.m in Sources */ = {isa = PBXBuildFile; fileRef = 844C915A1B65753E0051FC1B /* RSPlist.m */; }; 844F91D51D90D86100820C48 /* RSTransparentContainerView.h in Headers */ = {isa = PBXBuildFile; fileRef = 844F91D31D90D86100820C48 /* RSTransparentContainerView.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -189,6 +190,7 @@ 842E45CB1ED623C7000A8B52 /* UniqueIdentifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UniqueIdentifier.swift; sourceTree = ""; }; 8432B1851DACA0E90057D6DF /* NSResponder-Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSResponder-Extensions.swift"; sourceTree = ""; }; 8432B1871DACA2060057D6DF /* NSWindow-Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSWindow-Extensions.swift"; sourceTree = ""; }; + 84411E721FE5FFC3004B527F /* NSImage+RSCore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "NSImage+RSCore.swift"; path = "Images/NSImage+RSCore.swift"; sourceTree = ""; }; 844C91591B65753E0051FC1B /* RSPlist.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RSPlist.h; path = RSCore/RSPlist.h; sourceTree = ""; }; 844C915A1B65753E0051FC1B /* RSPlist.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RSPlist.m; path = RSCore/RSPlist.m; sourceTree = ""; }; 844F91D31D90D86100820C48 /* RSTransparentContainerView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RSTransparentContainerView.h; sourceTree = ""; }; @@ -468,6 +470,7 @@ 84CFF5631AC3D13C00CEA6C8 /* RSImageRenderer.m */, 84CFF5671AC3D1B000CEA6C8 /* RSScaling.h */, 84CFF5681AC3D1B000CEA6C8 /* RSScaling.m */, + 84411E721FE5FFC3004B527F /* NSImage+RSCore.swift */, ); name = Images; path = RSCore; @@ -771,6 +774,7 @@ 8461387F1DB3F5BE00048B83 /* RSToolbarItem.swift in Sources */, 84B99C941FAE64D500ECDEDB /* DisplayNameProvider.swift in Sources */, 84BB45431D6909C700B48537 /* NSMutableDictionary-Extensions.swift in Sources */, + 84411E731FE5FFC3004B527F /* NSImage+RSCore.swift in Sources */, 845DE0F41B80477100D1571B /* NSSet+RSCore.m in Sources */, 842635591D7FA24800196285 /* NSOutlineView+Extensions.swift in Sources */, 844C915C1B65753E0051FC1B /* RSPlist.m in Sources */, diff --git a/Frameworks/RSCore/RSCore/Images/NSImage+RSCore.swift b/Frameworks/RSCore/RSCore/Images/NSImage+RSCore.swift new file mode 100644 index 000000000..4b24f6dbc --- /dev/null +++ b/Frameworks/RSCore/RSCore/Images/NSImage+RSCore.swift @@ -0,0 +1,28 @@ +// +// NSImage+RSCore.swift +// RSCore +// +// Created by Brent Simmons on 12/16/17. +// Copyright © 2017 Ranchero Software, LLC. All rights reserved. +// + +import Cocoa + +public extension NSImage { + + public func tinted(with color: NSColor) -> NSImage { + + let image = self.copy() as! NSImage + + image.lockFocus() + + color.set() + let rect = NSRect(x: 0, y: 0, width: image.size.width, height: image.size.height) + rect.fill(using: .sourceAtop) + + image.unlockFocus() + + image.isTemplate = false + return image + } +}