0

I want to save SwiftUI Color's (type Color not UIColor) to my server. To do that I need to convert them to a string. I think the most convenient would be a Hex String. I followed the following stack solution to convert a Hex to a SwiftUI color:

Use Hex color in SwiftUI

but I'm not sure how to go the other way and convert the SwiftUI Color type to a Hex String?

alionthego
  • 8,508
  • 9
  • 52
  • 125
  • 1
    It is possible to use `Color.cgColor.components`, but I would recommend to keep `Color` as view and `hex` as model, so operate with hex at model/view-model level. – Asperi Feb 07 '22 at 11:24
  • thanks. that seems like a much simpler and more sensible way to handle this issue. – alionthego Feb 07 '22 at 11:25
  • There's a github package that can help, [UIColor-Hex-Swift](https://github.com/yeahdongcn/UIColor-Hex-Swift). Then you can get the hex string like this: `UIColor(color).hexString()` and the other way: `Color(UIColor(hexColor))` – tromgy Feb 07 '22 at 11:33
  • you could have a look at this SO post/answer https://stackoverflow.com/questions/68679658/swift-5-4-hex-to-nscolor/68680901#68680901 – workingdog support Ukraine Feb 07 '22 at 13:20

0 Answers0