Skip to content

Commit

Permalink
Add Color.Resolved init with platformColor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Sep 15, 2024
1 parent 3d95540 commit e4a4e34
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
90 changes: 90 additions & 0 deletions Sources/COpenSwiftUI/Overlay/CoreColor/CoreColor.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// 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
32 changes: 32 additions & 0 deletions Sources/COpenSwiftUI/include/CoreColor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// 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 <UIKit/UIKit.h>
#else
#include <AppKit/AppKit.h>
#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 */
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@

#define OPENSWIFTUI_TARGET_OS_WIN32 OPENSWIFTUI_TARGET_OS_WINDOWS
#define OPENSWIFTUI_TARGET_OS_MAC OPENSWIFTUI_TARGET_OS_DARWIN
#define OPENSWIFTUI_TARGET_OS_OSX OPENSWIFTUI_TARGET_OS_DARWIN

/* OpenSwiftUI Addition Begin */
#if OPENSWIFTUI_TARGET_OS_DARWIN
#include <TargetConditionals.h>
#define OPENSWIFTUI_TARGET_OS_OSX TARGET_OS_OSX
#define OPENSWIFTUI_TARGET_OS_IPHONE TARGET_OS_IPHONE
#define OPENSWIFTUI_TARGET_OS_IOS TARGET_OS_IOS
#define OPENSWIFTUI_TARGET_OS_WATCH TARGET_OS_WATCH
Expand Down
22 changes: 22 additions & 0 deletions Sources/OpenSwiftUI/View/Graphic/Color/ColorResolved.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Status: WIP

import Foundation
import OpenSwiftUICore
internal import COpenSwiftUI

// MARK: - Color.Resolved

Expand Down Expand Up @@ -289,3 +291,23 @@ func sRGBToLinear(_ sRGB: Float) -> Float {
}
return sRGB > 0 ? result : -result
}

#if canImport(Darwin)

// MARK: - Color.Resolved + platformColor

extension Color.Resolved {
package init?(platformColor: AnyObject) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
let result = CoreColorPlatformColorGetComponents(isAppKitBased(), platformColor, &red, &green, &blue, &alpha)
if result {
self.init(red: Float(red), green: Float(green), blue: Float(blue), opacity: Float(alpha))
} else {
return nil

Check warning on line 309 in Sources/OpenSwiftUI/View/Graphic/Color/ColorResolved.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/View/Graphic/Color/ColorResolved.swift#L300-L309

Added lines #L300 - L309 were not covered by tests
}
}
}
#endif

0 comments on commit e4a4e34

Please sign in to comment.