CGColor to UIColor conversion
Thats because that CGColor initializer uses a different color space. UIColor uses extendedSRGB while CGColor uses Generic RGB.
If you want to get the same color you need to specify the same colorspace when initializing your CGColor:
let cgColor = CGColor(colorSpace: .init(name: CGColorSpace.extendedSRGB)!, components: [19.0/255, 25.0/255, 28.0/255, 1.0])!
For iOS13+ or macOS10.15+ you can use the init(srgbRed:green:blue:alpha:)
initializer:
let cgColor = CGColor(srgbRed: 19/255, green: 25/255, blue: 28/255, alpha: 1)
Odd Conversion between UIColor and CGColor
You need to divide the parameters by 255.0. As noted by @Duncan C, ensure you are dividing by 255.0
[[UIColor colorWithRed:202.0/255.0 green:0 blue:11/255.0 alpha:1] CGColor]
[[UIColor colorWithRed:0 green:19/255.0 blue:133/255.0 alpha:1] CGColor]
Wrong color when convert UIColor to CGColor
Colors in UIKit are specified using value is between 0 and 1 in float not int from 0 to 255, so you need to divide all your RGB values by 255.0.
let color = UIColor(red: 198.0/255.0, green: 35.0/255.0, blue: 80.0/255.0, alpha: 1.0)
IOS/Objective-C: Convert standard UIColor to CGColor
You can't save UIColor object in NSUserDefaults directly.
try to archive object to get data and save the data like this:
UIColor *color = [UIColor redColor];
NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"ColorKey"];
And when you need the color firstly you should get NSData object from User Defaults and then create UIColor object like this
NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"ColorKey"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
Converting UIColor to CGColor in swift
// Original answer.
var newColor = UIColor.lightGrayColor().CGColor
// Swift 3 version
var newColor = UIColor.lightGray.cgColor
How to convert UIColor to SwiftUI‘s Color
Starting with beta 5, you can create a Color from a UIColor:
Color(UIColor.systemBlue)
Related Topics
Changing the Status Bar Text Color in Splash Screen iOS 7
Is It Necessary to Use [Unowned Self] in Closures of Uiview.Animatewithduration(...)
Change the Font Size of Uisearchbar
How to Define the Size of a Collectionview on Rotate
How to Create a Glow Around a Sprite via Skeffectnode
Open Specific View When Opening App from Notification
Disabled Uibutton Not Faded or Grey
Nsattributedstring Inserting a Bullet Point
How to Create an Ibinspectable of Type Enum
"Initialize" Class Method for Classes in Swift
How to Check Where My App Is Using Idfa
Can't Prevent 'Touchmove' from Scrolling Window on iOS
Nstimer - How to Delay in Swift
"Could Not Find Any Information for Class Named Viewcontroller"