2015-02-11 10:41:06 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/common/api/atom_api_native_image.h"
|
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2017-10-09 16:13:40 +00:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2017-10-10 06:05:13 +00:00
|
|
|
#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"
|
2017-10-09 16:13:40 +00:00
|
|
|
|
2015-02-11 10:41:06 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2017-10-10 06:05:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:13:40 +00:00
|
|
|
mate::Handle<NativeImage> NativeImage::CreateFromNamedImage(
|
|
|
|
mate::Arguments* args, const std::string& name) {
|
|
|
|
@autoreleasepool {
|
2017-10-10 06:05:13 +00:00
|
|
|
std::vector<double> hsl_shift;
|
2017-10-09 16:13:40 +00:00
|
|
|
NSImage* image = [NSImage imageNamed:base::SysUTF8ToNSString(name)];
|
|
|
|
if (!image.valid) {
|
|
|
|
return CreateEmpty(args->isolate());
|
|
|
|
}
|
|
|
|
|
2017-10-10 06:05:13 +00:00
|
|
|
NSData* pngData = bufferFromNSImage(image);
|
|
|
|
|
|
|
|
if (args->GetNext(&hsl_shift) && hsl_shift.size() == 3) {
|
|
|
|
gfx::Image gfxImage = gfx::Image::CreateFrom1xPNGBytes(
|
|
|
|
reinterpret_cast<const unsigned char*>((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());
|
|
|
|
}
|
2017-10-09 16:13:40 +00:00
|
|
|
|
|
|
|
return CreateFromPNG(args->isolate(), (char *) [pngData bytes], [pngData length]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 08:20:03 +00:00
|
|
|
void NativeImage::SetTemplateImage(bool setAsTemplate) {
|
|
|
|
[image_.AsNSImage() setTemplate:setAsTemplate];
|
|
|
|
}
|
|
|
|
|
2015-07-27 04:58:48 +00:00
|
|
|
bool NativeImage::IsTemplateImage() {
|
2015-07-29 03:48:40 +00:00
|
|
|
return [image_.AsNSImage() isTemplate];
|
2015-07-27 04:58:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 10:41:06 +00:00
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|