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

Implementation of Linear Gradient #1535

Merged
merged 5 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 4 additions & 6 deletions Frameworks/CoreGraphics/CGContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
static const wchar_t* TAG = L"CGContext";

// Coordinate offset to support CGGradientDrawingOptions
static const float s_CGGradientOffsetPoint = 1E-45;
static const float s_kCGGradientOffsetPoint = 1E-45;
Copy link
Contributor

@aballway aballway Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that works too XD #ByDesign


enum _CGCoordinateMode : unsigned int { _kCGCoordinateModeDeviceSpace = 0, _kCGCoordinateModeUserSpace };

Expand Down Expand Up @@ -2366,9 +2366,7 @@ void CGContextDrawTiledImage(CGContextRef context, CGRect rect, CGImageRef image
static inline void __CGGradientInsertTransparentColor(std::vector<D2D1_GRADIENT_STOP>& gradientStops, int location, float position) {
Copy link

@DHowett-MSFT DHowett-MSFT Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not appear to insert them in order. Are we required to do that for D2D's sake? #ByDesign

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested it and d2d uses the position rather than the order as it seems


In reply to: 93370134 [](ancestors = 93370134)

gradientStops[location].position = position;
// set the edge location to be transparent
D2D1_GRADIENT_STOP transparent;
transparent.color = D2D1::ColorF(0, 0, 0, 0);
transparent.position = location;
D2D1_GRADIENT_STOP transparent = { location, D2D1::ColorF(0, 0, 0, 0) };
gradientStops.push_back(transparent);
Copy link
Contributor

@aballway aballway Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just emplace_back{colorf..., location} #WontFix

}

Expand Down Expand Up @@ -2398,11 +2396,11 @@ static inline void __CGGradientInsertTransparentColor(std::vector<D2D1_GRADIENT_
// that d2d will automatically extend the transparent color, thus we obtain the desired effect for CGGradientDrawingOptions.

if (!(options & kCGGradientDrawsBeforeStartLocation)) {
__CGGradientInsertTransparentColor(gradientStops, 0, s_CGGradientOffsetPoint);
__CGGradientInsertTransparentColor(gradientStops, 0, s_kCGGradientOffsetPoint);
}

if (!(options & kCGGradientDrawsAfterEndLocation)) {
__CGGradientInsertTransparentColor(gradientStops, 1, 1 - s_CGGradientOffsetPoint);
__CGGradientInsertTransparentColor(gradientStops, 1, 1.f - s_kCGGradientOffsetPoint);
}

return gradientStops;
Expand Down
2 changes: 1 addition & 1 deletion Frameworks/CoreGraphics/CGGradient.mm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//******************************************************************************
//
// Copyright (c) 2016 Intel Corporation. All rights reserved.
// Copyright (c) 2016 Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
#include "DrawingTest.h"

#pragma region LinearGradient
DRAW_TEST_F(CGGradient, LinearGradient, UIKitMimicTest) {
CGContextRef context = GetDrawingContext();

CGFloat locations[2] = { 0, 1 };
CGFloat components[8] = { 0.0, 0.0, 1, 1.0, 1.0, 0, 0, 1.0 };
static void _drawLinearGradient(CGContextRef context,
CGPoint startPoint,
CGPoint endPoint,
CGFloat* components,
Copy link
Contributor

@aballway aballway Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass this in as an array rather than a pointer, and you can just use std::extent on it rather than passing in count #Resolved

CGFloat* locations,
size_t count,
CGGradientDrawingOptions options) {
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 2);

CGPoint startPoint = CGPointMake(0, 0);
CGPoint endPoint = CGPointMake(512, 1024);
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, count);

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, options);
CFRelease(colorspace);
CFRelease(gradient);
}
Expand All @@ -37,15 +37,27 @@ static void _drawShortLinearGradientWithOptions(CGContextRef context, CGRect bou
CGFloat locations[2] = { 0, 1 };
CGFloat components[8] = { 0.0, 1, 0.0, 1.0, 1.0, 0, 0, 1.0 };

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 2);

CGRect borderRect = CGRectInset(bounds, 30, 50);

CGContextDrawLinearGradient(context, gradient, borderRect.origin, CGPointMake(borderRect.size.width, borderRect.size.height), option);
_drawLinearGradient(context,
borderRect.origin,
CGPointMake(borderRect.size.width, borderRect.size.height),
components,
locations,
_countof(locations),
Copy link
Contributor

@aballway aballway Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use extent instead of _countof which is MS only and shouldn't work on reference platform. #Resolved

Copy link
Contributor Author

@msft-Jeyaram msft-Jeyaram Dec 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

darn i forgot about that #Resolved

option);
}

CFRelease(colorspace);
CFRelease(gradient);
DRAW_TEST_F(CGGradient, LinearGradient, UIKitMimicTest) {
CGFloat locations[2] = { 0, 1 };
CGFloat components[8] = { 0.0, 0.0, 1, 1.0, 1.0, 0, 0, 1.0 };
_drawLinearGradient(GetDrawingContext(),
CGPointMake(0, 0),
CGPointMake(512, 1024),
components,
locations,
_countof(locations),
kCGGradientDrawsBeforeStartLocation);
}

DRAW_TEST_F(CGGradient, LinearGradientShortBothSides_Options_0, UIKitMimicTest) {
Expand All @@ -61,135 +73,106 @@ DRAW_TEST_F(CGGradient, LinearGradientShortBothSides_Options_kCGGradientDrawsAft
}

DRAW_TEST_F(CGGradient, LinearGradientInvalidCount, UIKitMimicTest) {
CGContextRef context = GetDrawingContext();

CGFloat locations[] = { 0.0 };

CGFloat components[] = {
0.85, 0, 0, 1.0,
};

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 0);
CGPoint startPoint = CGPointMake(0, 0);
CGPoint endPoint = CGPointMake(512, 1024);

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
CFRelease(colorspace);
CFRelease(gradient);
_drawLinearGradient(
GetDrawingContext(), CGPointMake(0, 0), CGPointMake(512, 1024), components, locations, 0, kCGGradientDrawsBeforeStartLocation);
}

DRAW_TEST_F(CGGradient, LinearGradient2, UIKitMimicTest) {
CGContextRef context = GetDrawingContext();

CGFloat locations[] = { 0.0, 0.33, 0.66, 1.0 };

CGFloat components[] = {
0.85, 0, 0, 1.0, 1, 0, 0, 1.0, 0.85, 0.3, 0, 1.0, 0.1, 0, 0.9, 1.0,
};

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 4);
CGPoint startPoint = CGPointMake(0, 0);
CGPoint endPoint = CGPointMake(512, 1024);

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
CFRelease(colorspace);
CFRelease(gradient);
_drawLinearGradient(GetDrawingContext(),
CGPointMake(0, 0),
CGPointMake(512, 1024),
components,
locations,
_countof(locations),
kCGGradientDrawsBeforeStartLocation);
}

DRAW_TEST_F(CGGradient, LinearGradient2Short, UIKitMimicTest) {
CGContextRef context = GetDrawingContext();

CGFloat locations[] = { 0.0, 0.33, 1.0 };

CGFloat components[] = { 0.85, 0, 0, 1.0, 1, 0, 0, 1.0, 0.85, 0.3, 0, 1.0 };

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 3);
CGPoint startPoint = CGPointMake(120, 200);
CGPoint endPoint = CGPointMake(350, 800);

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
CFRelease(colorspace);
CFRelease(gradient);
_drawLinearGradient(GetDrawingContext(),
CGPointMake(120, 200),
CGPointMake(350, 800),
components,
locations,
_countof(locations),
kCGGradientDrawsBeforeStartLocation);
}

DRAW_TEST_F(CGGradient, LinearGradient3, UIKitMimicTest) {
CGContextRef context = GetDrawingContext();

CGFloat locations[] = { 0.0, 0.5, 1 };

CGFloat components[] = {
1, 0, 0, 1.0, 0, 1, 0, 1.0, 0, 0, 1, 1.0,
};

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 3);
CGPoint startPoint = CGPointMake(0, 0);
CGPoint endPoint = CGPointMake(512, 1024);

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
CFRelease(colorspace);
CFRelease(gradient);
_drawLinearGradient(GetDrawingContext(),
CGPointMake(0, 0),
CGPointMake(512, 1024),
components,
locations,
_countof(locations),
kCGGradientDrawsBeforeStartLocation);
}

DRAW_TEST_F(CGGradient, LinearGradientWithLowOpacity, UIKitMimicTest) {
CGContextRef context = GetDrawingContext();

CGFloat locations[] = { 0.0, 0.5, 1 };

CGFloat components[] = {
1, 0, 0, 0.1, 0, 1, 0, 0.9, 0, 0, 1, 0.8,
};

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 3);
CGPoint startPoint = CGPointMake(300, 750);
CGPoint endPoint = CGPointMake(0, 0);

CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
CFRelease(colorspace);
CFRelease(gradient);
_drawLinearGradient(GetDrawingContext(),
CGPointMake(300, 750),
CGPointMake(0, 0),
components,
locations,
_countof(locations),
kCGGradientDrawsBeforeStartLocation);
}

DRAW_TEST_F(CGGradient, LinearGradientWithAlpha, UIKitMimicTest) {
CGContextRef context = GetDrawingContext();

CGFloat locations[] = { 0.0, 0.25, 0.5, 0.6, 0.8, 0.9, 1 };

CGFloat components[] = {
1, 0, 0, 1, 0.4, 0.1, 0.5, 1, 0.50, 0.2, 0.99, 1, 0.41, 0.56, 0, 1, 0.12, 0.12, .3, 1, 0.9, 0.4, 1, 1, 0.2, 0.3, 0.8, 1,
};

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 7);
CGPoint startPoint = CGPointMake(512, 1024);
CGPoint endPoint = CGPointMake(0, 0);

CGContextSetAlpha(context, 0.75);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
CFRelease(colorspace);
CFRelease(gradient);
CGContextSetAlpha(GetDrawingContext(), 0.75);
_drawLinearGradient(GetDrawingContext(),
CGPointMake(0, 0),
CGPointMake(512, 1024),
components,
locations,
_countof(locations),
kCGGradientDrawsBeforeStartLocation);
}

DRAW_TEST_F(CGGradient, LinearGradientWithLowOpacityShort, UIKitMimicTest) {
CGContextRef context = GetDrawingContext();

CGFloat locations[] = { 0.0, 0.5, 1 };

CGFloat components[] = {
1, 0, 0, 0.8, 0, 1, 0, 0.9, 0, 0, 1, 0.1,
};

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, 3);
CGPoint startPoint = CGPointMake(250, 300);
CGPoint endPoint = CGPointMake(0, 0);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsAfterEndLocation);

CFRelease(colorspace);
CFRelease(gradient);
_drawLinearGradient(GetDrawingContext(),
CGPointMake(250, 300),
CGPointMake(0, 0),
components,
locations,
_countof(locations),
kCGGradientDrawsAfterEndLocation);
}

#pragma endregion LinearGradient