From a662befd6526bc9460a71dc9fea94cf3c0ccf1a0 Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 17 Sep 2024 04:14:18 +0800 Subject: [PATCH] Add Color.Resolved kitColor implementation (#115) * FIx COpenSwiftUI compile issue * Add CoreGraphicsContext and update CoreColor class * Add Color.Resolved kitColor implementation * Fix macOS selector crash issue --- .../Overlay/CoreColor/CoreColor.m | 90 ------- ...tocolDescriptor.c => ProtocolDescriptor.m} | 2 + Sources/COpenSwiftUI/include/CoreColor.h | 32 --- .../Other}/LockedPointer.c | 0 .../Other}/MovableLock.c | 0 .../Other}/TLS.c | 0 .../Overlay/CoreGraphics/CoreColor.m | 239 ++++++++++++++++++ .../CoreGraphics/CoreGraphicsContext.m | 74 ++++++ .../CoreGraphics/__CGPathParseString.m | 2 +- Sources/COpenSwiftUICore/README.md | 3 + Sources/COpenSwiftUICore/include/CoreColor.h | 62 +++++ .../include/CoreGraphicsContext.h | 33 +++ .../include/LockedPointer.h | 0 .../include/MovableLock.h | 0 .../include/OpenSwiftUI_SPI.h | 0 .../include/TLS.h | 0 .../include/__CGPathParseString.h | 0 .../View/Graphic/Color/ColorResolved.swift | 9 + .../Graphic/Color/CoreColor+Extension.swift | 73 ++++++ .../Graphics/Color/ColorResolvedTests.swift | 19 ++ 20 files changed, 515 insertions(+), 123 deletions(-) delete mode 100644 Sources/COpenSwiftUI/Overlay/CoreColor/CoreColor.m rename Sources/COpenSwiftUI/{ProtocolDescriptor.c => ProtocolDescriptor.m} (82%) delete mode 100644 Sources/COpenSwiftUI/include/CoreColor.h rename Sources/{COpenSwiftUI => COpenSwiftUICore/Other}/LockedPointer.c (100%) rename Sources/{COpenSwiftUI => COpenSwiftUICore/Other}/MovableLock.c (100%) rename Sources/{COpenSwiftUI => COpenSwiftUICore/Other}/TLS.c (100%) create mode 100644 Sources/COpenSwiftUICore/Overlay/CoreGraphics/CoreColor.m create mode 100644 Sources/COpenSwiftUICore/Overlay/CoreGraphics/CoreGraphicsContext.m rename Sources/{COpenSwiftUI => COpenSwiftUICore}/Overlay/CoreGraphics/__CGPathParseString.m (85%) create mode 100644 Sources/COpenSwiftUICore/README.md create mode 100644 Sources/COpenSwiftUICore/include/CoreColor.h create mode 100644 Sources/COpenSwiftUICore/include/CoreGraphicsContext.h rename Sources/{COpenSwiftUI => COpenSwiftUICore}/include/LockedPointer.h (100%) rename Sources/{COpenSwiftUI => COpenSwiftUICore}/include/MovableLock.h (100%) rename Sources/{COpenSwiftUI => COpenSwiftUICore}/include/OpenSwiftUI_SPI.h (100%) rename Sources/{COpenSwiftUI => COpenSwiftUICore}/include/TLS.h (100%) rename Sources/{COpenSwiftUI => COpenSwiftUICore}/include/__CGPathParseString.h (100%) create mode 100644 Sources/OpenSwiftUI/View/Graphic/Color/CoreColor+Extension.swift create mode 100644 Tests/OpenSwiftUITests/View/Graphics/Color/ColorResolvedTests.swift diff --git a/Sources/COpenSwiftUI/Overlay/CoreColor/CoreColor.m b/Sources/COpenSwiftUI/Overlay/CoreColor/CoreColor.m deleted file mode 100644 index b77dddec..00000000 --- a/Sources/COpenSwiftUI/Overlay/CoreColor/CoreColor.m +++ /dev/null @@ -1,90 +0,0 @@ -// -// CoreColor.h -// COpenSwiftUI -// Audited for RELEASE_2024 -// Status: Complete - -#import "CoreColor.h" - -#if OPENSWIFTUI_TARGET_OS_DARWIN - -Class CoreColorClass(BOOL isAppKitBased); - -#if OPENSWIFTUI_TARGET_OS_OSX -id NSColorSpaceForCGColorSpace(CGColorSpaceRef cgColorSpace); -Class NSColorSpaceClass(void); -#endif - -BOOL CoreColorPlatformColorGetComponents(BOOL isAppKitBased, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha) { - if (!color) { - return NO; - } - Class colorClass = CoreColorClass(isAppKitBased); - if (colorClass) { - #if OPENSWIFTUI_TARGET_OS_OSX - if (isAppKitBased) { - id colorSpace = - NSColorSpaceForCGColorSpace(CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB)); - NSColor *nameSpaceColor = [color colorUsingColorSpace:colorSpace]; - if (nameSpaceColor) { - [nameSpaceColor getRed:red green:green blue: blue alpha: alpha]; - return YES; - } else { - return NO; - } - } - #endif - return ((BOOL (*)(id, SEL))[color methodForSelector:@selector(getRed:green:blue:alpha:)])(color, @selector(getRed:green:blue:alpha:)); - } else { - return NO; - } -} - -Class CoreColorGetKitColorClass(BOOL isAppKitBased) { - CoreColorClass(isAppKitBased); -} - -Class CoreColorClass(BOOL isAppKitBased) { - static BOOL isValid = false; - static Class colorClass; - static dispatch_once_t once; - dispatch_once(&once, ^{ - if (isAppKitBased) { - Class class = NSClassFromString(@"NSColor"); - colorClass = class; - isValid = class != nil; - } else { - Class class = NSClassFromString(@"UIColor"); - colorClass = class; - isValid = class != nil; - } - }); - if (isValid) { - return colorClass; - } else { - [NSException raise:@"Invalid core color" format:@""]; - } -} - -#if OPENSWIFTUI_TARGET_OS_OSX -id NSColorSpaceForCGColorSpace(CGColorSpaceRef cgColorSpace) { - Class colorSpaceClass = NSColorSpaceClass(); - if (colorSpaceClass) { - return [[colorSpaceClass alloc] initWithCGColorSpace:cgColorSpace]; - } else { - return nil; - } -} - -OPENSWIFTUI_INLINE -Class NSColorSpaceClass(void) { - static Class colorSpaceClass; - static dispatch_once_t once; - dispatch_once(&once, ^{ - colorSpaceClass = NSClassFromString(@"NSColorSpace"); - }); - return colorSpaceClass; -} -#endif - -#endif diff --git a/Sources/COpenSwiftUI/ProtocolDescriptor.c b/Sources/COpenSwiftUI/ProtocolDescriptor.m similarity index 82% rename from Sources/COpenSwiftUI/ProtocolDescriptor.c rename to Sources/COpenSwiftUI/ProtocolDescriptor.m index a59102d1..e9e35170 100644 --- a/Sources/COpenSwiftUI/ProtocolDescriptor.c +++ b/Sources/COpenSwiftUI/ProtocolDescriptor.m @@ -1,5 +1,7 @@ // // ProtocolDescriptor.c +// FIXME: Use ProtocolDescriptor.m due to COpenSwiftUICore module contains ObjC symbol. +// Solution: Move down to COpenSwiftUICore module. // COpenSwiftUI diff --git a/Sources/COpenSwiftUI/include/CoreColor.h b/Sources/COpenSwiftUI/include/CoreColor.h deleted file mode 100644 index bfc017b8..00000000 --- a/Sources/COpenSwiftUI/include/CoreColor.h +++ /dev/null @@ -1,32 +0,0 @@ -// -// CoreColor.h -// COpenSwiftUI -// Audited for RELEASE_2024 -// Status: Complete - -#ifndef CoreColor_h -#define CoreColor_h - -#include "OpenSwiftUIBase.h" - -#if OPENSWIFTUI_TARGET_OS_DARWIN - -#if OPENSWIFTUI_TARGET_OS_IOS -#include -#else -#include -#endif - -OPENSWIFTUI_ASSUME_NONNULL_BEGIN - -OPENSWIFTUI_EXPORT -BOOL CoreColorPlatformColorGetComponents(BOOL isAppKitBased, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha); - -OPENSWIFTUI_EXPORT -Class CoreColorGetKitColorClass(BOOL isAppKitBased); - -OPENSWIFTUI_ASSUME_NONNULL_END - -#endif - -#endif /* CoreColor_h */ diff --git a/Sources/COpenSwiftUI/LockedPointer.c b/Sources/COpenSwiftUICore/Other/LockedPointer.c similarity index 100% rename from Sources/COpenSwiftUI/LockedPointer.c rename to Sources/COpenSwiftUICore/Other/LockedPointer.c diff --git a/Sources/COpenSwiftUI/MovableLock.c b/Sources/COpenSwiftUICore/Other/MovableLock.c similarity index 100% rename from Sources/COpenSwiftUI/MovableLock.c rename to Sources/COpenSwiftUICore/Other/MovableLock.c diff --git a/Sources/COpenSwiftUI/TLS.c b/Sources/COpenSwiftUICore/Other/TLS.c similarity index 100% rename from Sources/COpenSwiftUI/TLS.c rename to Sources/COpenSwiftUICore/Other/TLS.c diff --git a/Sources/COpenSwiftUICore/Overlay/CoreGraphics/CoreColor.m b/Sources/COpenSwiftUICore/Overlay/CoreGraphics/CoreColor.m new file mode 100644 index 00000000..1b42bf3f --- /dev/null +++ b/Sources/COpenSwiftUICore/Overlay/CoreGraphics/CoreColor.m @@ -0,0 +1,239 @@ +// +// CoreColor.h +// COpenSwiftUI +// Audited for RELEASE_2024 +// Status: Complete + +#include "CoreColor.h" + +#if OPENSWIFTUI_TARGET_OS_DARWIN +#include "CoreGraphicsContext.h" + +Class CoreColorClass(BOOL isAppKitBased); + +#if OPENSWIFTUI_TARGET_OS_OSX +id NSColorSpaceForCGColorSpace(CGColorSpaceRef cgColorSpace); +Class NSColorSpaceClass(void); +#endif + +BOOL CoreColorPlatformColorGetComponents(BOOL system, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha) { + if (!color) { + return NO; + } + Class colorClass = CoreColorClass(system); + if (!colorClass) { + return NO; + } + #if OPENSWIFTUI_TARGET_OS_OSX + if (system) { + id colorSpace = NSColorSpaceForCGColorSpace(CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB)); + NSColor *nameSpaceColor = [color colorUsingColorSpace:colorSpace]; + if (nameSpaceColor) { + [nameSpaceColor getRed:red green:green blue: blue alpha: alpha]; + return YES; + } else { + return NO; + } + } + #endif + // NOTE: Fix Mac Catalyst selector type issue + return ((BOOL (*)(id, SEL))[color methodForSelector:@selector(getRed:green:blue:alpha:)])(color, @selector(getRed:green:blue:alpha:)); +} + +NSObject *CorePlatformColorForRGBA(BOOL system, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha) { + Class colorClass = CoreColorClass(system); + if (!colorClass) { + return nil; + } + #if OPENSWIFTUI_TARGET_OS_OSX + if (system) { + id colorSpace = NSColorSpaceForCGColorSpace(CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB)); + return [colorClass colorWithColorSpace:colorSpace components:(CGFloat[]){red, green, blue, alpha} count:4]; + } + #endif + return [[colorClass alloc] initWithRed:red green:green blue:blue alpha:alpha]; +} + +Class CoreColorGetKitColorClass(BOOL system) { + CoreColorClass(system); +} + +Class CoreColorClass(BOOL system) { + static BOOL isValid = false; + static Class colorClass; + static dispatch_once_t once; + dispatch_once(&once, ^{ + if (system) { + Class class = NSClassFromString(@"NSColor"); + colorClass = class; + isValid = class != nil; + } else { + Class class = NSClassFromString(@"UIColor"); + colorClass = class; + isValid = class != nil; + } + }); + if (isValid) { + return colorClass; + } else { + [NSException raise:@"Invalid core color" format:@""]; + } +} + +#if OPENSWIFTUI_TARGET_OS_OSX +id NSColorSpaceForCGColorSpace(CGColorSpaceRef cgColorSpace) { + Class colorSpaceClass = NSColorSpaceClass(); + if (colorSpaceClass) { + return [[colorSpaceClass alloc] initWithCGColorSpace:cgColorSpace]; + } else { + return nil; + } +} + +OPENSWIFTUI_INLINE +Class NSColorSpaceClass(void) { + static Class colorSpaceClass; + static dispatch_once_t once; + dispatch_once(&once, ^{ + colorSpaceClass = NSClassFromString(@"NSColorSpace"); + }); + return colorSpaceClass; +} +#endif + +@interface CoreColor () { + CGColorRef _color; +} +@end + +@implementation CoreColor + ++ (NSObject *)colorWithSystem:(BOOL)system cgColor: (CGColorRef)cgColor { + Class colorClass = CoreColorClass(system); + if (colorClass) { + return [colorClass colorWithCGColor: cgColor]; + } else { + return [[CoreColor alloc] initWithCGColor: cgColor]; + } +} + ++ (NSObject *)blackColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass blackColor]; +} + ++ (NSObject *)systemRedColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemRedColor]; +} + ++ (NSObject *)systemOrangeColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemOrangeColor]; +} + ++ (NSObject *)systemYellowColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemYellowColor]; +} + ++ (NSObject *)systemGreenColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemGreenColor]; +} + ++ (NSObject *)systemTealColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemTealColor]; +} + ++ (NSObject *)systemMintColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemMintColor]; +} + ++ (NSObject *)systemCyanColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemCyanColor]; +} + ++ (NSObject *)systemBlueColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemBlueColor]; +} + ++ (NSObject *)systemIndigoColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemIndigoColor]; +} + ++ (NSObject *)systemPurpleColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemPurpleColor]; +} + ++ (NSObject *)systemPinkColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemPinkColor]; +} + ++ (NSObject *)systemBrownColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemBrownColor]; +} + ++ (NSObject *)systemGrayColorWithSystem:(BOOL)system { + Class colorClass = CoreColorClass(system); + return [colorClass systemGrayColor]; +} + +- (instancetype)initWithCGColor:(CGColorRef)cgColor { + if (self = [super init]) { + _color = CGColorRetain(cgColor); + } + return self; +} + +- (void)dealloc { + CGColorRelease(_color); +} + +- (void)set { + CGContextRef context = CoreGraphicsContext.current.CGContext; + if (!context) { + return; + } + CGContextSetFillColorWithColor(context, self.CGColor); + CGContextSetStrokeColorWithColor(context, self.CGColor); +} + +- (void)setFill { + CGContextRef context = CoreGraphicsContext.current.CGContext; + if (!context) { + return; + } + CGContextSetFillColorWithColor(context, self.CGColor); +} + +- (void)setStroke { + CGContextRef context = CoreGraphicsContext.current.CGContext; + if (!context) { + return; + } + CGContextSetStrokeColorWithColor(context, self.CGColor); +} + +- (CGColorRef)CGColor { + return _color; +} + +- (CoreColor *)colorWithAlphaComponent:(CGFloat)alpha { + CGColorRef newColor = CGColorCreateCopyWithAlpha(_color, alpha); + CoreColor *color = [[CoreColor alloc] initWithCGColor:newColor]; + CGColorRelease(newColor); + return color; +} + +@end + +#endif diff --git a/Sources/COpenSwiftUICore/Overlay/CoreGraphics/CoreGraphicsContext.m b/Sources/COpenSwiftUICore/Overlay/CoreGraphics/CoreGraphicsContext.m new file mode 100644 index 00000000..604d3c00 --- /dev/null +++ b/Sources/COpenSwiftUICore/Overlay/CoreGraphics/CoreGraphicsContext.m @@ -0,0 +1,74 @@ +// +// CoreColor.h +// COpenSwiftUI +// Audited for RELEASE_2024 +// Status: WIP + +#include "CoreGraphicsContext.h" + +#if OPENSWIFTUI_TARGET_OS_DARWIN +#include + +static _Thread_local __unsafe_unretained CoreGraphicsContext * _current = NULL; + +IMP _pushContextIMP; +IMP _popContextIMP; + +@interface CoreGraphicsContext () { + CoreGraphicsContext *_next; + CGContextRef _ctx; +} +- (id)__createsImages; +@end + +@implementation CoreGraphicsContext + +- (instancetype)initWithCGContext:(CGContextRef)ctx { + static dispatch_once_t __once; + dispatch_once(&__once, ^{ + Class renderClass = NSClassFromString(@"UIGraphicsRenderer"); + if (renderClass) { + _pushContextIMP = [renderClass instanceMethodForSelector:@selector(pushContext:)]; + _popContextIMP = [renderClass instanceMethodForSelector:@selector(popContext:)]; + } else { + // TODO: CoreTextGraphicsContextProvider.sharedProvider + } + }); + self = [super init]; + if (self) { + _ctx = ctx; + } + return self; +} + +- (void)push { + _next = _current; + _current = self; + if (_pushContextIMP != NULL && _popContextIMP != NULL) { + typedef BOOL (*FUNC)(id, SEL, CoreGraphicsContext *); + ((FUNC)(_pushContextIMP))(NULL, @selector(pushContext:), _current); + } +} + +- (void)pop { + _current = _next; + if (_pushContextIMP != NULL && _popContextIMP != NULL) { + typedef BOOL (*FUNC)(id, SEL, CoreGraphicsContext *); + ((FUNC)(_popContextIMP))(NULL, @selector(popContext:), _current); + } +} + +- (id)__createsImages { + return nil; +} + +- (CGContextRef)CGContext { + return _ctx; +} + ++ (CoreGraphicsContext *)current { + return _current; +} + +@end +#endif diff --git a/Sources/COpenSwiftUI/Overlay/CoreGraphics/__CGPathParseString.m b/Sources/COpenSwiftUICore/Overlay/CoreGraphics/__CGPathParseString.m similarity index 85% rename from Sources/COpenSwiftUI/Overlay/CoreGraphics/__CGPathParseString.m rename to Sources/COpenSwiftUICore/Overlay/CoreGraphics/__CGPathParseString.m index 1aa12ecd..308716ab 100644 --- a/Sources/COpenSwiftUI/Overlay/CoreGraphics/__CGPathParseString.m +++ b/Sources/COpenSwiftUICore/Overlay/CoreGraphics/__CGPathParseString.m @@ -2,7 +2,7 @@ // __CGPathParseString.m // COpenSwiftUI -#include "__CGPathParseString.h" +#import "__CGPathParseString.h" #if OPENSWIFTUI_TARGET_OS_DARWIN BOOL __CGPathParseString(CGMutablePathRef path, const char *utf8CString) { diff --git a/Sources/COpenSwiftUICore/README.md b/Sources/COpenSwiftUICore/README.md new file mode 100644 index 00000000..44a57483 --- /dev/null +++ b/Sources/COpenSwiftUICore/README.md @@ -0,0 +1,3 @@ +## COpenSwiftUICore + +TODO: module name - OpenSwiftUI_SPI diff --git a/Sources/COpenSwiftUICore/include/CoreColor.h b/Sources/COpenSwiftUICore/include/CoreColor.h new file mode 100644 index 00000000..9a319ef3 --- /dev/null +++ b/Sources/COpenSwiftUICore/include/CoreColor.h @@ -0,0 +1,62 @@ +// +// CoreColor.h +// COpenSwiftUI +// Audited for RELEASE_2024 +// Status: Complete + +#ifndef CoreColor_h +#define CoreColor_h + +#include "OpenSwiftUIBase.h" + +#if OPENSWIFTUI_TARGET_OS_DARWIN + +#if OPENSWIFTUI_TARGET_OS_IOS +#include +#else +#include +#endif + +OPENSWIFTUI_ASSUME_NONNULL_BEGIN + +OPENSWIFTUI_EXPORT +BOOL CoreColorPlatformColorGetComponents(BOOL system, id color, CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha); + +OPENSWIFTUI_EXPORT +NSObject * _Nullable CorePlatformColorForRGBA(BOOL system, CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha); + +OPENSWIFTUI_EXPORT +Class CoreColorGetKitColorClass(BOOL system); + +@interface CoreColor : NSObject + ++ (nullable NSObject *)colorWithSystem:(BOOL)system cgColor: (CGColorRef)cgColor; ++ (nullable NSObject *)blackColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemRedColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemOrangeColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemYellowColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemGreenColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemTealColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemMintColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemCyanColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemBlueColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemIndigoColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemPurpleColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemPinkColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemBrownColorWithSystem:(BOOL)system; ++ (nullable NSObject *)systemGrayColorWithSystem:(BOOL)system; + +- (instancetype)initWithCGColor:(CGColorRef)cgColor; +- (void)set; +- (void)setFill; +- (void)setStroke; +- (CGColorRef)CGColor; +- (CoreColor *)colorWithAlphaComponent:(CGFloat)alpha; + +@end + +OPENSWIFTUI_ASSUME_NONNULL_END + +#endif + +#endif /* CoreColor_h */ diff --git a/Sources/COpenSwiftUICore/include/CoreGraphicsContext.h b/Sources/COpenSwiftUICore/include/CoreGraphicsContext.h new file mode 100644 index 00000000..2cd1a602 --- /dev/null +++ b/Sources/COpenSwiftUICore/include/CoreGraphicsContext.h @@ -0,0 +1,33 @@ +// +// CoreGraphicsContext.h +// COpenSwiftUI +// Audited for RELEASE_2024 +// Status: Complete + +#ifndef CoreGraphicsContext_h +#define CoreGraphicsContext_h + +#include "OpenSwiftUIBase.h" + +#if OPENSWIFTUI_TARGET_OS_DARWIN + +#include + +OPENSWIFTUI_ASSUME_NONNULL_BEGIN + +@interface CoreGraphicsContext : NSObject + +@property (class, nonatomic, readonly) CoreGraphicsContext *current; +@property (nonatomic, readonly) CGContextRef CGContext; + +- (instancetype)initWithCGContext:(CGContextRef)ctx; +- (void)push; +- (void)pop; + +@end + +OPENSWIFTUI_ASSUME_NONNULL_END + +#endif + +#endif /* CoreGraphicsContext_h */ diff --git a/Sources/COpenSwiftUI/include/LockedPointer.h b/Sources/COpenSwiftUICore/include/LockedPointer.h similarity index 100% rename from Sources/COpenSwiftUI/include/LockedPointer.h rename to Sources/COpenSwiftUICore/include/LockedPointer.h diff --git a/Sources/COpenSwiftUI/include/MovableLock.h b/Sources/COpenSwiftUICore/include/MovableLock.h similarity index 100% rename from Sources/COpenSwiftUI/include/MovableLock.h rename to Sources/COpenSwiftUICore/include/MovableLock.h diff --git a/Sources/COpenSwiftUI/include/OpenSwiftUI_SPI.h b/Sources/COpenSwiftUICore/include/OpenSwiftUI_SPI.h similarity index 100% rename from Sources/COpenSwiftUI/include/OpenSwiftUI_SPI.h rename to Sources/COpenSwiftUICore/include/OpenSwiftUI_SPI.h diff --git a/Sources/COpenSwiftUI/include/TLS.h b/Sources/COpenSwiftUICore/include/TLS.h similarity index 100% rename from Sources/COpenSwiftUI/include/TLS.h rename to Sources/COpenSwiftUICore/include/TLS.h diff --git a/Sources/COpenSwiftUI/include/__CGPathParseString.h b/Sources/COpenSwiftUICore/include/__CGPathParseString.h similarity index 100% rename from Sources/COpenSwiftUI/include/__CGPathParseString.h rename to Sources/COpenSwiftUICore/include/__CGPathParseString.h diff --git a/Sources/OpenSwiftUI/View/Graphic/Color/ColorResolved.swift b/Sources/OpenSwiftUI/View/Graphic/Color/ColorResolved.swift index b39d850f..f043bae6 100644 --- a/Sources/OpenSwiftUI/View/Graphic/Color/ColorResolved.swift +++ b/Sources/OpenSwiftUI/View/Graphic/Color/ColorResolved.swift @@ -309,5 +309,14 @@ extension Color.Resolved { return nil } } + + // ID: 4330A474F53D66045762501ED6F8A749 + private static let cache: ObjectCache = ObjectCache { resolved in + CoreColor.platformColor(resolvedColor: resolved)! + } + + package var kitColor: NSObject { + Self.cache[self] + } } #endif diff --git a/Sources/OpenSwiftUI/View/Graphic/Color/CoreColor+Extension.swift b/Sources/OpenSwiftUI/View/Graphic/Color/CoreColor+Extension.swift new file mode 100644 index 00000000..68d9b63e --- /dev/null +++ b/Sources/OpenSwiftUI/View/Graphic/Color/CoreColor+Extension.swift @@ -0,0 +1,73 @@ +// +// CoreColor+Extension.swift +// OpenSwiftUI +// +// Audited for RELEASE_2024 +// Status: WIP + +import COpenSwiftUICore + +extension CoreColor { + package static func platformColor(resolvedColor: Color.Resolved) -> NSObject? { + platformColor(red: CGFloat(resolvedColor.red), green: CGFloat(resolvedColor.green), blue: CGFloat(resolvedColor.blue), alpha: CGFloat(resolvedColor.opacity)) + } + + package static func platformColor(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) -> NSObject? { + CorePlatformColorForRGBA(isAppKitBased(), red, green, blue, alpha) + } + + #if os(iOS) + static var systemRed: NSObject? { + systemRedColor(withSystem: isAppKitBased()) + } + + static var systemOrange: NSObject? { + systemOrangeColor(withSystem: isAppKitBased()) + } + + static var systemYellow: NSObject? { + systemYellowColor(withSystem: isAppKitBased()) + } + + static var systemGreen: NSObject? { + systemGreenColor(withSystem: isAppKitBased()) + } + + static var systemTeal: NSObject? { + systemTealColor(withSystem: isAppKitBased()) + } + + static var systemMint: NSObject? { + systemMintColor(withSystem: isAppKitBased()) + } + + static var systemCyan: NSObject? { + systemCyanColor(withSystem: isAppKitBased()) + } + + static var systemBlue: NSObject? { + systemBlueColor(withSystem: isAppKitBased()) + } + + static var systemIndigo: NSObject? { + systemIndigoColor(withSystem: isAppKitBased()) + } + + static var systemPurple: NSObject? { + systemPurpleColor(withSystem: isAppKitBased()) + } + + static var systemPink: NSObject? { + systemPinkColor(withSystem: isAppKitBased()) + } + + static var systemBrown: NSObject? { + systemBrownColor(withSystem: isAppKitBased()) + } + + static var systemGray: NSObject? { + systemGrayColor(withSystem: isAppKitBased()) + } + #endif +} + diff --git a/Tests/OpenSwiftUITests/View/Graphics/Color/ColorResolvedTests.swift b/Tests/OpenSwiftUITests/View/Graphics/Color/ColorResolvedTests.swift new file mode 100644 index 00000000..f29ad47e --- /dev/null +++ b/Tests/OpenSwiftUITests/View/Graphics/Color/ColorResolvedTests.swift @@ -0,0 +1,19 @@ +// +// ColorResolvedTests.swift +// OpenSwiftUITests + +@testable import OpenSwiftUI +import Testing + +struct ColorResolvedTests { + @Test + func kitColor() { + let r1 = Color.Resolved(colorSpace: .sRGB, red: 1, green: 1, blue: 1, opacity: 1) + let r2 = Color.Resolved(colorSpace: .sRGBLinear, red: 1, green: 1, blue: 1, opacity: 1) + + #expect(r2.kitColor === r1.kitColor) + + let r3 = Color.Resolved(colorSpace: .sRGB, red: 0.3, green: 0.3, blue: 0.3, opacity: 0.3) + #expect(r3.kitColor !== r1.kitColor) + } +}