Use CGColor and remove PlatformColor

This commit is contained in:
Guille Gonzalez
2026-01-04 07:09:56 +01:00
parent fe6df90c4a
commit 6e1a38ede7
5 changed files with 33 additions and 41 deletions

View File

@@ -5,8 +5,8 @@ import Foundation
extension CGContext { extension CGContext {
func draw(_ displayNode: Math.DisplayNode, foregroundColor: CGColor) { func draw(_ displayNode: Math.DisplayNode, foregroundColor: CGColor) {
let foregroundColor = let foregroundColor =
displayNode.localTextColor?.cgColor displayNode.localTextColor
?? displayNode.textColor?.cgColor ?? displayNode.textColor
?? foregroundColor ?? foregroundColor
switch displayNode { switch displayNode {

View File

@@ -9,9 +9,9 @@ extension Math {
var position: CGPoint = .zero var position: CGPoint = .zero
var range: NSRange = NSRange(location: 0, length: 0) var range: NSRange = NSRange(location: 0, length: 0)
var hasScript: Bool = false var hasScript: Bool = false
var textColor: PlatformColor? var textColor: CGColor?
var localTextColor: PlatformColor? var localTextColor: CGColor?
var localBackgroundColor: PlatformColor? var localBackgroundColor: CGColor?
func bounds() -> CGRect { func bounds() -> CGRect {
CGRect(x: position.x, y: position.y - descent, width: width, height: ascent + descent) CGRect(x: position.x, y: position.y - descent, width: width, height: ascent + descent)

View File

@@ -1,3 +1,4 @@
import CoreGraphics
import CoreText import CoreText
import Foundation import Foundation
@@ -423,7 +424,9 @@ extension Math {
return line return line
} }
static var placeholderColor: PlatformColor { PlatformColor.blue } static var placeholderColor: CGColor {
CGColor(srgbRed: 0, green: 0, blue: 1, alpha: 1)
}
init( init(
withFont font: PlatformFont?, style: Style.Level, cramped: Bool, spaced: Bool, withFont font: PlatformFont?, style: Style.Level, cramped: Bool, spaced: Bool,
@@ -986,7 +989,7 @@ extension Math {
let colorAtom = atom as! Color let colorAtom = atom as! Color
let display = Typesetter.createLineForMathList( let display = Typesetter.createLineForMathList(
colorAtom.innerList, font: font, style: style, maxWidth: maxWidth) colorAtom.innerList, font: font, style: style, maxWidth: maxWidth)
display!.localTextColor = PlatformColor(fromHexString: colorAtom.colorString) display!.localTextColor = CGColor.fromHexString(colorAtom.colorString)
// Check if we need to break before adding this colored content // Check if we need to break before adding this colored content
let shouldBreak = shouldBreakBeforeDisplay( let shouldBreak = shouldBreakBeforeDisplay(
@@ -1013,7 +1016,7 @@ extension Math {
let colorAtom = atom as! TextColor let colorAtom = atom as! TextColor
let display = Typesetter.createLineForMathList( let display = Typesetter.createLineForMathList(
colorAtom.innerList, font: font, style: style, maxWidth: maxWidth) colorAtom.innerList, font: font, style: style, maxWidth: maxWidth)
display!.localTextColor = PlatformColor(fromHexString: colorAtom.colorString) display!.localTextColor = CGColor.fromHexString(colorAtom.colorString)
// Check if we need to break before adding this colored content // Check if we need to break before adding this colored content
let shouldBreak = shouldBreakBeforeDisplay( let shouldBreak = shouldBreakBeforeDisplay(
@@ -1051,7 +1054,7 @@ extension Math {
let display = Typesetter.createLineForMathList( let display = Typesetter.createLineForMathList(
colorboxAtom.innerList, font: font, style: style, maxWidth: maxWidth) colorboxAtom.innerList, font: font, style: style, maxWidth: maxWidth)
display!.localBackgroundColor = PlatformColor(fromHexString: colorboxAtom.colorString) display!.localBackgroundColor = CGColor.fromHexString(colorboxAtom.colorString)
// Check if we need to break before adding this colorbox // Check if we need to break before adding this colorbox
let shouldBreak = shouldBreakBeforeDisplay( let shouldBreak = shouldBreakBeforeDisplay(
@@ -1387,7 +1390,7 @@ extension Math {
let color = Typesetter.placeholderColor let color = Typesetter.placeholderColor
current = NSAttributedString( current = NSAttributedString(
string: atom.nucleus, string: atom.nucleus,
attributes: [kCTForegroundColorAttributeName as NSAttributedString.Key: color.cgColor] attributes: [kCTForegroundColorAttributeName as NSAttributedString.Key: color]
) )
} else { } else {
current = NSAttributedString(string: atom.nucleus) current = NSAttributedString(string: atom.nucleus)

View File

@@ -0,0 +1,20 @@
import CoreGraphics
import Foundation
extension CGColor {
static func fromHexString(_ hexString: String) -> CGColor? {
guard !hexString.isEmpty, hexString.hasPrefix("#") else { return nil }
var rgbValue = UInt64(0)
let scanner = Scanner(string: hexString)
scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
guard scanner.scanHexInt64(&rgbValue) else { return nil }
return CGColor(
srgbRed: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: 1.0
)
}
}

View File

@@ -1,31 +0,0 @@
import SwiftUI
#if canImport(UIKit)
typealias PlatformColor = UIColor
typealias PlatformBezierPath = UIBezierPath
#elseif canImport(AppKit)
typealias PlatformColor = NSColor
typealias PlatformBezierPath = NSBezierPath
#endif
extension PlatformColor {
convenience init?(fromHexString hexString: String) {
self.init(hexString: hexString)
}
convenience init?(hexString: String) {
guard !hexString.isEmpty, hexString.hasPrefix("#") else { return nil }
var rgbValue = UInt64(0)
let scanner = Scanner(string: hexString)
scanner.charactersToBeSkipped = CharacterSet(charactersIn: "#")
guard scanner.scanHexInt64(&rgbValue) else { return nil }
self.init(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: 1.0
)
}
}