diff --git a/atom/common/api/atom_api_native_image_mac.mm b/atom/common/api/atom_api_native_image_mac.mm index d9665ceabe..058d5e930d 100644 --- a/atom/common/api/atom_api_native_image_mac.mm +++ b/atom/common/api/atom_api_native_image_mac.mm @@ -7,23 +7,50 @@ #import #include "base/strings/sys_string_conversions.h" +#include "ui/gfx/color_utils.h" +#include "ui/gfx/image/image.h" +#include "ui/gfx/image/image_skia.h" +#include "ui/gfx/image/image_skia_operations.h" namespace atom { namespace api { +NSData* bufferFromNSImage(NSImage* image) { + CGImageRef ref = [image CGImageForProposedRect:nil context:nil hints:nil]; + NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithCGImage:ref]; + [rep setSize:[image size]]; + return [rep representationUsingType:NSPNGFileType properties:[[NSDictionary alloc] init]]; +} + +double safeShift(double in, double def) { + if (in >= 0 || in <= 1 || in == def) return in; + return def; +} + mate::Handle NativeImage::CreateFromNamedImage( mate::Arguments* args, const std::string& name) { @autoreleasepool { + std::vector hsl_shift; NSImage* image = [NSImage imageNamed:base::SysUTF8ToNSString(name)]; if (!image.valid) { return CreateEmpty(args->isolate()); } - CGImageRef ref = [image CGImageForProposedRect:nil context:nil hints:nil]; - NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithCGImage:ref]; - [rep setSize:[image size]]; - NSData* pngData = [rep representationUsingType:NSPNGFileType properties:[[NSDictionary alloc] init]]; + NSData* pngData = bufferFromNSImage(image); + + if (args->GetNext(&hsl_shift) && hsl_shift.size() == 3) { + gfx::Image gfxImage = gfx::Image::CreateFrom1xPNGBytes( + reinterpret_cast((char *) [pngData bytes]), [pngData length]); + color_utils::HSL shift = { + safeShift(hsl_shift[0], -1), + safeShift(hsl_shift[1], 0.5), + safeShift(hsl_shift[2], 0.5) + }; + pngData = bufferFromNSImage(gfx::Image( + gfx::ImageSkiaOperations::CreateHSLShiftedImage( + gfxImage.AsImageSkia(), shift)).CopyNSImage()); + } return CreateFromPNG(args->isolate(), (char *) [pngData bytes], [pngData length]); } diff --git a/docs/api/native-image.md b/docs/api/native-image.md index 96510eecf7..186df8fb1f 100644 --- a/docs/api/native-image.md +++ b/docs/api/native-image.md @@ -157,9 +157,10 @@ Returns `NativeImage` Creates a new `NativeImage` instance from `dataURL`. -### `nativeImage.createFromNamedImage(imageName)` _macOS_ +### `nativeImage.createFromNamedImage(imageName[, hslShift])` _macOS_ * `imageName` String +* `hslShift` Number[] Returns `NativeImage` @@ -167,6 +168,23 @@ Creates a new `NativeImage` instance from the NSImage that maps to the given image name. See [`NSImageName`](https://developer.apple.com/documentation/appkit/nsimagename?language=objc) for a list of possible values. +The `hslShift` is applied to the image with the following rules +* `hsl_shift[0]` (hue): The absolute hue value for the image - 0 and 1 map + to 0 and 360 on the hue color wheel (red). +* `hsl_shift[1]` (saturation): A saturation shift for the image, with the + following key values: + 0 = remove all color. + 0.5 = leave unchanged. + 1 = fully saturate the image. +* `hsl_shift[2]` (lightness): A lightness shift for the image, with the + following key values: + 0 = remove all lightness (make all pixels black). + 0.5 = leave unchanged. + 1 = full lightness (make all pixels white). + +This means that `[-1, 0, 1]` will make the image completely white and +`[-1, 1, 0]` will make the image completely black. + ## Class: NativeImage > Natively wrap images such as tray, dock, and application icons.