-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Support for Wide Gamut (DisplayP3) Colors to React Native #1
base: main
Are you sure you want to change the base?
Changes from 15 commits
134b675
396d05e
f4539c6
ee6d760
12e3c59
88b16da
284653c
51414ea
4c81a65
c5eab07
c9bddc8
53090da
a36c9fc
7c5eeb9
4b6a0e8
53aba9b
9f30b94
f303df1
f975b6b
1b41b02
6a99355
2083fa0
f396fc3
e4b0949
506d1ab
96ade88
d3895f9
a8d00a4
e2884ad
f21a2a5
f0e1f69
d65366e
5352b6d
b5431ee
366001c
6344460
e8cee4d
d07c577
c6f8d99
b072f06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
#import "RCTParserUtils.h" | ||
#import "RCTUtils.h" | ||
|
||
#import <react/renderer/graphics/ColorComponents.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for us: this might require us to update the |
||
|
||
@implementation RCTConvert | ||
|
||
RCT_CONVERTER(id, id, self) | ||
|
@@ -439,7 +441,7 @@ + (UIKeyboardType)UIKeyboardType:(id)json RCT_DYNAMIC | |
mapping = temporaryMapping; | ||
}); | ||
|
||
UIKeyboardType type = RCTConvertEnumValue("UIKeyboardType", mapping, @(UIKeyboardTypeDefault), json).integerValue; | ||
UIKeyboardType type = (UIKeyboardType)RCTConvertEnumValue("UIKeyboardType", mapping, @(UIKeyboardTypeDefault), json).integerValue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems unrelated. I appreaciate the effort of improving the current codebase, but I'd rather keep the work here focused on wide gamut. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I was trying to avoid this as well but after converting this file to ObjC++ these were required to get this compiling again. They aren't required with just ObjC. Should I include converting this to ObjC++ as well in that separate PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, let's keep it here in this case. |
||
return type; | ||
} | ||
|
||
|
@@ -844,7 +846,7 @@ + (UIEdgeInsets)UIEdgeInsets:(id)json | |
RCTAssert([UIColor respondsToSelector:selector], @"RCTUIColor does not respond to a semantic color selector."); | ||
Class klass = [UIColor class]; | ||
IMP imp = [klass methodForSelector:selector]; | ||
id (*getSemanticColorObject)(id, SEL) = (void *)imp; | ||
id (*getSemanticColorObject)(id, SEL) = (id (*)(id, SEL))imp; | ||
id colorObject = getSemanticColorObject(klass, selector); | ||
if ([colorObject isKindOfClass:[UIColor class]]) { | ||
color = colorObject; | ||
|
@@ -878,6 +880,38 @@ + (UIEdgeInsets)UIEdgeInsets:(id)json | |
return names; | ||
} | ||
|
||
static RCTColorSpace defaultColorSpace = (RCTColorSpace)facebook::react::defaultColorSpace; | ||
RCTColorSpace RCTGetDefaultColorSpace(void) | ||
{ | ||
return (RCTColorSpace)facebook::react::defaultColorSpace; | ||
} | ||
void RCTSetDefaultColorSpace(RCTColorSpace colorSpace) | ||
{ | ||
facebook::react::setDefaultColorSpace((facebook::react::ColorSpace)colorSpace); | ||
} | ||
|
||
+ (UIColor *)createColorFrom:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)a | ||
{ | ||
RCTColorSpace space = RCTGetDefaultColorSpace(); | ||
return [self createColorFrom:r green:g blue:b alpha:a andColorSpace:space]; | ||
} | ||
+ (UIColor *)createColorFrom:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha andColorSpace:(RCTColorSpace)colorSpace | ||
{ | ||
if (colorSpace == RCTColorSpaceDisplayP3) { | ||
return [UIColor colorWithDisplayP3Red:red green:green blue:blue alpha:alpha]; | ||
} | ||
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; | ||
} | ||
|
||
+ (RCTColorSpace)colorSpaceFromString:(NSString *)colorSpace { | ||
if ([colorSpace isEqualToString:@"display-p3"]) { | ||
return RCTColorSpaceDisplayP3; | ||
} else if ([colorSpace isEqualToString:@"srgb"]) { | ||
return RCTColorSpaceSRGB; | ||
} | ||
return RCTGetDefaultColorSpace(); | ||
} | ||
|
||
+ (UIColor *)UIColor:(id)json | ||
{ | ||
if (!json) { | ||
|
@@ -886,7 +920,7 @@ + (UIColor *)UIColor:(id)json | |
if ([json isKindOfClass:[NSArray class]]) { | ||
NSArray *components = [self NSNumberArray:json]; | ||
CGFloat alpha = components.count > 3 ? [self CGFloat:components[3]] : 1.0; | ||
return [UIColor colorWithRed:[self CGFloat:components[0]] | ||
return [self createColorFrom:[self CGFloat:components[0]] | ||
green:[self CGFloat:components[1]] | ||
blue:[self CGFloat:components[2]] | ||
alpha:alpha]; | ||
|
@@ -896,11 +930,19 @@ + (UIColor *)UIColor:(id)json | |
CGFloat r = ((argb >> 16) & 0xFF) / 255.0; | ||
CGFloat g = ((argb >> 8) & 0xFF) / 255.0; | ||
CGFloat b = (argb & 0xFF) / 255.0; | ||
return [UIColor colorWithRed:r green:g blue:b alpha:a]; | ||
return [self createColorFrom:r green:g blue:b alpha:a]; | ||
} else if ([json isKindOfClass:[NSDictionary class]]) { | ||
NSDictionary *dictionary = json; | ||
id value = nil; | ||
if ((value = [dictionary objectForKey:@"semantic"])) { | ||
NSString *rawColorSpace = [dictionary objectForKey: @"space"]; | ||
if ([@[@"display-p3", @"srgb"] containsObject:rawColorSpace]) { | ||
CGFloat r = [[dictionary objectForKey:@"r"] floatValue]; | ||
CGFloat g = [[dictionary objectForKey:@"g"] floatValue]; | ||
CGFloat b = [[dictionary objectForKey:@"b"] floatValue]; | ||
CGFloat a = [[dictionary objectForKey:@"a"] floatValue]; | ||
RCTColorSpace colorSpace = [self colorSpaceFromString: rawColorSpace]; | ||
return [self createColorFrom:r green:g blue:b alpha:a andColorSpace:colorSpace]; | ||
} else if ((value = [dictionary objectForKey:@"semantic"])) { | ||
if ([value isKindOfClass:[NSString class]]) { | ||
NSString *semanticName = value; | ||
UIColor *color = [UIColor colorNamed:semanticName]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should pass the space, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cipolleschi Yeah I believe that's the better solution. Is there any value in converting back and forth from components to integer at all anymore? Or should we just create the UIColor from the color components directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I think that at this point we can keep the UIColors. I have no idea why we used to use ints for colors, but it looks a very bad API to me.