Skip to content

Commit a8e00dc

Browse files
committed
Added ColorMap, and some popular maps from matplotlib (which their license appears to allow - "derivative works" only need to mention how they changed matplotlib, which I did)
1 parent 3cdffbb commit a8e00dc

File tree

4 files changed

+1892
-15
lines changed

4 files changed

+1892
-15
lines changed

Sources/SwiftPlot/ColorMap.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
// This struct is a namespace for static functions and properties which return known color maps.
3+
// All of these functions and properties also _return_ a `ColorMap`,
4+
// instances of which are used as an interface type
5+
// rather than existentials of `ColorMapProtocol` directly
6+
// (again, so users can have access to convenient, known color maps).
7+
8+
public struct ColorMap: ColorMapProtocol {
9+
var base: ColorMapProtocol
10+
11+
public init(_ base: ColorMapProtocol) {
12+
if let existing = base as? ColorMap { self = existing }
13+
else { self.base = base }
14+
}
15+
16+
public func colorForOffset(_ offset: Float) -> Color {
17+
return base.colorForOffset(offset)
18+
}
19+
}
20+
21+
public protocol ColorMapProtocol {
22+
func colorForOffset(_ offset: Float) -> Color
23+
}
24+
25+
// Linear Gradients.
26+
27+
struct LinearGradient: ColorMapProtocol {
28+
var start: Color
29+
var end: Color
30+
func colorForOffset(_ offset: Float) -> Color {
31+
return lerp(startColor: start, endColor: end, offset)
32+
}
33+
}
34+
35+
extension ColorMap {
36+
public static func linear(_ start: Color, _ end: Color) -> ColorMap {
37+
return ColorMap(LinearGradient(start: start, end: end))
38+
}
39+
}

0 commit comments

Comments
 (0)