-
Notifications
You must be signed in to change notification settings - Fork 805
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
Changes from 1 commit
613b276
bfdf479
01b435e
b3df981
28a0a8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
enum _CGCoordinateMode : unsigned int { _kCGCoordinateModeDeviceSpace = 0, _kCGCoordinateModeUserSpace }; | ||
|
||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: just emplace_back{colorf..., location} #WontFix |
||
} | ||
|
||
|
@@ -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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
@@ -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 |
There was a problem hiding this comment.
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