Enable hsl shifting of named images

This commit is contained in:
Samuel Attard 2017-10-10 17:05:13 +11:00
parent 767a178bd1
commit b5ba8699f3
No known key found for this signature in database
GPG key ID: E89DDE5742D58C4E
2 changed files with 50 additions and 5 deletions

View file

@ -7,23 +7,50 @@
#import <Cocoa/Cocoa.h>
#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> NativeImage::CreateFromNamedImage(
mate::Arguments* args, const std::string& name) {
@autoreleasepool {
std::vector<double> 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<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());
}
return CreateFromPNG(args->isolate(), (char *) [pngData bytes], [pngData length]);
}