Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Pattern colors as polygon fill-pattern/line-pattern images #5033

Open
ojanes opened this issue May 13, 2016 · 7 comments
Open

Pattern colors as polygon fill-pattern/line-pattern images #5033

ojanes opened this issue May 13, 2016 · 7 comments
Labels
feature iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS
Milestone

Comments

@ojanes
Copy link

ojanes commented May 13, 2016

Hello, I am working on a project where I have to paint polygons with custom image patterns and I can't figure out how to do it with GL version (3.2.2). It could be nice to be able to use the UIColor class

func mapView(mapView: MGLMapView, fillColorForPolygonAnnotation annotation: MGLPolygon) -> UIColor {
     let image = UIImage(named:"pattern")!
     return UIColor(patternImage: image)
}
@1ec5 1ec5 changed the title Image patterns to fill polygons Pattern colors as polygon fill-pattern images May 13, 2016
@1ec5 1ec5 added feature iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS labels May 13, 2016
@1ec5
Copy link
Contributor

1ec5 commented May 13, 2016

Cool idea. The image pattern effect is controlled by the fill-pattern paint property in core mbgl. What we’d need is a way to get the image’s pixel data out of the color. This is also blocked by #837, which will open the door to a lot more style customization at runtime.

@1ec5
Copy link
Contributor

1ec5 commented May 13, 2016

We’d need to get a CGColorRef via -[UIImage CGColor], then get a CGPatternRef using CGColorGetPattern(), then draw the image into a context using Core Graphics.

@1ec5
Copy link
Contributor

1ec5 commented Oct 14, 2016

Now that the runtime styling API has been implemented, when MGLFillStyleLayer.fillColor is set to a pattern color, that property would need to install the image using -[MGLStyle setImage:forName:] and set MGLFillStyleLayer.fillPattern by side effect.

@1ec5
Copy link
Contributor

1ec5 commented Oct 14, 2016

In the meantime, you can call -[MGLStyle setImage:forName:] and set fillPattern on any MGLFillStyleLayer you’ve added to your style. We don’t support applying these effects to annotations – #1734 (comment) – but it sounds like the runtime styling API should be sufficient for your use case.

@1ec5 1ec5 changed the title Pattern colors as polygon fill-pattern images Pattern colors as polygon fill-pattern/line-pattern images Jan 24, 2017
@1ec5 1ec5 added this to the ios-v3.5.0 milestone Jan 24, 2017
@1ec5 1ec5 modified the milestones: ios-v3.5.0, ios-v3.6.0 Mar 9, 2017
@boundsj boundsj modified the milestones: ios-future, ios-v3.6.0 Mar 31, 2017
@stale stale bot added the archived Archived because of inactivity label Nov 10, 2018
@stale
Copy link

stale bot commented Nov 26, 2018

This issue has been automatically detected as stale because it has not had recent activity and will be archived. Thank you for your contributions.

@stale stale bot closed this as completed Nov 26, 2018
@samfader samfader reopened this Mar 16, 2020
@stale stale bot removed the archived Archived because of inactivity label Mar 16, 2020
@samfader
Copy link
Contributor

@knov @chloekraw Wanted to put this back on your radar. Would the best next step be moving it to https://github.com/mapbox/mapbox-gl-native-ios/issuess?

@1ec5
Copy link
Contributor

1ec5 commented Mar 16, 2020

Yes, I think it makes sense to track this issue in mapbox-gl-native-ios. Pattern colors are an iOS/macOS concept and I think the translation to a pattern image would have to happen in platform-specific code.

The workaround in #5033 (comment) remains relevant. If we were to implement support for pattern colors as a convenience, it would look something like this:

  • -[MGLFillStyleLayer setFillColor:] (which is currently generated by a script) would intercept the fillColor expression and iterate through its arguments (or evaluate its constant value):
    • If any argument is a pattern-based NSColor or UIColor, replace the argument with clearColor.
  • -[MGLFillStyleLayer setFillColor:] would also call -[MGLFillStyleLayer setFillPattern:], which would iterate through the expression’s arguments:
    • If any argument is a pattern-based NSColor or UIColor:
      • Call -[MGLStyle setImage:forName:], generating an image name by hashing the image data.
      • Replace the argument with the image name as a string.
    • Otherwise, replace the argument with a NIL expression.

You’d then be able to set the fill color to a pattern color, like this:

UIImage *crosshatchImage = [UIImage imageNamed:@"crosshatch"];
UIColor *crosshatchPattern = [UIColor colorWithPatternImage:crosshatchImage];
fillLayer.fillColor = [NSExpression expressionWithFormat:@"MGL_MATCH(saturation, 0, %@, 1, %@, %@)", UIColor.blackColor, UIColor.whiteColor, crosshatchPattern];

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS
Projects
None yet
Development

No branches or pull requests

4 participants