Skip to content

Commit

Permalink
Add string utils for Color
Browse files Browse the repository at this point in the history
  • Loading branch information
metasidd committed Feb 23, 2025
1 parent 7feaa64 commit b144bfd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,4 @@ public extension Color {
func toXYZ() -> XYZColor {
RGBColor(color: self).toXYZ()
}

/// Get LCH string representation
func getLCHString() -> String {
let lchColor = LCHColor(color: self)
return "L:\(Int(lchColor.l)) C:\(Int(lchColor.c)) H:\(Int(lchColor.h))"
}
}
46 changes: 46 additions & 0 deletions Sources/ColorTokensKit/Platform/SwiftUI/Color+Strings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// LCH+Utils.swift
// ColorTokensKit
//
// Created by Siddhant Mehta on 2025-02-23.
//

import SwiftUI

public extension Color {

/// Converts the `LCHColor` to a hexadecimal string representation.
public func getHexString() -> String {
let resolvedColor = resolve(in: .init())

// Ensure RGB values are within [0, 1]
let r = Double(max(0, min(resolvedColor.red, 1)))
let g = Double(max(0, min(resolvedColor.green, 1)))
let b = Double(max(0, min(resolvedColor.blue, 1)))
let a = Double(max(0, min(resolvedColor.cgColor.alpha, 1)))

// Convert to hexadecimal
let hexString: String
if a != 1.0 {
hexString = String(format: "%02lX%02lX%02lX%02lX", lround(r * 255), lround(g * 255), lround(b * 255), lround(a * 255))
} else {
hexString = String(format: "%02lX%02lX%02lX", lround(r * 255), lround(g * 255), lround(b * 255))
}

print("red \(r)")
print("green \(g)")
print("blue \(b)")
print("alpha \(a)")
print("hex: \(hexString)")
print("-------")

return hexString
}

/// Get LCH string representation
public func getLCHString() -> String {
let lchColor = LCHColor(color: self)
return "L:\(Int(lchColor.l)) C:\(Int(lchColor.c)) H:\(Int(lchColor.h))"
}

}

0 comments on commit b144bfd

Please sign in to comment.