Skip to content

Commit

Permalink
Remove opacity and use alpha component
Browse files Browse the repository at this point in the history
  • Loading branch information
frederoni committed Apr 20, 2017
1 parent bcdd2cf commit 965daaf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
13 changes: 13 additions & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,25 @@ class ViewController: UIViewController {

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

let coordinates = [CLLocationCoordinate2D(latitude: 45.4562, longitude: -122.8793),
CLLocationCoordinate2D(latitude: 45.4562, longitude: -122.4645),
CLLocationCoordinate2D(latitude: 45.6582, longitude: -122.4645),
CLLocationCoordinate2D(latitude: 45.6582, longitude: -122.8793),
CLLocationCoordinate2D(latitude: 45.4562, longitude: -122.8793)]

let path = Path(coordinates: coordinates)
path.fillColor = UIColor.red.withAlphaComponent(0.5)
path.strokeColor = UIColor.green.withAlphaComponent(0.5)

let camera = SnapshotCamera(lookingAtCenter: CLLocationCoordinate2D(latitude: 45, longitude: -122), zoomLevel: 6)
let options = SnapshotOptions(
styleURL: URL(string: "mapbox://styles/mapbox/streets-v9")!,
camera: camera,
size: imageView.bounds.size)

options.overlays.append(path)

_ = Snapshot(options: options, accessToken: accessToken).image { [weak self] (image, error) in
if let error = error {
print(error)
Expand Down
13 changes: 13 additions & 0 deletions MapboxStatic/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ internal extension Color {

return NSString(format: "%02x%02x%02x", Int(r), Int(g), Int(b)) as String
}

#if !os(macOS)
internal var alphaComponent: CGFloat {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0

getRed(&r, green: &g, blue: &b, alpha: &a)

return a
}
#endif

convenience init(hexString: String) {
var hexString = hexString.replacingOccurrences(of: "#", with: "")
Expand Down
20 changes: 3 additions & 17 deletions MapboxStatic/Overlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,20 +328,6 @@ open class Path: NSObject, Overlay {
open var fillColor = UIColor(hexString: "555")
#endif

/**
The stroke opacity of the overlay, expressed as a percentage such that 0.0 is completely transparent and 1.0 is completely opaque.

By default, the overlay’s stroke is completely opaque.
*/
open var strokeOpacity: Double = 1

/**
The fill opacity of the overlay, expressed as a percentage such that 0.0 is completely transparent and 1.0 is completely opaque.

By default, the overlay’s fill is completely transparent.
*/
open var fillOpacity: Double = 0

/**
Initializes a polyline overlay with the given vertices.

Expand Down Expand Up @@ -437,9 +423,9 @@ open class Path: NSObject, Overlay {

open override var description: String {
let encodedPolyline = polylineEncode(coordinates).addingPercentEncoding(withAllowedCharacters: allowedCharacterSet)!
var description = "path-\(strokeWidth)+\(strokeColor.toHexString())-\(strokeOpacity)"
if fillOpacity > 0 {
description += "+\(fillColor.toHexString())-\(fillOpacity)"
var description = "path-\(strokeWidth)+\(strokeColor.toHexString())-\(strokeColor.alphaComponent)"
if fillColor.alphaComponent > 0 {
description += "+\(fillColor.toHexString())-\(fillColor.alphaComponent)"
}
description += "(\(encodedPolyline))"
return description
Expand Down
6 changes: 2 additions & 4 deletions MapboxStaticTests/ClassicOverlayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,8 @@ class ClassicOverlayTests: XCTestCase {
)
])
path.strokeWidth = 2
path.strokeColor = .black
path.strokeOpacity = 0.75
path.fillColor = .red
path.fillOpacity = 0.25
path.strokeColor = Color.black.withAlphaComponent(0.75)
path.fillColor = Color.red.withAlphaComponent(0.25)

let options = ClassicSnapshotOptions(
mapIdentifiers: ["mapbox.streets"],
Expand Down

0 comments on commit 965daaf

Please sign in to comment.