diff options
author | Neil <nyamatongwe@gmail.com> | 2018-05-22 09:08:54 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2018-05-22 09:08:54 +1000 |
commit | 2e2e70c7ee89b353ad0e6ef409cb9621729d72f8 (patch) | |
tree | d5899867d76d457bda2c48dd298b6f41a0217ad1 /cocoa/PlatCocoa.mm | |
parent | cc5e63013d7aa447a74fafba0ee4dd560b6952c7 (diff) | |
download | scintilla-mirror-2e2e70c7ee89b353ad0e6ef409cb9621729d72f8.tar.gz |
Add GradientRectangle method to Surface to draw rectangles with vertical or
horizontal gradients.
Diffstat (limited to 'cocoa/PlatCocoa.mm')
-rw-r--r-- | cocoa/PlatCocoa.mm | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/cocoa/PlatCocoa.mm b/cocoa/PlatCocoa.mm index 0e1e1544c..7d7eafb42 100644 --- a/cocoa/PlatCocoa.mm +++ b/cocoa/PlatCocoa.mm @@ -641,6 +641,50 @@ void Scintilla::SurfaceImpl::AlphaRectangle(PRectangle rc, int cornerSize, Colou } } +void Scintilla::SurfaceImpl::GradientRectangle(PRectangle rc, const std::vector<ColourStop> &stops, GradientOptions options) { + if (!gc) { + return; + } + + CGPoint ptStart = CGPointMake(rc.left, rc.top); + CGPoint ptEnd = CGPointMake(rc.left, rc.bottom); + if (options == GradientOptions::leftToRight) { + ptEnd = CGPointMake(rc.right, rc.top); + } + + std::vector<CGFloat> components; + std::vector<CGFloat> locations; + for (const ColourStop &stop : stops) { + locations.push_back(stop.position); + components.push_back(stop.colour.GetRedComponent()); + components.push_back(stop.colour.GetGreenComponent()); + components.push_back(stop.colour.GetBlueComponent()); + components.push_back(stop.colour.GetAlphaComponent()); + } + + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + if (!colorSpace) { + return; + } + + CGGradientRef gradiantRef = CGGradientCreateWithColorComponents(colorSpace, + components.data(), + locations.data(), + locations.size()); + if (gradiantRef) { + CGContextSaveGState(gc); + CGRect rect = PRectangleToCGRect(rc); + CGContextClipToRect(gc, rect); + CGContextBeginPath(gc); + CGContextAddRect(gc, rect); + CGContextClosePath(gc); + CGContextDrawLinearGradient(gc, gradiantRef, ptStart, ptEnd, 0); + CGGradientRelease(gradiantRef); + CGContextRestoreGState(gc); + } + CGColorSpaceRelease(colorSpace); +} + static void ProviderReleaseData(void *, const void *data, size_t) { const unsigned char *pixels = static_cast<const unsigned char *>(data); delete []pixels; |