Xcode 11 #colorLiteral written instead of being displayed as color - swift

I recently updated my Xcode from 10.3 to 11.0.
I registered all the colors I am using among the app in a Scheme.swift file with its own struct.
Since I updated, the #colorLiterals are not computed properly by Xcode in my struct constructor but just written as code:
Xcode 11
Raw code, so you can copy/paste it in a playground
import UIKit
struct Theme {
var color1: UIColor
var color2: UIColor
}
Theme(color1: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0),
color2: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0))
Does anyone know how I can force Xcode to refresh or compute these colorLiterals so I can use the color picker as usual?
Is it not possible anymore to define a colorLiteral in a function call (here a struct init)?
Edit
It seems to be related to the fact that my #colorLiterals are written in a function call, before a ,. If I surround it by parenthesis it automatically switch to a "color picker".
Plus, it doesn't happen to the last colorLiteral in the call

Related

SKLightNode with flat shape

Does anyone knows what could affect the "shape" for a SKLightNode?
In a Playground project, I added 2 lights: one for the ambiant light (located of the scene) and one for a local light (located on scene) and the "shape" of the light in nice and round like here:
But adding the same code in another project, the light is having a flat shape like this:
In both cases, the light is added as part of the SKScene and I change the colour with this:
backgroundLightNode.ambientColor = NSColor(red: 1.0, green: 1.0, blue: 1.0, alpha: alpha)
lightNode.lightColor = NSColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1 - alpha)

How to convert UIColor to SwiftUI‘s Color

I want to use a UIColor as foregroundcolor for an object but I don’t know how to convert UIColor to a Color
var myColor: UIColor
RoundedRectangle(cornerRadius: 5).foregroundColor(UIColor(myColor))
Starting with beta 5, you can create a Color from a UIColor:
Color(UIColor.systemBlue)
Both iOS and macOS
Color has a native initializer for it that takes an UIColor or NSColor as an argument:
Color(.red) /* or any other UIColor/NSColor you need INSIDE parentheses */
DO NOT call Color(UIColor.red) explicitly !!!. This will couple your SwiftUI code to the UIKit. Instead, just call Color(.red) That will infer the correct module automatically.
Also, Note this difference:
Color.red /* this `red` is SwiftUI native `red` */
Color(.red) /* this `red` is UIKit `red` */
Note that:
Color.red and UIColor.red are NOT same! They have different values and look different with each other. So DON'T assume this worth nothing
These are equal instead: SwiftUI.Color.Red == UIKit.UIColor.systemRed
Also, You can check out How to get RGB components from SwiftUI.Color
Extension
You can implement a custom variable for it to make it more like cgColor and ciColor
extension UIColor {
/// The SwiftUI color associated with the receiver.
var suColor: Color { Color(self) }
}
so it would be like:
UIColor.red // UIKit color
UIColor.red.suColor // SwiftUI color
UIColor.red.cgColor // Core graphic color
UIColor.red.ciColor // Core image color
Note: Click here to see How to convert SwiftUI.Color to UIColor
Using two helper extensions:
To extract components from UIColor:
extension UIColor {
var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
getRed(&red, green: &green, blue: &blue, alpha: &alpha)
return (red, green, blue, alpha)
}
}
To init with UIColor:
extension Color {
init(uiColor: UIColor) {
self.init(red: Double(uiColor.rgba.red),
green: Double(uiColor.rgba.green),
blue: Double(uiColor.rgba.blue),
opacity: Double(uiColor.rgba.alpha))
}
}
Usage:
Color(uiColor: .red)
In Swift UI custom colors with a convenient extension:
extension UIColor {
struct purple {
static let normal = UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
static let light = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
static let dark = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
}
struct gray {
static let normal = UIColor(red:0.5, green:0.5 ,blue:0.5 , alpha:1.00)
static let dark = UIColor(red: 1, green: 1, blue: 1, alpha: 1)
}
}
Wrapping Color of SwiftUI:
extension UIColor {
var toSUIColor: Color {
Color(self)
}
}
and using this:
var body: some View {
Text("Hello World")
.foregroundColor(Color(UIColor.purple.normal))
.background(Color(UIColor.gray.normal))
// with wrap
//.foregroundColor(UIColor.purple.normal.toSUIColor)
//.background(UIColor.gray.normal.toSUIColor)
}
I'm a really old hobbyist. Here is one way that works for me. Yes, I do use globals for reusable statements in a Constant.swift file. This example is inline so that it is easier to see. I do not say this is the way to go, it is just my old way.
Screenshot (27k)
import SwiftUI
// named color from the developer's pallet
let aluminumColor = Color(UIColor.lightGray)
// a custom color I use often
let butterColor = Color.init(red: 0.9993399978,
green: 0.9350042167,
blue: 0.5304131241)
// how I use this in a SwiftUI VStack:
Text("Fur People")
.font(.title)
.foregroundColor(aluminumColor)
.shadow(color: butterColor, radius: 4, x: 2, y: 2)
.minimumScaleFactor(0.75)
Create an extension like this:
extension Color {
static let someColor = Color(UIColor.systemIndigo) // << Select any UIColor
}
Usage:
struct ContentView: View {
var body: some View {
Rectangle()
.frame(width: 100, height: 100)
.foregroundColor(.someColor)
}
}

Type 'NSColor' has no member 'black'

I'm working with Swift and SceneKit in Xcode 9.2. I've been applying color to geometry with NSColor, but all of a sudden (gradually) most of my colors get the type NSColor has no member .colorname. Like my game compiled and ran just fine, and then on the next compile, I got an error with .cyan. I changed it to .black and it ran. Then another color got an error, and then most of the colors got an error.
Here's an example:
node.geometry?.firstMaterial?.diffuse.contents = NSColor.black
the options that I have left are:
.blue, .green, .int, .rawValue, .red, .yellow.
What happened to all the colors?
EDIT: I cleaned up the project, it all compiles without warnings but I still only have 4 colors and they all render gray. I printed out the nodes diffuse colors to console and they read correct.
I've also imported Cocoa framework.
EDIT: I FOUND MY MISTAKE.
I accidentally replaced the name of an enum with NSColor while doing a find and replace. That's why SKColor worked, because it was a new name.
I am so embarrassed.
You could use:
node.geometry?.firstMaterial?.diffuse.contents = NSColor(srgbRed: 0, green: 0, blue: 0, alpha: 1.0)
In this way you create a color with your own RGBs values, so in this way you can get the color black. The last parameter, alpha, is the value of opacity and it needs to be 100 in order to see the color
In your case the better way is to use this syntax:
NSColor(calibratedRed: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
In iOS you should use the syntax like this:
cube.firstMaterial?.diffuse.contents = UIColor(red: 0.3, green: 0.45, blue: 0.75, alpha: 1.0)
In macOS the syntax is like this:
sphere.firstMaterial?.reflective.contents = NSColor(calibratedRed: 99.0, green: 0.0, blue: 0.0, alpha: 1.0)
Remember, the alpha (aka opacity), specified as a value from 0.0-1.0. Alpha values below 0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.
And, of course, you can specify the colorspace explicitly (like sRGB):
init(srgbRed: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)

Fill Quartz 2D line with custom Texture instead of UIColor

So instead of this:
context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
I want to be able to fill / add a texture to those drawed lines.
I found out that you could use CGPatternCreate in Objective-C for this.
Does this apply to Swift as well and if so how can I do this?
Thanks

How to update SKLabelNode color after the node is initiated?

I have SKLabelNode in my Swift-code. I need to change the Label's color during SKAction. Simply:
override func didMoveToView(view: SKView) {
...
var color = UIColor(red: CGFloat(1.0), green: CGFloat(0.0), blue: CGFloat(0.0), alpha: CGFloat(0.0))
myLabel.fontColor = color
...
}
Doesn't work. I still have to somehow update the node but how? I'm noobie to Swift and Sprite Kit.
Do you need it to be in an SKAction? If not, simply use this:
myLabel.fontColor = SKColor.blueColor()
Substitute blueColor with whichever color you want, or use the generic method where 'float here' is a fraction of 255 (such as 50.0f/255.0f).
myLabel.fontColor = SKColor(red: floatHere, green: floatHere, blue: floatHere, alpha: floatFrom0To1Here)
In case you do need to set the color through an SKAction, you can use this method:
myLabel.runAction(SKAction.colorizeWithColor(UIColor.blueColor(), colorBlendFactor: 1, duration: 1))
I had a similar problem a few weeks ago.
Try changing your color variable to the following:
var color = UIColor(red: 1.0 / 255, green: 0.0 / 255, blue: 0.0 / 255, alpha: 0.0)