Skip to content

SwiftUI's Color object is not codable - and therefore not easy to store. This makes it simple by allowing you to store a Color using Color.description and then recreating the Color using this function as an extension to Color

License

Notifications You must be signed in to change notification settings

tj4shee/Color.fromString

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Color.fromString

An important update ! From a Reddit user (pengo-san) that commented on my thread for this... they found the following blog entry that you should read if you are needing more than what my solution offers. https://nilcoalescing.com/blog/EncodeAndDecodeSwiftUIColor/

SwiftUI's Color object is not codable - and therefore not easy to store. This makes it simple by allowing you to store a Color using Color.description and then recreating the Color using this function as an extension to Color

Simple to use...

Add this to any of your apps .swift files - outside of any structs or classes

Save a Color to a String using Color.description (this even works with the different Color.description obtained from ColorPicker and by using Color enums such as Color.Pink) You can also manually create a color string using the format "#rrggbboo" where the initial # is required, then the rr, gg, bb and oo are hexadecimal numbers for Red, Green, Blue and Opacity components.

Recreate the Color from the stored String using Color.fromString()

Example...

var myColorString: String = "#C0C0C0FF" var myColorObj: Color = .clear var myPink: String = Color.pink.description var pinkColorObj: Color = .clear

in code... myColorObj = Color.fromString(myColorString) //Returns a Color that is gray (rgba = C0C0C0FF) pinkColorObj = Color.fromString(myPink) //Returns a Color equal to Color.pink

you can now use a Color.description String in structs (and classes) and make the struct Codable... you can also store Colors in CoreData as a string

Please feel free to make this better and share it with me !

About

SwiftUI's Color object is not codable - and therefore not easy to store. This makes it simple by allowing you to store a Color using Color.description and then recreating the Color using this function as an extension to Color

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages