Skip to content

Instantly share code, notes, and snippets.

@ryancoughlin
Created January 27, 2025 15:10
Show Gist options
  • Save ryancoughlin/cffdf414ea49f53fbe8a8cfb187551b5 to your computer and use it in GitHub Desktop.
Save ryancoughlin/cffdf414ea49f53fbe8a8cfb187551b5 to your computer and use it in GitHub Desktop.
import UIKit
import MapboxMaps
class SSTColorUtil {
private static let colors = DatasetType.sst.config.colorScale
static func getContourLineExpression(min minTemp: Double, max maxTemp: Double) -> Exp {
let range = maxTemp - minTemp
var stops: [Exp.Argument] = []
for (index, color) in colors.enumerated() {
let temp = minTemp + (range * Double(index) / Double(colors.count - 1))
stops.append(.number(temp))
stops.append(.string(color))
}
return Exp(operator: .interpolate, arguments: [
.expression(Exp(operator: .linear, arguments: [])),
.expression(Exp(operator: .get, arguments: [.string("temperature")])),
] + stops)
}
static func getLineWidth(isHovered: Bool = false, strength: String? = nil) -> Double {
if isHovered { return 5.0 }
switch strength {
case "strong": return 2.0
case "moderate": return 1.0
default: return 0.75
}
}
}
// Helper for string chunking
extension StringProtocol {
func chunks(ofCount count: Int) -> [SubSequence] {
var chunks: [SubSequence] = []
var i = startIndex
while i < endIndex {
let j = index(i, offsetBy: count, limitedBy: endIndex) ?? endIndex
chunks.append(self[i..<j])
i = j
}
return chunks
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment