Skip to content

Commit

Permalink
Allow full color tinting on grayscale template images. (#1629)
Browse files Browse the repository at this point in the history
* Allow full color tinting on grayscale template images.

* Signing a commit with the correct email address.

* Add snapshot test for a grayscale image with ASExperimentalDrawingGlobal enabled.
  • Loading branch information
jessietea authored and rahul-malik committed Aug 22, 2019
1 parent 28be9bb commit ccee566
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
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
// 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.

0 comments on commit ccee566

Please sign in to comment.