blob: 68e77f5b1010a01d276d916eefb8a5d1dacdff5c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#import "UIColor+MGLAdditions.h"
@implementation UIColor (MGLAdditions)
- (mbgl::Color)mgl_color
{
CGFloat r, g, b, a;
[self getRed:&r green:&g blue:&b alpha:&a];
// UIColor provides non-premultiplied color components, so we have to premultiply each
// color component with the alpha value to transform it into a valid
// mbgl::Color which expects premultiplied color components.
return { static_cast<float>(r*a), static_cast<float>(g*a), static_cast<float>(b*a), static_cast<float>(a) };
}
- (mbgl::style::PropertyValue<mbgl::Color>)mgl_colorPropertyValue
{
mbgl::Color color = self.mgl_color;
return {{ color.r, color.g, color.b, color.a }};
}
+ (UIColor *)mgl_colorWithColor:(mbgl::Color)color
{
// If there is no alpha value, return original color values.
if (color.a == 0.0f) {
return [UIColor colorWithRed:color.r green:color.g blue:color.b alpha:color.a];
}
// mbgl::Color provides premultiplied color components, so we have to convert color
// components to non-premultiplied values to return a valid UIColor object.
float red = static_cast<float>((color.r / color.a));
float green = static_cast<float>((color.g / color.a));
float blue = static_cast<float>((color.b / color.a));
return [UIColor colorWithRed:red green:green blue:blue alpha:color.a];
}
@end
@implementation NSExpression (MGLColorAdditions)
+ (NSExpression *)mgl_expressionForRGBComponents:(NSArray<NSExpression *> *)components {
if (UIColor *color = [self mgl_colorWithRGBComponents:components]) {
return [NSExpression expressionForConstantValue:color];
}
NSExpression *color = [NSExpression expressionForConstantValue:[UIColor class]];
NSExpression *alpha = [NSExpression expressionForConstantValue:@1.0];
return [NSExpression expressionForFunction:color
selectorName:@"colorWithRed:green:blue:alpha:"
arguments:[components arrayByAddingObject:alpha]];
}
+ (NSExpression *)mgl_expressionForRGBAComponents:(NSArray<NSExpression *> *)components {
if (UIColor *color = [self mgl_colorWithRGBComponents:components]) {
return [NSExpression expressionForConstantValue:color];
}
NSExpression *color = [NSExpression expressionForConstantValue:[UIColor class]];
return [NSExpression expressionForFunction:color
selectorName:@"colorWithRed:green:blue:alpha:"
arguments:components];
}
+ (UIColor *)mgl_colorWithRGBComponents:(NSArray<NSExpression *> *)components {
if (components.count < 3 || components.count > 4) {
return nil;
}
for (NSExpression *component in components) {
if (component.expressionType != NSConstantValueExpressionType) {
return nil;
}
NSNumber *number = (NSNumber *)component.constantValue;
if (![number isKindOfClass:[NSNumber class]]) {
return nil;
}
}
return [UIColor colorWithRed:[components[0].constantValue doubleValue] / 255.0
green:[components[1].constantValue doubleValue] / 255.0
blue:[components[2].constantValue doubleValue] / 255.0
alpha:components.count == 3 ? 1.0 : [components[3].constantValue doubleValue]];
}
@end
|