This basic code works until I try to add gradientColors
:
let noise = GKNoise(src)
noise.gradientColors = [ 0.0: NSColor.blue, 0.5: NSColor.green, 0.75: NSColor.white]
let map = GKNoiseMap(noise,
size: vector_double2(x: inParams.noiseSize, y: inParams.noiseSize),
origin: vector_double2(x:0, y:0),
sampleCount: vector_int2(x: Int32(inParams.size), y:Int32(inParams.size)),
seamless: true)
let tex = SKTexture(noiseMap: map)
At which point I get the following in the Xcode console, upon creating the SKTexture
:
*** -getRed:green:blue:alpha: not valid for the NSColor Generic Gray Gamma 2.2 Profile colorspace 1 1; need to first convert colorspace.
Other people seem to be able to do this, so I’m not sure what I’m doing differently. I can’t figure out where I might set the color space.
macOS 11.3, Xcode 12.5, SwiftUI app.
GameplayKit is invoking getRed(_:green:blue:alpha:)
on each of your NSColor
instances behind the scenes, and according to the documentation, this method only works on colors in the calibratedRGB
or deviceRGB
color spaces (although it appears to also work for other spaces, like sRGB
).
NSColor.blue
and NSColor.green
use sRGB
by default, but .white
uses NSColorSpace.genericGray
. To convert:
let white = NSColor.white.usingColorSpace(.sRGB)!
Alternatively, instantiating a color with its components directly, e.g. NSColor(red: 1, green: 1, blue: 1, alpha: 1)
, will put it in sRGB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With