Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow full color tinting on grayscale template images. #1629

Merged
merged 4 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Source/ASImageNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,14 @@ - (void)setNeedsDisplayWithCompletion:(void (^ _Nullable)(BOOL canceled))display
[self setNeedsDisplay];
}

- (void)tintColorDidChange
{
[super tintColorDidChange];
if (_image.renderingMode == UIImageRenderingModeAlwaysTemplate) {
[self setNeedsDisplay];
}
}

#pragma mark Interface State

- (void)clearContents
Expand Down
10 changes: 9 additions & 1 deletion Source/Details/ASGraphicsContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ NS_INLINE void ASConfigureExtendedRange(UIGraphicsImageRendererFormat *format)

UIGraphicsImageRendererFormat *format;
if (sourceImage) {
format = sourceImage.imageRendererFormat;
if (sourceImage.renderingMode == UIImageRenderingModeAlwaysTemplate) {
// Template images will be black and transparent, so if we use
rahul-malik marked this conversation as resolved.
Show resolved Hide resolved
// sourceImage.imageRenderFormat it will assume a grayscale color space.
// This is not good because a template image should be able to tint to any color,
// so we'll just use the default here.
format = defaultFormat;
} else {
format = sourceImage.imageRendererFormat;
}
// We only want the private bits (color space and bits per component) from the image.
// We have our own ideas about opacity and scale.
format.opaque = opaque;
Expand Down
27 changes: 27 additions & 0 deletions Tests/ASImageNodeSnapshotTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ - (UIImage *)testImage
return [UIImage imageWithContentsOfFile:path];
}

- (UIImage *)testGrayscaleImage
{
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"logo-square-black"
ofType:@"png"
inDirectory:@"TestResources"];
return [UIImage imageWithContentsOfFile:path];
}

- (void)testRenderLogoSquare
{
// trivial test case to ensure ASSnapshotTestCase works
Expand Down Expand Up @@ -79,6 +87,25 @@ - (void)testTintColorOnNodePropertyAlwaysTemplate
ASSnapshotVerifyNode(node, @"blue_tint");
}

- (void)testTintColorOnGrayscaleNodePropertyAlwaysTemplate
{
ASConfiguration *config = [ASConfiguration new];
config.experimentalFeatures = ASExperimentalDrawingGlobal;
[ASConfigurationManager test_resetWithConfiguration:config];

UIImage *test = [self testGrayscaleImage];
ASImageNode *node = [[ASImageNode alloc] init];
node.image = [test imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
node.tintColor = UIColor.redColor;
ASDisplayNodeSizeToFitSize(node, test.size);
// Tint color should change view
ASSnapshotVerifyNode(node, @"red_tint");

node.tintColor = UIColor.blueColor;
// Tint color should change view
ASSnapshotVerifyNode(node, @"blue_tint");
}

- (void)testTintColorOnNodePropertyAutomatic
{
UIImage *test = [self testImage];
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/TestResources/logo-square-black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.