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

Turned gradient components and locations into constants #3775

Merged
merged 1 commit into from
Dec 25, 2018
Merged
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
44 changes: 16 additions & 28 deletions Source/Charts/Renderers/LineChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -836,40 +836,28 @@ open class LineChartRenderer: LineRadarRenderer

let gradientStart = CGPoint(x: 0, y: boundingBox.minY)
let gradientEnd = CGPoint(x: 0, y: boundingBox.maxY)
var gradientColorComponents: [CGFloat] = []
var gradientLocations: [CGFloat] = []

for position in gradientPositions.reversed()
{
let location = CGPoint(x: boundingBox.minX, y: position)
.applying(matrix)
let normalizedLocation =
(location.y - boundingBox.minY) / (boundingBox.maxY - boundingBox.minY)
switch normalizedLocation {
case ..<0:
gradientLocations.append(0)
case 0..<1:
gradientLocations.append(normalizedLocation)
case 1...:
gradientLocations.append(1)
default:
assertionFailure()
}
let gradientColorComponents: [CGFloat] = dataSet.colors
.reversed()
.reduce(into: []) { (components, color) in
guard let (r, g, b, a) = color.nsuirgba else {
return
}
components += [r, g, b, a]
}

for color in dataSet.colors.reversed()
{
guard let (r, g, b, a) = color.nsuirgba else {
continue
}
gradientColorComponents += [r, g, b, a]
let gradientLocations: [CGFloat] = gradientPositions.reversed()
.map { (position) in
let location = CGPoint(x: boundingBox.minX, y: position)
.applying(matrix)
let normalizedLocation = (location.y - boundingBox.minY)
/ (boundingBox.maxY - boundingBox.minY)
return normalizedLocation.clamped(to: 0...1)
}

let baseColorSpace = CGColorSpaceCreateDeviceRGB()
guard let gradient = CGGradient(
colorSpace: baseColorSpace,
colorComponents: &gradientColorComponents,
locations: &gradientLocations,
colorComponents: gradientColorComponents,
locations: gradientLocations,
count: gradientLocations.count) else {
return
}
Expand Down